Setting up Ansible
Hey Fellas, in this article, we will be installing Ansible and creating a simple task of automation using Ansible and showing you all the processes involved.
We need to create 3 Amazon EC2 instances, 1 will be the Master while the other 2 will be the targets.
After creating the Instances, we can name one of them Ansible_Master while the other two Ansible_Target1 and Ansible_Target2 to simulate.
Step 1: Install Ansible
With the following command, we can install ansible
sudo yum install ansible
Create the ec2-key.pem file and paste your key into this file
It should look something like this if you run the cat ec2-file.pem
Run chmod on the key file to ensure it is not accessible to anyone and only the root user can read the file.
chmod 400 ec2-file.pem
The image below shows how to use Ansible to call different servers all at the same time
Step 2: Create Inventory File
Use the nano program to create the inventory.txt file
nano inventory.txt
ansible-target-1 ansible_host=16.170.230.179 ansible_connection=ssh ansible_user=ec2-user
ansible-target-2 ansible_host=51.20.116.72 ansible_connection=ssh ansible_user=ec2-user
Escape and save the file. To run the file, we can use the following
ansible ansible-target-1 -m ping -i inventory.txt
We should get this error and i will explain in a sec why that is
In the inventory.txt file the mode of connection we used is SSH and we need the SSH key for the servers we are trying to connect to, that is why we are receiving this error resposne from the server.
We need to assign the SSH key we copied at the beginning when installing our instances to the ssh-agent using the command below
ssh-agent bash
cp ec2-key.pem ~/.ssh/
ssh-add ~/.ssh/ec2-key.pem
Running this command again
ansible ansible-target-1 -m ping -i inventory.txt
ansible ansible-target-2 -m ping -i inventory.txt
To ensure that we can connect to our servers (instances) at all times, we need to configure the ansible configuration file to allow us to do that without interruption.
If the ansible config file does not exist in the /etc/ansible/ansible.cfg
we can create it by running the following
ansible-config init --disabled -f ini > /tmp/ansible.cfg
The config file will be sent to the /tmp/ansible.cfg
(tmp folder) and then it can be moved to the main folder /etc/ansible
As a refresher, we can use
sudo cp /tmp/ansible.cfg /etc/ansible
to copy the ansible config file into the main directory where it can be read.
Let's now try the command once again for both targets
ansible ansible-target-1 -m ping -i inventory.txt
ansible ansible-target-2 -m ping -i inventory.txt
Assuming we are having a hundred or more targets, do we keep changing the targets number using the above command? NO
Here is a better way to ensure we can communicate to all the targets at once:
Let's add a tag-name servers
to the inventory.txt file we created earlier.
Your inventory.txt file should now look like this
Run this command to see the magic all targets can now be connected to at the same time with a single command
ansible servers -m ping -i inventory.txt
Top comments (0)