A Comprehensive Ansible Tutorial for Beginners — Part 1

Praveen Dandu
5 min readJul 28


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.

Useful Links:
- Ansible Official Website: https://www.ansible.com/
- Ansible Documentation: https://docs.ansible.com/
- Ansible GitHub Repository: https://github.com/ansible/ansible

YAML Overview:

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.

Useful Links:
- YAML Official Website: https://yaml.org/
- YAML Syntax Cheat Sheet: https://learnxinyminutes.com/docs/yaml/

How to Access Your Working Files:

1. Create a new directory for your Ansible project:

mkdir ansible_project
cd ansible_project

2. Initialize a Git repository (optional but recommended):

git init

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:

vagrant init

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 up

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.

Installing Ansible:

  1. 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

For Mac:

brew install ansible

2. Verify the installation:

ansible --version

Manual Inventory:

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.

  1. 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:
vagrant ssh-config

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:

vagrant status

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= ansible_port=2222 ansible_user=vagrant ansible_ssh_private_key_file=/path/to/vagrant_ssh_key
centos_vm ansible_host= 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:

  1. Create your first playbook file (e.g., playbook.yml) in the Ansible project directory.
  2. Define the tasks you want to perform in the playbook using YAML syntax:
- name: Example playbook for Ubuntu
hosts: ubuntu_vm
become: true
- name: Update package cache and install Nginx on Ubuntu
name: nginx
state: present

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:

  1. Ansible uses a configuration file to set options and defaults. The default configuration file is located at /etc/ansible/ansible.cfg.
  2. You can create a local configuration file named ansible.cfg in your project directory to override specific settings.
  3. 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:

  1. To get help on the Ansible CLI, use the --help option followed by the command you want to learn more about.

For example, to get help on the ansible command:

ansible --help

Ad-Hoc Commands:

  1. Ad-Hoc commands are executed on the command line without writing a playbook.
  2. To run an ad-hoc command, use the ansible command 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!



Praveen Dandu

🚀 DevOps Engineer | Automating Infrastructure, Streamlining Deployments | Continuous Integration & Delivery Specialist https://www.linkedin.com/in/pravin24/