Day 57 Task: Ansible Hands-on with video

Day 57 Task: Ansible Hands-on with video

ยท

7 min read

๐Ÿ”ถ Task: Write a Blog explanation for the Ansible video.

๐Ÿ”ถ Installation of Ansible on AWS EC2 (Master Node)

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

To install Ansible on an AWS EC2 instance and set it up as a master node, you can follow these steps:

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 dashboard.

  3. Launch a new EC2 instance.

  4. Configure the instance with appropriate security groups and key pairs. Make sure you have SSH access to the instance.

  5. Launch the instance.

  6. Connect to Your EC2 Instance

Step 2: Add Ansible PPA repository by using the below command

sudo apt-add-repository ppa:ansible/ansible

Step 3: Update the System

Update the package manager and upgrade the system packages to the latest versions:

sudo apt update -y

Step 4: Install Ansible

sudo apt install ansible -y

Step 5: Verify Ansible Installation

You can verify the installation by checking the Ansible version:

ansible --version

This should display the installed Ansible version.


๐Ÿ”ถ Read more about the Host file

sudo nano /etc/ansible/hosts

The /etc/ansible/hosts file, often referred to as the Ansible inventory file, is a critical component in Ansible. It defines the hosts or remote servers that Ansible will manage and allows you to group them into categories. This inventory file provides the necessary information for Ansible to connect to these remote hosts, such as their IP addresses or DNS names and the SSH or other connection parameters.

ansible-inventory --list -y

The ansible-inventory command is used in Ansible to display the current inventory. When you run ansible-inventory --list -y, it will show the inventory in YAML format. The inventory is typically defined in the /etc/ansible/hosts file, but it can also be dynamically generated using scripts or other sources.

Here's what the ansible-inventory --list -y command does:

  1. ansible-inventory: This is the command itself.

  2. --list: This option tells Ansible to list the inventory. It will display the inventory in JSON format.

  3. -y: This option tells Ansible to output the inventory data in YAML format. If you omit this option, the output will be in JSON format.

When you run this command, Ansible will read the inventory file and any other dynamic inventory sources (if configured) and then display the complete inventory information in either YAML format. This information includes all defined groups, hosts, host variables, and group variables.

This command is useful for debugging and verifying your inventory setup when working with Ansible. It helps you confirm that Ansible can correctly detect and interpret your inventory sources.


๐Ÿ”ถ Setup 3 more EC2 instances with the same Private keys as the previous instance (Node)

To set up two 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, and then try a ping command using Ansible to the nodes, you can follow these steps:

Launch Two New EC2 Instances

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 service.

  3. Launch three new EC2 instances with the same private key as the previous instance (Node). You can do this by selecting the same key pair during the instance launch process.

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

    We have created the two servers and set up the server to master the above task.

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

    1. Create a public key on the master server and copy the key using the ssh-keygen command.

        ssh-keygen
      

    2. We can see id_rsa.pub which is the public key of the master server.

    3. Copy the above public key of the master to both the node servers.

  • Try a ping command using Ansible to the Nodes.

    1. Now, use the ping command with the input of inventory file to it so that it will ping both the node servers.

        ansible all -m ping -u ubuntu
      

    2. We can see both pings are successful which indicates servers are in active states.

If the setup is correct and there are no connectivity issues, you should see successful ping responses from the nodes.

That's it! We have now set up two additional EC2 instances, copied the private key to the Ansible master, and tested Ansible's connectivity to the nodes.


Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact Swiss army knife when you want to do a quick task across multiple machines.

To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.

Ansible ad hoc commands come in handy when you want to perform a quick task.

๐Ÿ”ถ Write an ansible ad hoc ping command to ping 3 servers from the inventory file

ansible -i  /path/to/inventory/file server1:server2:server3 -m ping
or 
ansible -i /etc/ansible/hosts server1:server2:server3 -m ping

The Ansible command is used to perform a ping operation on specific hosts from your inventory file. Here's a breakdown of the command:

  • -i /path/to/inventory/file: This flag specifies the path to the Ansible inventory file. The inventory file lists the hosts or nodes that Ansible will manage.

  • server_1:server_2:server_3: These are the host patterns or names you want to target with the Ansible command. In this case, you've specified server_1, server_2, and server_3. Ansible will perform the ping operation on these specific hosts.

  • -m ping: This flag specifies the Ansible module to use, which is the ping module in this case. The ping module is used to check if hosts are responsive and reachable.

So, when you run this Ansible command, it will ping the hosts server_1, server_2, and server_3 to check if they are reachable and responsive. You should see an output indicating whether each host was reachable (SUCCESS) or not (UNREACHABLE) based on the results of the ping operation.

Please ensure that your inventory file (/path/to/inventory/file) is correctly configured with the hostnames or IP addresses of server_1, server_2, and server_3, along with the necessary SSH connection details if required for accessing these hosts.


๐Ÿ”ถ Write an ansible ad hoc command to check uptime.

ansible -i /path/to/inventory/file all -m command -a uptime
or 
ansible -i /etc/ansible/hosts all -m command -a uptime

The command is an Ansible command that performs the uptime command on all hosts defined in the inventory file located at /path/to/inventory/file. Here's a breakdown of the command:

  • -i /path/to/inventory/file: This flag specifies the path to the Ansible inventory file. The inventory file lists the hosts or nodes that Ansible will manage. In your case, you've provided the path to this file.

  • all: This is an Ansible pattern that refers to all hosts defined in the inventory file. It means that Ansible will execute the following command on all hosts.

  • -m command: This flag specifies the Ansible module to use, which in this case is the command module. The command module is used to run shell commands on remote hosts.

  • -a uptime: This flag specifies the argument to pass to the command module. In this case, it's the uptime command, which is a standard Unix/Linux command that shows the current system uptime.

So, when you run this Ansible command, Ansible will connect to all hosts listed in the inventory file and execute the uptime command on each of them. It will then display the output of the uptime command for each host, showing how long each host has been running.

That's it! We have now set up two additional EC2 instances, copied the private key to the Ansible master, and tested Ansible's connectivity to the nodes.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS #Ansible

ย