Day 55 - Understanding Configuration Management with Ansible

Day 55 - Understanding Configuration Management with Ansible

Day 55 of 90daysofdevops

What's Ansible?

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

Task 1

Installation of Ansible on AWS EC2 (Master Node)

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible

  • Launch an instance and use SSH to connect to the EC2.

  • To add the Ansible PPA (Personal Package Archive) repository, you can execute the command below:

      sudo apt-add-repository ppa:ansible/ansible
    

  • Update the package.

  • Install Ansible using the following command:

      sudo apt install ansible
    

  • Verify the installation.

Task 2

Read more about Hosts file

In Ansible, the "hosts" file is a configuration file that defines the inventory of target hosts or servers that Ansible can manage. It is a text file that lists the hostnames or IP addresses of the target systems on which Ansible will perform tasks or execute playbooks. It allows you to organize and group hosts into different categories, such as development, production, or specific functional roles like web servers or database servers.

The hosts file is located at /etc/ansible/hosts by default, but you can specify a different path using the -i option when executing Ansible commands or playbooks.

To edit the hosts file, we can use any text editor sudo nano /etc/ansible/hosts

The hosts file typically has the following structure:

[group_name]
hostname1
hostname2
hostname3

[another_group_name]
hostname4
hostname5

In this example, "group_name" and "another_group_name" represent groups of hosts, and the hostnames listed underneath each group are the individual target systems. You can define multiple groups and include hosts in different groups based on your infrastructure setup.

After we have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command with the --list and -y options:

ansible-inventory --list -y

This command will display a YAML-formatted list of hosts and their attributes, including the hostnames, IP addresses, and any other defined variables or group memberships.

Task 3

Setup 2 more EC2 instances with the same Private keys as the previous instance (Node)

  • Launched two more instances with the same private keys.

Copy the private key to the master server where Ansible is set up

  • Go to the .ssh folder and copy path.

  • Run the below command to copy the ansible key file on the server followed by the copied path

      sudo scp -i "ansible-key.pem" ansible-key.pem ubuntu@ec2-3-234-240-111.compute-1.amazonaws.com:/home/ubuntu/.ssh
    

  • Check the folder again and see if the key file is there. Now change the permission of key file

  • Edit the hosts file by using sudo vim /etc/ansible/hosts command.

  • Once you have added the host information to the file, you can check the list of hosts that Ansible can work with by using the "ansible-inventory --list -y" command.

Try a ping command using Ansible to the Nodes.

  • Ping the nodes by using ansible servers -m ping command.

  • You can also use all instead of servers.

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #iac #ansible