Ansible is an open-source automation tool that allows you to automate the provisioning, configuration management, and application deployment of multiple systems simultaneously.
It uses a declarative language called YAML to describe the desired state of your infrastructure, and it can manage both Linux and Windows-based systems.
Ansible is a suite of software tools that enables infrastructure as code and is acquired by Red Hat (now part of IBM) in 2015.
So what's the use of Ansible and why DevOps Engineers are using it?
Considering a small example in an analogy of an event manager in an organization:
Supposing there is an event manager in an XYZ organization that person has the responsibility to manage all the stuff around in the arena and fulfill the demand of the event.
So what's the CTA (Call to Action) of that event manager?
Doubtless, the event manager will be managing the space for the event, preparing guest lists, sending invitations, managing registrations, and sending reminders to attendees.
Woohoo, The event went successful. ๐
More people are approaching him. His schedule is getting tightened with managing all the stuff for the event. At some point, the manager got exhausted because of the repetitive tasks. So he brought Ansible into the picture.
Wait!? How a DevOps tool is now managing an event? ๐ค
See, Ansible will not manage any event nor it is an event management tool. Ansible can help you manage the repetitive part of the event management efficiently. You can create playbooks that automate tasks like sending invitations, setting up event venues, managing registrations, and sending reminders to attendees. Ansible ensures that everything runs smoothly, saving time and reducing manual effort.
So in a similar manner, it simplifies the repetitive tasks and makes it easier to manage and control computers, networks, websites, and other systems. It helps you save time, reduce manual errors, and perform tasks efficiently. Whether it's managing computers in a lab, setting up networks, deploying websites, automating tasks, or working with cloud services.
Now let's set up Ansible and its configuration
Part A: Setting up AWS Instances
So, this is how we're trying to work around the AWS Instances to configure the nodes from the Master.
So here we are creating an EC2 instance to communicate and configure the system and implement an IAC, Infrastructure as a Code.
You can create an instance from the AWS console page.
So of now, we are done with creating 4 instances of AWS EC2 and looking to console them by connecting to them.
The picture above shows the instances which are running and it makes sense according to the plan to work around the instances. Please refer to the AWS Overview picture.
NOTE: Do not forget to download your .pem file, which will be needed later on.
Part B: Setting up Ansible in Master Node
The plan is very simple, the master is the node that serves as the ansible-server and typically manages the infrastructure through there to every node using SSH.
So your mind should be creating an image like this:
Till here we have created a mind map that depicts the mechanism and the flow of working with Ansible.
Installing Ansible in Master Node:
Since we need the master to manage the control, the master node will be treated as the Ansible server.
So let's install it.
Since we've created the Ubuntu instances so we will be following the docs of Ansible saying about installation in Ubuntu.
P.S. -> Please refer to the docs to follow the command line instructions. The repository may change in the future so might the instructions.
Now you're done with the installation of Ansible on your server. Now let us check out some important files that are required by Ansible to communicate.
cat /etc/ansible/ansible.cfg
cat /etc/ansible/hosts
There are some possibilities that these files might not be there, so do not worry, just go ahead and create one. ;<
Till here, Ansible is there on the Master Node but doesn't know whom to contact or to whom to push the updates.
So why not give all the information....information......information as in the form of IP in the host file which is an inventory to store all those IP addresses.
So open the hosts' file using any text editor, I am using Vim, you may use Nano.
vi /etc/ansible/hosts
And here I am listing all the node and their IPs. Let all the nodes install python3 using the variable being sent to them.
Now let us check whether we've got the host info or not. So try the following:
ansible-inventory --list -y -i /etc/ansible/hosts
Let's check whether our efforts are going well or not. Let's try to connect with the given nodes.
ansible all -m ping -i /etc/ansible/hosts
Gosshhh!! See, we are not able to connect the nodes. Can you understand why?
Give it a try for a moment.
Nope? See, We are not passing an ssh key which will authenticate the system. So lets with solving the same.
Just try to memorize, you might have gotten a .pem file while setting up your AWS instance. Please open up that locally downloaded file in your system using any text editor and copy all the content from it.
So we have to configure an ssh key into our master node, so open up the following:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/ansible_key
ansible all -m ping -i /etc/ansible/hosts --private-key=/home/ubuntu/.ssh/ansible_key
Hurray! This is huge! Congratulations you just set up the ansible.
Got Pong as a result of Ping.
Why not install some packages on those Nodes? Indeed, that's the major work of Ansible.
Part C: Installing packages on nodes using Ansible to nodes using playbooks.
We will try to install Docker on our node servers from Master i.e. Ansible Server.
Just create a directory name playbooks and a YAML file named install_docker.yml to run it as a playbook and save it.
---
- hosts: all
become: true
vars:
container_count: 4
default_container_name: docker
default_container_image: ubuntu
default_container_command: sleep 1d
tasks:
- name: Install aptitude
apt:
name: aptitude
state: latest
update_cache: true
- name: Install required system packages
apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- python3-pip
- virtualenv
- python3-setuptools
state: latest
update_cache: true
- name: Add Docker GPG apt Key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker Repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Update apt and install docker-ce
apt:
name: docker-ce
state: latest
update_cache: true
- name: Install Docker Module for Python
pip:
name: docker
- name: Pull default Docker image
community.docker.docker_image:
name: "{{ default_container_image }}"
source: pull
- name: Create default containers
community.docker.docker_container:
name: "{{ default_container_name }}{{ item }}"
image: "{{ default_container_image }}"
command: "{{ default_container_command }}"
state: present
with_sequence: count={{ container_count }}
After saving the file, just run the file using the following command in that directory itself.
ansible-playbook install_docker.yml -i /etc/ansible/hosts --private-key=/home/ubuntu/.ssh/ansible_key
Here you can see the docker is getting installed on the nodes.
So you have successfully installed Docker into the node from the master ie. Ansible server.
Part D: Installing packages on nodes using Ansible to nodes using ad-hoc.
There are times when packages don't need any playbook to be used for installation.
A single command is enough from the master node.
Let's suppose, we need to install git on every other node, so we can use ad-hoc.
They are just Linux command which comes into action for modifying the packages.
ansible all -a "sudo apt install git -y" -i /etc/ansible/hosts --private-key=/home/ubuntu/.ssh/ansible_key
From the following command, you can install git in a single command.
NOTE: There is a method for installing packages using a module. There you just have to install using the module.
Give it a try to all the modules available for Ansible: https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
Resources to learn Ansible:
Ansible Docs: https://docs.ansible.com/
Basic Tutorials: https://www.redhat.com/en/topics/automation/learning-ansible-tutorial
Configuration management: https://cloudacademy.com/course/what-is-configuration-management/general-concepts-1/