Introduction to Ansible:
Ansible is an open-source automation tool that simplifies the configuration management, application deployment, and orchestration of IT infrastructure. It allows you to manage multiple servers effortlessly, making system administration tasks more efficient and scalable.
Ansible uses YAML (YAML Ain’t Markup Language) to define playbooks and inventory files. YAML is a human-readable data serialization format that facilitates writing configuration files in a simple and easy-to-understand manner.
How to Access Your Working Files:
1. Create a new directory for your Ansible project:
2. Initialize a Git repository (optional but recommended):
3. Open your preferred code editor and start creating Ansible playbooks and configuration files in this directory.
Setting Up a Test Environment for Ansible with Vagrant:
we will set up a test environment using Vagrant, a tool for managing virtual machines, with virtual machines running Ubuntu and CentOS.
1. Install VirtualBox:
VirtualBox is a free and open-source virtualization platform that Vagrant uses to create and manage virtual machines.
Download the appropriate installer for your operating system from the VirtualBox website (https://www.virtualbox.org/wiki/Downloads) and follow the installation instructions for your OS.
2. Install Vagrant:
Vagrant is a command-line tool that simplifies the management of virtual machines. It works seamlessly with VirtualBox.
Download the Vagrant installer for your operating system from the Vagrant website (https://www.vagrantup.com/downloads) and follow the installation instructions for your OS.
Step 1: Initialize a New Vagrant Environment:
Initialize the Vagrant environment inside the directory:
Step 2: Configure the Vagrantfile:
The `Vagrantfile` is a configuration file that defines the characteristics of the virtual machines you want to create. We will configure it to create virtual machines running Ubuntu and CentOS.
1. Open the `Vagrantfile` in a text editor and replace its contents with the following configuration:
For the Ubuntu virtual machine:
Vagrant.configure("2") do |config|
config.vm.define "ubuntu_vm" do |ubuntu|
ubuntu.vm.box = "ubuntu/bionic64" # Ubuntu 18.04 LTS box
For the CentOS virtual machine:
Vagrant.configure("2") do |config|
config.vm.define "centos_vm" do |centos|
centos.vm.box = "centos/8" # CentOS 8 box
Step 3: Start and Provision the Virtual Machines:
1. Start both virtual machines by running the following command inside the `ansible_project` directory:
Vagrant will download the base boxes (if not already downloaded) and create the virtual machines based on the configurations in the `Vagrantfile`. This process might take a few minutes, depending on your internet connection speed.
2. Once the virtual machines are up and running, you can access them using SSH.
To log in to the Ubuntu VM, use:
vagrant ssh ubuntu_vm
To log in to the CentOS VM, use:
vagrant ssh centos_vm
Step 4: Verify the Test Environment:
Congratulations! You have successfully set up a test environment for Ansible using Vagrant with two virtual machines running Ubuntu and CentOS.
1. Inside the Ubuntu VM, you’ll have a clean Ubuntu 18.04 LTS system.
2. Inside the CentOS VM, you’ll have a fresh CentOS 8 system.
You can now use these virtual machines as test servers to practice Ansible playbooks and automation tasks.
- Install Ansible on your local machine using the package manager of your OS.
For example, on Ubuntu or Debian:
sudo apt update
sudo apt install ansible
For CentOS or RHEL:
sudo yum install epel-release sudo yum install ansible
brew install ansible
2. Verify the installation:
Since you are using Vagrant to manage your virtual machines, there could be some specific configurations required to make Ansible work seamlessly with Vagrant-managed VMs.
- Ensure the Vagrant SSH configuration is correct:
Vagrant usually manages SSH access to VMs using key-based authentication. Ensure that the Vagrant SSH keys are properly configured and accessible.
Check the Vagrant SSH configuration by running:
This will display the SSH configuration for each Vagrant-managed VM. Make sure the output looks correct, including the `IdentityFile` path.
2. Verify that Vagrant VMs are up and running:
Ensure that both Vagrant VMs (`ubuntu_vm` and `centos_vm`) are up and running.
You can check their status by running:
3. Test SSH connection to Vagrant VMs:
Attempt to SSH into each Vagrant VM manually to confirm SSH connectivity.
For example, to SSH into the `ubuntu_vm`:
vagrant ssh ubuntu_vm
4. Check the Vagrant inventory in Ansible playbook:
If you are using a Vagrant-specific inventory file, ensure that it is correctly referenced in your Ansible playbook (`playbook.yml`).
The inventory file may look something like this (e.g., `vagrant_inventory.yml`):
ubuntu_vm ansible_host=127.0.0.1 ansible_port=2222 ansible_user=vagrant ansible_ssh_private_key_file=/path/to/vagrant_ssh_key
centos_vm ansible_host=127.0.0.1 ansible_port=2200 ansible_user=vagrant ansible_ssh_private_key_file=/path/to/vagrant_ssh_key
Remember to replace `/path/to/vagrant_ssh_key` with the actual path to your Vagrant SSH private key(IdentifyFile).
Getting Started with Configuration:
- Create your first playbook file (e.g.,
playbook.yml) in the Ansible project directory.
- Define the tasks you want to perform in the playbook using YAML syntax:
- name: Example playbook for Ubuntu
- name: Update package cache and install Nginx on Ubuntu
3. Save the playbook file.
4. Try running the Ansible playbook with the Vagrant inventory:
Run the Ansible playbook specifying the Vagrant inventory file:
ansible-playbook -i vagrant_inventory.yml playbook.yml
Ansible Configuration File:
- Ansible uses a configuration file to set options and defaults. The default configuration file is located at
- You can create a local configuration file named
ansible.cfgin your project directory to override specific settings.
- Customize the configuration according to your needs. For example, you can set the remote user, the location of your inventory file, etc.
Where to Go for Help on the CLI:
- To get help on the Ansible CLI, use the
--helpoption followed by the command you want to learn more about.
For example, to get help on the
- Ad-Hoc commands are executed on the command line without writing a playbook.
- To run an ad-hoc command, use the
ansiblecommand followed by the target hosts and the module you want to use.
For example, to ping all the servers in the inventory:
ansible servers -m ping
In conclusion, we’ve covered a comprehensive introduction to Ansible, a powerful open-source automation tool that simplifies configuration management, application deployment, and orchestration of IT infrastructure. Throughout this tutorial, we explored the basics of YAML, essential for writing Ansible playbooks and inventory files.
We set up a test environment for Ansible using Vagrant, allowing us to work with virtual machines running Ubuntu and CentOS. Additionally, we learned how to install Ansible on our local machine and how to configure it to work seamlessly with Vagrant-managed VMs.
With this foundation in place, we took our first steps into Ansible’s world of automation by creating a simple playbook to install Nginx on an Ubuntu VM.
But this is just the beginning! In Part 2 of our Ansible tutorial, we will dive deeper into the power and flexibility of Ansible. We’ll explore more advanced playbooks, understand Ansible roles to organize our automation tasks, and delve into best practices for security and managing sensitive data.
Stay tuned for the upcoming Part 2, where we’ll continue our Ansible journey and unlock even more automation possibilities. Make sure to follow me for updates on the upcoming Ansible Course Part 2 and other valuable content to help you become an Ansible expert.
Happy automating, and see you in Part 2!