Getting Started with Ansible and Dynamic Amazon EC2 Inventory
Hi, I am writing a simple blog on how to use Ansible to interact with AWS EC2 instanaces using the dynamic libraries. Here I am demonstarting a simple ping command to ping your EC2 instances in AWS using the dynamic inventories.
Step by step procedure:
I am assuming that, you have already setup Ansible in your EC2 infrastructure. If not yet done, I am strongly recommend you to get it done with the help of my blog here: AWS EC2 step-by-step Automation by Ansible
1. Download the following files 3 files into your ansible working directory. /etc/ansible is my ansible working directory
2. Make the following changes. We are actually going to use the dynamic inventory as a default ansible inventory. So, you need to edit the ansible.cfg file present in /etc/ansible directory.
– Search for inventory parameter in the ansible.cfg. Change the inventory parameter value as shown below:
3. If you run the ping command straightaway, you will see lot of permission errors due to the private key used in your EC2 instance is not authorized to execute a Ping command in your AWS infrastructure. So, you have to give the SSH permission to your key file as below.
I assume that, your key file ansible.pem is already saved to the directory ~/.ssh in your ec2 instance from which you are running ansible
Now you are ready to start connecting your ec2 instances with a ping command as below:
ansible all -m ping
Also, verify the host names updated in the file vi ~/.ssh/known_hosts
Good Luck !