Ansible is fun, you saw in the last few days how easy it is.
Let's make it fun now, by using a video explanation for Ansible.
๐ถ 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
Log in to your AWS Management Console.
Navigate to the EC2 dashboard.
Launch a new EC2 instance.
Configure the instance with appropriate security groups and key pairs. Make sure you have SSH access to the instance.
Launch the instance.
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:
ansible-inventory: This is the command itself.
--list: This option tells Ansible to list the inventory. It will display the inventory in JSON format.
-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
Log in to your AWS Management Console.
Navigate to the EC2 service.
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.
Copy the private key to the master server where Ansible is set
Assuming you already have the private key (.pem file) on your local machine, use a tool like
scp
(secure copy) to copy the private key to your Ansible master server. Replaceyour-private-key.pem
andansible-master-ip
with your actual private key file name and Ansible master server's IP address:scp -i your-existing-key.pem your-private-key.pem ec2-user@ansible-master-ip:/path/to/destination/
This will copy the private key to the specified path on your Ansible master server.
Test Ansible Ping Command
On your Ansible master server, ensure Ansible is correctly installed.
Create an Ansible inventory file (e.g.,
sudo vim /etc/ansible/hosts
) and define the IP addresses of the two EC2 instances you want to manage. Example:
[servers]
server_1 ansible_host=<server-1 Public IP>
server_2 ansible_host=<server-2 Public IP>
server_3 ansible_host=<server-2 Public IP>
Replace with the actual IP addresses of your EC2 instances, and provide the correct path to the private key file for each host.
- Try a ping command using Ansible to the Nodes.
Test the Ansible ping command to check connectivity to the nodes:
ansible all -m ping
This command uses the -i
option to specify the inventory file and the -m
option to specify the Ansible module (ping) for testing connectivity. It will attempt to SSH into each node using the provided private key and user (ec2-user in this example) and report the results.
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 server_1:server_2:server_3 -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 specifiedserver_1
,server_2
, andserver_3
. Ansible will perform theping
operation on these specific hosts.-m ping
: This flag specifies the Ansible module to use, which is theping
module in this case. Theping
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
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 thecommand
module. Thecommand
module is used to run shell commands on remote hosts.-a uptime
: This flag specifies the argument to pass to thecommand
module. In this case, it's theuptime
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.
Happy Learning :)
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps โพ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #AWS