Day 55 of #90DaysOfDevOps
What is 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 update
sudo apt install ansible
Task-02
- read more about "Hosts" file
sudo nano /etc/ansible/hosts ansible-inventory --list -y
Task-03
Setup 2 more EC2 instances with same Private keys as the previous instance (Node)
Copy the private key to master server where Ansible is setup
Try a ping command using ansible to the Nodes.
Task - 1:
Launch an EC2 instance with Ubuntu OS, t2.micro (Free tier). Create a new key pair for the instance.
After launching the instance, connect it with SSH or with AWS Instance Connect.
I will connect it with SSH.
Enter command sudo apt-add-repository ppa:ansible/ansible
to add ansible python repository.
Next, update the system and then enter the command to install the Ansible into the system.
sudo apt update
sudo apt install ansible
To verify installation, run this command: cat /etc/ansible/hosts
If you get a response like this, then you have installed Ansible successfully.
Task - 2
The "hosts" file is a default host file of Ansible which is used to mention the host server IP address. The path of the file is /etc/ansible/hosts
To edit the file, enter the command: sudo nano /etc/ansible/hosts
Here are some examples to write:
Ex 1: Ungrouped hosts, specify before any group headers:
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
Ex 2: A collection of hosts belonging to the 'webservers' group:
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
If you have multiple hosts following a pattern, you can specify them like this:
www[001:006].example.com
You can also use ranges for multiple hosts:
db-[99:101]-node.example.com
Ex 3: A collection of database servers in the 'dbservers' group:
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
Ex4: Multiple hosts arranged into groups such as 'Debian' and 'openSUSE':
[Debian]
alpha.example.org
beta.example.org
[openSUSE]
green.example.com
blue.example.com
After you have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command with the --list and -y options:
ansible-inventory --list -y
This command will display a YAML-formatted list of hosts and their attributes, including the hostnames, IP addresses, and any other defined variables or group memberships.
Task - 3
Launch 2 more instance with the same settings and same private key.
Copy the private key to master server where Ansible is setup.
"Try a ping command using ansible to the Nodes."
Before trying to ping we need to configure the hosts file.
First, add IP addresses.
Copy the IP addresses of both the instances and past under the group name of servers.
Second, add the variables.
Save the file.
Change the access permission of the private key.
sudo chmod 600 /home/ubuntu/.ssh/ansible-master.pem
Now ping the servers.
The pong in the reply indicates the success of the connection.
Thank you!