Ansible is fun, you saw in last few days how easy it is.
Let's make it fun now, by using a video explanation for Ansible.
Task-01
- Write a Blog explanation for the ansible video
This blog is a written explanation for the below video. we have already done some of the tasks in our earlier challenge.
Day55 :https://hashnode.com/post/clr5w3d5h000g0al5hh8ic6ag
Day56:https://hashnode.com/post/clr7af96w000108jn8bd494o5
Step 1: Creating an EC2 Instance
To begin, you need to create an EC2 instance on AWS. Make sure you have an AWS account set up and are logged into the AWS Management Console.
Step 2: Adding Ansible Repository
Once your EC2 instance is up and running, we will add the Ansible PPA repository to it. Use the following commands:
sudo apt-add-repository ppa:ansible/ansible
Step 3: Installing Ansible
Update the package list and install Ansible with the following commands:
sudo apt-get update
sudo apt-get install ansible
After the installation is complete, you can check the Ansible version to ensure it's correctly installed:
ansible --version
Step 4: Setting Up Multiple EC2 Instances
To make the most of Ansible, you'll want to manage multiple servers. Here's how you can set up two more EC2 instances with the same private key as the Ansible master instance:
Launch two new EC2 instances.
Copy the private key from the master server to where Ansible is set up.
Create a new file on the master server at
/home/ubuntu/.ssh
and paste the private key into it.
With these steps, you can SSH into the two new server instances from the master instance using the private key.
Step 5: Creating an Ansible Inventory
To manage your servers effectively, you need to create an Ansible inventory. This is a list of IP addresses that Ansible will work with.
Follow these steps to create your inventory file:
Create a new folder named "ansible."
Inside the folder, create a "hosts" file, which will serve as your inventory.
Add the IP addresses of the servers inside the "hosts" file.
You can verify the inventory of hosts using the ansible-inventory
command:
ansible-inventory --list -y -i /path/to/your/inventory-file
Step 6: Testing Connectivity with Ansible
Before you start automating tasks with Ansible, you should ensure that your Ansible master can communicate with the remote servers. If you encounter SSH connection issues, it's likely due to authentication problems.
You can resolve this by following these steps:
Check the permissions on the remote host's private key file.
Give permissions to the key file using the
chmod
command.Specify the private key file to use for authentication when running the Ansible command:
ansible -i /path/to/inventory_file all -m ping --private-key=/path/to/private_key
In the command above, replace /path/to/inventory_file
and /path/to/private_key
with your actual file paths.
Step 7: Running Ad Hoc Commands
With Ansible, you can run ad hoc commands to perform various tasks. Here are a couple of examples:
- To check free memory or memory usage of hosts:
ansible -i /path/to/inventory_file all -a "free -m"
- To check uptime:
ansible -i /path/to/inventory_file all -a uptime
In these commands, replace /path/to/inventory_file
with your inventory file's actual path.