Day 41: Getting started with Ansible

Hi, I am an AWS-certified cloud engineer and I write about my progress and learnings of DevOps.
Hi everyone,
In the last article, we saw the comparison between IaC and configuration management. So today we are going to start understanding Ansible in-depth.
What’s this Ansible?
Ansible is an IT automation engine that can automate various IT needs. It has features like application deployment which means you can deploy your application easily as per your requirements, cloud provisioning, configuration management is also the main feature where you can configure and describe your automation job, and intra-service orchestration.
In this, (Yet Another Markup Language)YAML is used for configuring that helps for describing automation jobs as per requirement. It is Designed for multi-tier deployments, Ansible models the IT infrastructure by describing how various systems interrelate, instead of managing one system at a time.
Features :
In this, It uses no extra functionality and cost like no agents and no extra custom security infrastructure, hence it is easy to deploy.
It uses a very simple language called YAML (Yet Another Markup Language) in the form of Ansible Playbooks and you can configure it as per your requirement, it helps describe the automation jobs in a way that looks like basic English.
The Ansible Automation Engine has a direct interaction with the users who write playbooks and also interacts with cloud services and the Configuration Management Database (CMDB).
Ansible Architecture
The Ansible architecture is shown below in the diagram.

Inventories –
Ansible inventories are lists of hosts with their IP addresses, servers, and databases which have to be managed via SSH for UNIX, Linux, or Networking devices, and WinRM for Windows systems.APIs –
Application Programming Interface or APIs are used as a mode of transport for public and private cloud services.Modules –
Modules are executed directly on remote hosts through playbooks and can control resources like services, packages, and files, or execute system commands. They act on system files, install packages, and make API calls to the service network. There are over 450 Ansible that provide modules that automate various jobs in an environment. For example, Cloud Modules like Cloud Formation create or delete an AWS cloud formation stack.Plugins –
Plugins are pieces of code that augment Ansible’s core functionality and allow executing Ansible tasks as a job build step. Ansible ships with several handy plugins and one can also write it on their own. For example, Action plugins act as front-ends to modules and can execute tasks on the controller before calling the modules themselves.Networking –
Ansible uses a simple, powerful, and agent-less automation framework to automate network tasks. It uses a separate data model and spans different network hardware.Hosts –
Hosts refer to the nodes or systems (Linux, Windows, etc.) which are automated by Ansible.Playbooks –
Playbooks are simple files written in YAML format that describe the tasks to be executed by Ansible. Playbooks can declare configurations, orchestrate the steps of any manually ordered process, and can also launch various tasks.CMDB –
It stands for Configuration Management Database (CMDB). In this, it holds data to a collection of IT assets, and it is a repository or data warehouse where we will store this kind of data, It also defines the relationships between such assets.Cloud –
It is a network of remote servers hosted on the internet to store, manage, and process data instead of storing it on a local server.
Task 1) Installation of Ansible on AWS EC2 (Master Node)
Update your system packages:
sudo apt-get update
Install Ansible
sudo apt-get install ansible
ansible --version
Task 2) Read more about Hosts file
In Ansible, the term “host file” typically refers to the inventory file or inventory configuration file. An inventory file is used to define the target hosts or nodes that Ansible should manage and interact with during playbook execution. This file provides information about the hosts’ IP addresses, hostnames, connection details, and various host-specific variables.
Here’s a basic example of an INI-style inventory file:
[web_servers]
web1 ansible_host=192.168.1.10 ansible_user=your_username
web2 ansible_host=192.168.1.11 ansible_user=your_username
[database_servers]
db1 ansible_host=192.168.1.20 ansible_user=your_username
You can specify the inventory file when running Ansible commands or playbooks using the -i flag:
ansible-playbook -i inventory.ini your_playbook.yml
To view the playbooks run the below command
sudo nano /etc/ansible/hosts ansible-inventory — list -y
Task 3) Set up 2 more EC2 instances with the same Private keys as the previous instance (Node). Copy the private key to the master server where Ansible is set up. Try a ping command using Ansible to the Nodes.
Step 1) Generate the key on the master machine. Use below command
ssh-keygen

Copy generated public key. use the below command for that.
sudo cat ~/.ssh/id_rsa.pub
Once you get the public key, copy that and paste it into the slave machine’s “authorized_keys” file (Do this on both machines)
You can do this using the below command, it will open the authorized_keys file and you have to paste that key in it.
vi ~/.ssh/authorized_keys
By adding a public key from the master to the slave machine we have now configured keyless access. To verify you can try to access the slave machine and use the command as mentioned in the below format from the Master server.
ssh <IP Address of slave machine>

Configure slave, for that, create a host file on the master machine. Use the below command.
sudo vi /etc/ansible/hosts
Add our slave’s IP address here or you can choose any other location as well.


Add our slave’s IP address here
[webservers]
Server1 ansible_host=172.31.35.47
To check the nodes are connected, run
ansible -m ping all

Here, We have successfully configured the master-slave ansible connection.
Thanks for reading ;)





