Open In App

Provisioning Virtual Machines with Ansible: A How-To Guide

Last Updated : 01 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Nowadays, with the rapidly changing world of IT infrastructure, the ability to provision and manage virtual machines (VMs) automatically is indispensable. Virtual machines offer great flexibility and scalability for deploying various applications, services, or development environments. Provisioning and setting up these VMs manually is a very time-consuming process and is prone to errors. This is where Ansible comes in.

Ansible is a free open-source automation tool for the provisioning and configuration of IT environments. With Ansible, you can automate VM provisioning effortlessly and make sure your infrastructure management remains consistent and effective. Define automation tasks with YAML, an easily human-readable language. that is easy for developers and system administrators to use.

This guide will walk you through provisioning virtual machines using Ansible, common terminologies that you need to know, the steps involved, practical examples, and best practices that run. No matter if you're managing a small number of VMs or scaling to a large infrastructure, there is the right set of automation and productivity tools available in Ansible so that you can easily get your job done.

Primary Terminologies

  • Ansible: A free open-source automation tool for configuration management, application deployment, and task automation, using simple YAML syntax to define tasks in playbooks.
  • Playbook: A YAML file that contains a list of tasks that are to be executed by Ansible on managed hosts. Playbooks lie at the core of Ansible's automation functionality, which helps determine how the desired state of the infrastructure is defined.
  • Task: a strictly atomic individual action in a playbook. A task contains a single command, a single script, or a single operation that is executed on target machines by Ansible.
  • Role: A set of related tasks, templates, and modules organized in a standardized file structure. Roles help in organizing and reusing the Ansible code across multiple playbooks.
  • Inventory: This is a file that contains a list of all the hosts, i.e., servers or VMs, that are about to be managed by Ansible. This can be done in two different ways: either static, defined in a file; or dynamic, fetched from a cloud provider or other sources.
  • Module: A unit of code that Ansible employs to carry out some task. Modules are the basic building blocks of tasks in Ansible, to allow you to make different operations, like software installation or file and system configuration.
  • Play: A section of a playbook where the host-task relationship is defined. Each play maps to a set of hosts and runs specific tasks to be performed on the hosts.
  • Handler: A handler is a task that only executes when notified by another task, using the notify directive. Typically, handlers are used to run tasks conditionally, e.g., to restart a service after a configuration change.
  • Variable: A variable is a kind of placeholder that can store values, which later may be referred to in a playbook, task, or template. Variables allow dynamic content that can be more flexible in Ansible scripts.
  • Template: A file of variables that Ansible fills up with real values during execution. Normally, templates are used with configuration files that are changeable among different environments or hosts.
  • Provisioning: This is simply the making and configuring of virtual machines, or any other components of infrastructure. It involves tasks such as operating systems' installations, network configuration setup, and application deployment.

Step-by-Step Process to Provisioning Virtual Machines with Ansible

Step 1: Launch EC2 Instance

  • Login to the AWS console with your credentials.
  • Navigate to EC2 Dashboard and launch EC2 instance.
Launch EC2 Instance

Step 2: Install Ansible

  • Now install ansible in our virtual machine.
sudo amazon-linux-extras install ansible2 -y
Install Ansible

Step 3: Create Inventory file or Host file.

Create Inventory file or Host file

Step 4: Create Variable file

  • Define your variables in a file named var.yml:

aws_access_key: "YOUR_AWS_ACCESS_KEY"

aws_secret_key: "YOUR_AWS_SECRET_KEY"

region: "us-east-1" #replace all with your own configurations

vpc_cidr_block: "10.0.0.0/16"

vpc_name: "my-vpc"

igw_name: "my-igw"

pubsubnet_cidr_block: "10.0.1.0/24"

pubsubnet_name: "public-subnet"

pvtzone: "us-east-1a"

pubroute_table_name: "public-route-table"

security_group_name: "my-security-group"

image_id: "ami-0abcdef1234567890"

type: "t2.micro"

Create Variable file

Step 5: Create Playbook

- hosts: localhost

become: yes

gather_facts: false

vars_files:

- var.yml

tasks:

# VPC creation

- name: Create VPC

