Updated: Aug 9, 2020
This is the second part of the series of articles on dynamic inventory in ansible In the last part we had just launched 3 ec2 instances in AWS and now we will move forward.
Now let us login to ansible1 with the default user i.e. ec2-user
Copy the contents of kops.pem ( the pem file that we used while creating ec2-instances) to /home/ec2-user/.ssh/id_rsa
Give appropriate permissions to id_rsa
chmod 400 id_rsa
Now do the same on other two ec2 instances as well ( ansible2 and ansible3). This is done to enable pass-wordless authentication between ansible1 and other 2 servers.
Install ansible rpm on ansible1 ec2 instance.
sudo amazon-linux-extras install ansible2 -y
Configure aws cli credentials .
[ec2-user@ip-172-31-15-28 ~]$ aws configure AWS Access Key ID [None]: XXXXXXXXXXX AWS Secret Access Key [None]: XXXXXXXXXXXXXX Default region name [None]: ap-south-1 Default output format [None]: json [ec2-user@ip-172-31-15-28 ~]$
Download ec2.py and ec2.ini files on the path /etc/ansible/ and make them executable.
[ec2-user@ip-172-31-15-28 ansible]$ chmod +x ec2.py [ec2-user@ip-172-31-15-28 ansible]$ pwd /etc/ansible [ec2-user@ip-172-31-15-28 ansible]$ ls -lrt total 108 drwxr-xr-x 2 root root 6 May 21 17:00 roles -rw-r--r-- 1 root root 1016 May 21 17:00 hosts -rw-r--r-- 1 root root 19985 May 21 17:00 ansible.cfg -rwxrwxr-x 1 ec2-user ec2-user 73130 Jul 23 04:34 ec2.py -rw-rw-r-- 1 ec2-user ec2-user 9529 Jul 23 04:36 ec2.ini
Install pip and boto3 ( boto 3 is a python module required for dynamic inventory script to work)
sudo yum install python-pip -y sudo pip install boto
Now test the script
Let us test the settings by running a simple ping module
ansible -i ec2.py ap-south-1 -m ping
Alright people we have successfully configured dynamic inventory in our lab setup.