In this blog let us understand how to install Ansible, how to create an inventory file and how to run a playbook.
Requirement: An AWS account and basics of EC2.
Step 1: Create three EC2 Instances.
Create three EC2 instances with the same key-pair and security group.
Rename them as Ansible-ControlNode, Ansible-ManagedNode1 and Ansible-ManagedNode2.
Step 2: Download and install Git.
On your local machine download and install Git using below link so that we can use Git Bash:
You can use any CLI such as Command Prompt, WSL (Windows Subsystem for Linux), VS Code Terminal, etc as alternatives for Git Bash.
Step 3: Connect to each instance using Git Bash.
Navigate to the folder where you stored the .pem file locally.
Right click and select “Open Git Bash here”.
You will get the below screen in a new Git Bash window.
Go to AWS Management Console, search for the service EC2 and click on Instances.
Select the 1st instance Ansible-ControlNode and click on Connect to connect to it.
Make sure that the SSH client is selected and copy the ssh command displayed at the bottom.
Go to Git Bash, right click and paste the copied ssh command. Then hit Enter.
Then type yes and hit Enter if it prompts the confirmation about the connection.
There we go!!…..
We are successfully connected to the 1st instance. Its private IP is 172.31.45.125.
Open two more separate Git Bash windows and repeat these connection steps for another two instances (Managed Nodes) as well. Their private IPs are 172.31.33.145 and 172.31.41.176.
Open the .pem file locally in the notepad and copy the content.
Create a .pem file in all three instances with the same file name and paste the same content.
Now you will see the same content in the file .ssh/authorized_keys in all three instances.
Step 4: Install Ansible on 1st instance Ansible-ControlNode.
_sudo apt update
sudo apt install ansible
ansible –-version_
Step 5: Create an inventory file.
In the 1st instance Ansible-ControlNode, create a directory ansible_quickstart and navigate to it.
Also in the same directory where you have created a .pem file in the 1st instance, create a .ini file for inventory by adding the Public IPs of 2nd and 3rd instances to a group myhosts.
NOTE: Whenever we restart the EC2 instances we get new Public IPs. So in the below screenshots the Public IPs may vary from one screenshot to another.
Step 6: Verify your inventory.
Make sure that the output of the following command has listed the Public IPs that we have added in the previous step.
ansible-inventory -i inventory.ini - list
Step 7: Ping the group myhosts in your inventory.
Ensure the permission of the .pem file is readable only by the owner in all three instances. We can change it to 644 by using the chmod command. But since I am the owner of the .pem file I set the permission to 600 itself.
For 1st instance:
chmod 600 /home/ubuntu/ansible_quickstart/sandy-devops-stuffs-mumbai.pem
For 2nd and 3rd instances:
chmod 600 /home/ubuntu/sandy-devops-stuffs-mumbai.pem
Also I modified the inventory file as follows to ensure my inventory.ini and Ansible configuration are correctly set up to use the SSH key.
_[myhosts]
13.233.123.32 ansible_user=ubuntu ansible_ssh_private_key_file=/home/ubuntu/ansible_quickstart/sandy-devops-stuffs-mumbai.pem
43.205.239.162 ansible_user=ubuntu ansible_ssh_private_key_file=/home/ubuntu/ansible_quickstart/sandy-devops-stuffs-mumbai.pem_
Now run the ansible ping command in the 1st instance.
ansible myhosts -m ping -i inventory.ini
OR
ansible -m ping myhosts -i inventory.ini
OR
ansible -m ping -i inventory.ini myhosts
OR
There we go!!…..
The control node is successfully connected to the managed nodes and now the control node can manage the managed nodes.
NOTE: Pass the -u option with the ansible ping command if the username is different on the control node and the managed node(s).
NOTE: It is easy and straightforward to write an inventory file in .ini format. But if the number of managed nodes increases then it is a best practice to write an inventory file in .yaml format as shown below which is equivalent to the file inventory.ini which we have already created.
Step 8: Create and run the playbook to ping the hosts.
Now create a file playbook.yaml with following content in the directory ansible_quickstart on 1st instance:
NOTE: ansible.builtin.ping: and ansible.builtin.debug: in the above playbook are collections and modules of Ansible.
Ansible.builtin is one of the Ansible collections.
ping and debug are modules in the collection ansible.builtin.
ping module - Try to connect to the host, verify a usable python and return pong on success.
debug module - Print statements during execution.
To know more about all other collections & modules and to understand what they do please refer to the following links.
Run the following command on 1st instance:
ansible-playbook playbook.yaml -i inventory.ini
There we go!!…..
We have successfully run a simple Ansible playbook to ping to the hosts listed in the inventory file.
In this way we can write other playbooks and run them on the control node to deploy and configure the applications on all the managed nodes at a time instead of doing it on each node manually.
Credit: Ansible official document Introduction to Ansible
Source link
lol