Understanding Configuration Management with Ansible

Understanding Configuration Management with Ansible

Ansible work via ssh! No Master Server! No Agent is Required!

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

Installation of Ansible on AWS EC2 (Say On Master Server )

Firstly, we have to create a new key pair when launching the Ansible-Master instance and select it when you launch the instance.

Now, I will be simply adding the Ansible installation command in User Data when launching the instance.

Whatever we have learned so far let's try to use it and make our task more easy.

#!/bin/bash
sudo apt update -y
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt update -y
sudo apt-get -y install ansible

Thus, For the Master EC2 Instance, Ansible has been installed, see Version details.

Successfully installed Ansible on your EC2 Instance!

Hosts file or inventory file in Ansible

In Ansible, the hosts file (often referred to as the "inventory" file) is a foundational component. It is where you define which machines you want to manage and can also specify details about those machines. This file is used by Ansible to determine which machines it should communicate with to execute playbooks or run ad-hoc commands.

Ansible Agentless Connect from One to Many

Launch 3 instances using the same key pair as the Ansible-Master server

Now we have a total 4 instances, 1 master and 3 Slave/Targets/Nodes

Let's Login to Ansible-Master server and try to connect it to the remaining servers.

Check the Ansible version and create a folder where we are keeping ansible private key to interact with the target server.

Once you copy the file; give permission as read and write access (600 access) to ansible-key.pem file

ansible --version
mkdir keys
cd keys
touch ansible-key.pem
vim ansible-key.pem #Open ansible-key.pem file and Copy its content from local to paste
cat ansible-key.pem #verify copy has done or not
chmod 600 ansible-key.pem

Now, configure host file as below.

Create inventory file at location /etc/ansible/hosts which is by default location of file. Ansible hosts file is a configuration file that contains a list of hosts or servers.

Once the file is open, you can add the IP addresses of the servers and also add a private key file location to use for authentication.

[servers]
server1 ansible_host=34.239.182.161 #Public IP Address Of Target Server
server2 ansible_host=54.163.90.97
server3 ansible_host=54.198.118.172

[all:vars]
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3

What Python interpreter does Ansible use?

By default, Ansible assumes it can find a /usr/bin/python on your remote system that is either Python2, version 2.6 or higher or Python3, 3.5 or higher. Setting the inventory variable ansible_python_interpreter on any host will tell Ansible to auto-replace the Python interpreter with that value instead.

Inventory

After you have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command.

ansible-inventory --list -y

Ping

Once the host file is configured, we will try to see if the connection has been established or not using Ping Command

ansible all -m ping
#OR
ansible all -m ping -u ubuntu #as we have mentioned in the host file to use ubuntu user

Dance GIFs | Tenor

This is how you can connect to your target machine without even logging in to it!

Ansible automates everything across your IT infrastructure and makes it look simple. We learned how to install Ansible on Ubuntu and also saw how to connect to remote servers using SSH key-based authentication, Ran some simple Ansible commands to see the connection to the servers.

Thanks for reading! ๐Ÿ˜ƒ๐Ÿ™

ย