Understanding Configuration Management with Ansible

·

4 min read

What's this Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

Ansible works on the concept of control nodes and managed nodes where control nodes refer to the server where Ansible is installed and running and managed nodes are the ones to which the commands and instructions are sent.

Commonly used terms in Ansible:

  1. Ansible server- Server where Ansible is installed from which all the tasks and plays are run

  2. Tasks- It is a section which consists of a single procedure to be completed

  3. Modules- A command or Set of similar commands to be executed on the client's end

  4. Playbook- It consists of YAML format file codes with the tasks to be executed

  5. Inventory file-File which consists of all the information of client-server

  6. Facts-Information fetched from the client system from global variables with gather facts operation

  7. Hosts: Nodes which are automated by Ansible

Installation of Ansible on AWS EC2 (Master Node)

To start using Ansible, we need to install it on the master node, in our case EC2 instance

  1. Create t2.micro EC2 instance with Ubuntu server AMI

  2. Make sure to create a key pair and save it as this will be required in the future to connect to the client-server

  3. Now connect to the instance through SSH and run the following commands

  4. The below commands download all the required packages for Ansible

    sudo apt-add-repository ppa:ansible/ansible

  5. By running the sudo apt update command, our system understands that there are new packages in the system that need to be installed

  6. Now, sudo apt install ansible installs Ansible on our system

Now, to check if the ansible is installed, we need to check for inventory file in the path /etc/ansible/hosts

Since we are able open and read the inventory file, ansible is successfully installed and running

More about Hosts file

An Ansible inventory is a collection of managed hosts we want to manage with Ansible for various automation and configuration management tasks. Typically, when starting with Ansible, we define a static list of hosts known as the inventory. These hosts can be grouped into different categories, and then we can leverage various patterns to run our playbooks selectively against a subset of hosts.

For example, if we are working on a 3-tier application that consists of frontend, backend and frontend, then these servers can be grouped accordingly in the inventory as frontend, data and backend servers respecively

By default, the inventory is stored in /etc/ansible/hosts, but you can specify a different location with the -i flag or the ansible.cfg configuration file.

Setting up Ansible client

Now, let us set up 3 more instances that act as the client for our Ansible server.

To do so,

  1. Create 3 t2.micro ubuntu AMI, EC2 instances, and make sure you have selected the same key pair as the one we created for the ansible master node.

  2. Now, we shall add the IP addresses of these servers to ansible's inventory file thereby these servers become the client

    The command to open host file is sudo vim /etc/ansible/hostsOnce the file opens, add the IP addresses

  3. For the Ansible master to connect with the client we should provide the public key that we downloaded while creating the key pair.

    1. To do so, we shall make use of SCP.

      sudo scp -i "ansible_new_key.pem" ansible_new_key.pem ubuntu@ec2-34-222-55-178.us-west-2.compute.amazonaws.com:/home/ubuntu/.ssh can be used to transfer the key ansible_new_key.pem file from local to server with the same key ansible_new_key.pem

  1. We will let Ansible know that the key is added by adding the path to the key in the inventory file

  2. Since the private key file and the path to it both are added to the inventory, we shall try to ping these servers through the command ansible server -m ping

    We have successfully installed and connected the clients to Ansible!!