ec2_vpc_net:

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

cidr_block: "{{ vpc_cidr_block }}"

name: "{{ vpc_name }}"

region: "{{ region }}"

dns_support: yes

dns_hostnames: yes

tenancy: default

state: present

register: vpc_result

Create Playbook

IGW creation

- name: Create Internet Gateway

ec2_vpc_igw:

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

vpc_id: "{{ vpc_result.vpc.id }}"

region: "{{ region }}"

state: present

tags:

Name: "{{ igw_name }}"

register: igw_result

 IGW creation

Public-subnet creation

- name: Create VPC Public Subnet

ec2_vpc_subnet:

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

vpc_id: "{{ vpc_result.vpc.id }}"

region: "{{ region }}"

az: "{{ pubzone }}"

state: present

cidr: "{{ pubsubnet_cidr_block }}"

map_public: yes

resource_tags:

Name: "{{ pubsubnet_name }}"

register: pubsubnet_result

Public-subnet creation

Public-Route table

- name: Create VPC Public Route Table

ec2_vpc_route_table:

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

vpc_id: "{{ vpc_result.vpc.id }}"

region: "{{ region }}"

state: present

tags:

Name: "{{ pubroute_table_name }}"

subnets:

- "{{ pubsubnet_result.subnet.id }}"

routes:

- dest: 0.0.0.0/0

gateway_id: "{{ igw_result.gateway_id }}"

register: public_route_table

Public-Route table

Security-group

- name: Create VPC Security Group

ec2_group:

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

vpc_id: "{{ vpc_result.vpc.id }}"

region: "{{ region }}"

state: present

name: "{{ security_group_name }}"

description: allow

tags:

Name: "{{ security_group_name }}"

rules:

- proto: all

cidr_ip: 0.0.0.0/0

rule_desc: allow all traffic

register: security_group_results

Security-group

EC2 Instance

- name: Launch EC2 Instance

ec2:

image: "{{ image_id }}"

instance_type: "{{ type }}"

region: "{{ region }}"

wait: yes

count: 1

state: present

vpc_subnet_id: "{{ pubsubnet_result.subnet.id }}"

assign_public_ip: yes

group_id: "{{ security_group_results.group_id }}"

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

instance_tags:

Name: "{{ instance_name }}"

EC2 Instance

Auto Scaling Group

- name: Create Auto Scaling Group

ec2_asg:

aws_access_key: "{{ aws_access_key }}"

aws_secret_key: "{{ aws_secret_key }}"

region: "{{ region }}"

name: my-auto-scaling-group

launch_config_name: my-launch-config

min_size: 1

max_size: 3

desired_capacity: 2

vpc_zone_identifier: "{{ pubsubnet_result.subnet.id }}"

tags:

- key: Name

value: my-instance

Auto Scaling Group

Step 6: Run playbook

 Run playbook

Step 7: Verify Virtual Machines

Verify Virtual Machines

Conclusion

Therefore, Ansible eases the process of creating playbooks that describe how to provision virtual machines, making the task easier for automating the process of building and configuring infrastructure. Use its simple, declarative language to define the desired state of the infrastructure in your playbooks and let the magic of Ansible work with the intricacies of execution.

In this documentation, we have gone through the base terminologies that are crucial for handling Ansible: playbooks, tasks, roles, and modules. Besides, a detailed explanation was given about how to provision VMs: the importance of proper inventory management, using variables, and the steps to customize templates.

The ability of Ansible to interact with many cloud providers and manage infrastructure as code makes it one of the prime tools for current-day DevOps practices. Idempotence is one of its attractive features, ensuring that tasks are only executed when necessary and result in a desired state without causing unintended changes.

Learn the ways to be efficient in deploying infrastructure and make your systems reliable with Ansible. Whether you are provisioning just a few virtual machines or managing a complex, multi-cloud environment, Ansible provides the ease, flexibility, and power to enable you to automate your workflows.

As you continue your journey into Ansible, remember to use its documentation and community resources. With practice and experience, you unlock the full power of Ansible; your infrastructure provisioning process becomes more effective and scalable.


Article Tags :

Similar Reads