Day 55: Understanding Configuration Management with Ansible
Welcome to Day 55 of our comprehensive 90-day journey into the realm of modern IT infrastructure management. In this installment, we delve into the world of Configuration Management, shedding light on its significance and unveiling the powerful tool that is Ansible.
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 Architecture
The Ansible architecture is shown below in the diagram.
The Ansible Orchestration engine is the center of the Ansible tool.
- It consists of an Inventory table, API, Plugins, and modules written to configure, manage, automate, and orchestrate the process.
- It can get the inputs from the Playbook software, public/private cloud, and configuration management databases to do the networking manage the hosts or the servers, operating systems, and manage security.
Benefits of Ansible in DevOps
- The feedback loop is accelerated at a faster rate
- The bugs are found sooner and not wait till the end
- Risk due to lack of sufficient knowledge is mitigated
- The deployments are reliable
- The IT infrastructure is coordinated
- The deployments are faster
- Need for automation
- Version control and configuration management
- Orchestration of the IT Infrastructure.
What Ansible Can Do?
- Configuration Management — The enterprise hardware and software information is recorded and updated in detail, thus maintaining the consistency of the product performance.
- Application Deployment — The applications can be managed in Ansible from Development to Production when you define and manage the applications using Ansible.
- Orchestration — To manage as a whole and how the configurations interact.
- Security and Compliance — Wide security policy can be deployed across the infrastructure when the policy is defined in Ansible
- Provisioning — Helps to automate and manage the process
Tasks
Task 1) Installation of Ansible on AWS EC2 (Master Node)
Step1 :Update your system packages:
sudo apt-get update
Step2: First Install Required packages to install Ansible.
sudo apt install software-properties-common
Step3: Add the ansible repository via PPA
sudo add-apt-repository --yes --update ppa:ansible/ansible
Step4: 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 below command
sudo nano /etc/ansible/hosts ansible-inventory — list -y
Task 3) Setup 2 more EC2 instances with same Private keys as the previous instance (Node). Copy the private key to master server where Ansible is setup. Try a ping command using ansible to the Nodes.
Step 1) Generate ssh-key on master machine. Use below command
ssh-keygen
Copy generated public key. use below command for that.
sudo cat ~/.ssh/id_rsa.pub
Once you get public key, copy that and paste it in the slave machine’s “authorized_keys” file (Do on both machines)
You can do this using below command, it will open authorized_keys file and you have paste that key in it.
vi ~/.ssh/authorized_keys
By adding public key from master to slave machine we have now configured key less access. To verify you can try to access slave machine and use command as mentioned in below format.
Configure slave, for that create host file on master machine. Use below command.
sudo vi /etc/ansible/hosts
Add our slave’s Ip address here
[webservers]
Server1 ansible_host=54.152.167.35
Server2 ansible_host=54.237.230.206
To check the nodes are connected, run
ansible -m ping all
Here, We have successfully configured master slave ansible connection.