Understanding Configuration Management with Ansible
What is Ansible?
Ansible is an open-source automation tool utilized to automate various IT processes, including resource provisioning and configuration. It was initially developed by Red Hat and is primarily written in Python. Ansible operates as a command-line software application.
Ansible works on a push-based mechanism, in which there is a master server that pushes the updates in all the connected worker nodes.
Installation of Ansible on AWS EC2:
- Set up the AWS EC2 Linux-based instance, which will serve as the Master Node.
Launch the instance and connect to it. Run the below command to install Ansible :
- Adding repository
sudo apt-add-repository ppa:ansible/ansible
- Updating and installing Ansible.
sudo apt update
sudo apt install ansible
- Check the Ansible version. which will make sure that you have installed Ansible in your system.
ansible --version
Editing the host file:
In the Ansible master server , there exists the host file of the Ansible, configuration file, which contains the information on the remote servers, which we want to manage.
The host file is kept at location /etc/ansible/hosts.
You need to update that host file, so open that file, by running the below command:
sudo vim /etc/ansible/hosts
Creating Group:
Groups are used to organize and categorize your inventory of hosts, making it easier to manage and run tasks on specific sets of hosts.
To create a group, open the host file and use the below syntax to create a group:
[group_nmae]
In the next line, you can write your hostname and provide the IP address of that host:
machine_name ansible_host=ip_address
For example:
Here, the tasks,
is the group name, day_55
is the hostname followed by its IP address.
Connecting the Master node with the other hosts:
Now we want to connect our master node to other servers, so that we can provision them, to connect them we will use the SSH protocol.
Steps to connect:
Step 1: Provide the Private Key
- Provide the private key that you used to create other worker machines via the SCP (Secure Copy Protocol). This key is typically used for SSH access to your remote servers.
Step 2: Locate Your Private Key
- On your local system, locate your private key file.
Step 3: Use SCP to Send the Key to the Master Server
- Open your terminal.
- Run the following command to securely transfer the private key from your local system to the master server:
scp -i "private_key_1.pem" private_key_2.pem ubuntu@ip_adrress_of_ec2_master.compute-1.amazonaws.com:path_where_you_want_to_copy
private_key_1.pem:
Used for connecting our EC2 master instance.private_key_2.pem:
Is the file/key that you want to copy in your instance.
Step 4: Update the Host File on the Master Server
- On your master server, open the host file.
- Create a variable that specifies the location of the private key file as an argument. This variable will be used to connect to remote machines. For example:
[group:vars]
ansible_ssh_private_key_file=/home/ubuntu/keys/key_name.pem
Step 5: Connect to Remote Machines
- On your local system, open your terminal.
- Use the following command to connect to a remote machine within the “tasks” group:
ansible tasks -m ping
This command attempts to ping all the servers defined in the “tasks” group using the specified private key, Python version, and username.
Testing:
In your master server run the ad-hoc command to learn about RAM used in the servers under tasks
group. You can do this by running the command :
ansible tasks -a "free -h"
Conclusion:
This brief demonstration highlights the tremendous power of Ansible. With Ansible, you can effortlessly manage your servers, whether local or remote, with remarkable ease and efficiency.