Provisioning Virtual Machines with Ansible: A How-To Guide
Last Updated :
01 Aug, 2024
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.
Step 2: Install Ansible
- Now install ansible in our virtual machine.
sudo amazon-linux-extras install ansible2 -y
Step 3: 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"
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
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
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-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
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
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 }}"
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
Step 6: Run playbook
Step 7: 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.
Similar Reads
Virtual Machines in Microsoft Azure Before we move on with how to deploy and use virtual machines in Microsoft Azure let's know a few things. A Virtual Machine (VM) is like any physical laptop, smartphone, or server that we use in our day-to-day life. It has features like CPU, memory, storage size, and different networking ports with
4 min read
How to Create Virtual Machines with VirtualBox in Linux? Quick Preview - Create Virtual Machines with VirtualBox on Linux:Configuring Virtual Machine ISO File:Open the VirtualBox Application & click New.Upload the ISO file of the OS.Click Next to move ahead.Increase Base Memory at least by 3000 MB.Increase Virtual Disk Size by at least 30 GB.Click Fin
5 min read
Managing Files with Ansible Lineinfile Module Ansible can be used to deploy software on multiple servers without human intervention. Ansible is also capable of configuring servers and creating user accounts. Ansible is agent-less software, which means there is no need to install the software on the nodes, and you are required to connect the nod
4 min read
Ansible vs Chef: Which one to Choose in 2025 In today's world, managing computer systems and IT infrastructure is very important for any coder. There are two popular tools for managing IT infrastructure. They are Ansible and Chef, But when it comes to choosing the right one for your project and needs. It can be confusing. So, let's break down
8 min read
How to Install Helm using Ansible Playbook? Managing various infrastructure and applications under different conditions in the present dynamic IT landscape can challenge. Automation tools like Ansible assist with smoothing out these undertakings by providing a basic yet amazing method to automate configuration management, application deployme
8 min read
How To Install Docker Using Ansible Playbook ? Docker is a software platform that allows you to build, test and deploy applications that use OS-Level virtualization to deliver software in packages called "containers". Containers - Docker package software into standardized units called "Containers". Docker is a stand-alone tool. which means no ne
7 min read