What's this 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-01
- Installation of Ansible on AWS EC2 (Master Node)
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install ansible -y
Task-02
- read more about Hosts file
sudo vim /etc/ansible/hosts
ansible-inventory --list -y
Task-03
Setup 2 more EC2 instances with same Private keys as the previous instance (Node)
- Once again, launch three instances named server-1, server-2, and server-3, using the same key.
Copy the private key to master server where Ansible is setup
To connect to the host file, you need the private key on your 'Ansible-Master'.
To connect using your private key, you need to create a folder where you will store the private key to connect to the other servers.
mkdir keys && cd keys
- Open your local machine, like your terminal, go to the 'Downloads' folder, find your 'ansible-key,' and then you'll want to transfer that key to your 'Ansible-Master' server using the SSH command.
ssh -i "ansibile-key.pem" ubuntu@ec2-3-82-116-205.compute-1.amazonaws.com
- To do this, copy the 'Ansible-Master' SSH command and paste it into your local machine's terminal. Then, modify the command: replace 'SSH' with 'scp,' specify your key name after '.pem,' and at the end of the command, paste the present working directory of your 'Ansible-Master' using colon :
scp -i "ansibile-key.pem" ansibile-key.pem ubuntu@ec2-3-82-116-205.compute-1.amazonaws.com:/home/ubuntu/keys
- If you run 'ls' on your 'Ansible-Master' server, you'll see your private key listed.
- Open the host file using the command provided and once you've read it, navigate to a specific section, for instance, 'ex:2', there you'll define your groups such as 'dev group' and 'prd group'. You can define them using square brackets '[]' and the corresponding variables like 'ansible_host=your server's IP address'.
sudo vim /etc/ansible/hosts
I think you now understand how to configure your servers in the Ansible host file. You can create and organize them into groups, similar to the image above. This enables you to list multiple servers using their respective IP addresses, and now you know how to set this up.
Now your private key's location in your 'Ansible-Master' server should be known to the Ansible variable. Navigate to your Ansible host file using the command and update it accordingly.
Next you have written your server's IP address and variables. At the bottom, write the private key file like this:
[all:vars]
,ansible_python_interpreter=/usr/bin/python3
,ansible_user=ubuntu, ansible_ssh_private_key_file=/<file>
, then come back to your host file.
Try a ping command using ansible to the Nodes.
- After that use this command to ping your hosts
chmod 400 /home/ubuntu/keys/ansible-key.pem
ansible servers -m ping
Happy Learning
Thanks For Reading! :)
-DevOpsParthu💝💥