Skip to content

Instantly share code, notes, and snippets.

@rgodishela
Created April 23, 2017 16:06
Show Gist options
  • Save rgodishela/318b60d36fa9bb36ef932784436d053f to your computer and use it in GitHub Desktop.
Save rgodishela/318b60d36fa9bb36ef932784436d053f to your computer and use it in GitHub Desktop.
AWS Dynamic inventory creation using Ansible.
AWS Dynamic inventory creation using Ansible.
— Download Boto
sudo pip install boto
— Find the Access ID and Security Key from your aws account.
— Get the Access ID, and Security Key
— Make sure that right Policy’s are assigned
— Add AWS keys in ~/.boto file
[Credentials]
aws_access_key_id=<AccessKey>
aws_secret_access_key=<SecurityKey>
— Get ec2.py file
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
— Get ec2.ini from
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
— Update regions value in ec2.ini to your region
regions = us-west-2 #Example
Export AWS_ACCESS_KEY
export AWS_ACCESS_KEY_ID='YOUR_AWS_API_KEY'
Export AWS_SECRET_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY='YOUR_AWS_API_SECRET_KEY'
Give Executable permissions to ec2.py
chmod +x /path/to/ec2.py
Export ANSIBLE_INVENTORY value
export ANSIBLE_INVENTORY=/path/to/ec2.py
Export EC2_INI_PATH value
export EC2_INI_PATH=/path/to/ec2.ini
Add your AWS keypair.pem identity to memory
ssh-agent bash
ssh-add ~/.ssh/keypair.pem
Execute ec2.py script to see the Dynamic inventory
/path/to/ec2.py --list
If you want to refresh your inventory everytime you run then use –refresh-cache
/path/to/ec2.py --list --refresh-cache
If you want to see one particular host related variables from your inventory then use –host
sudo /path/to/ec2.py --host [hostname or hostip]
Now, we understand how to create AWS Dynamic inventory. so Lets see How we run Ansible commands and playbooks against that Dynamic inventory.
sudo ansible-playbook -i /path/to/ec2.py TestPlaybook.yml -e "rhel=tag_Name_Jenkins" -u ec2-user
sudo ansible tag_Name_Jenkins -i /path/to/ec2.py -m ping -u ec2-user
I have tested above process successfully, Hope it may help you to design your dynamic inventory.
If you face any issues then please comment below. I will look into that issue.
Thanks.
@sarathchandrabhaskara
Copy link

hi i am getting error while adding ssh-add ~/.ssh/keypair.pem (it is not taking mykey.pem which i used in AWS for creating instance) it is taking only ~/.ssh/id_rsa. when i am pinging i am getting below error. pl give some steps how to access ssh keys while pinging dynamic inventory.

My error :
ansible -i p.py all -m pigng
172.31.35.154 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
"unreachable": true
}
172.31.34.151 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey).",
"unreachable": true

pl help how to resole this issue.

@jking6884
Copy link

hi i am getting error while adding ssh-add ~/.ssh/keypair.pem (it is not taking mykey.pem which i used in AWS for creating instance) it is taking only ~/.ssh/id_rsa. when i am pinging i am getting below error. pl give some steps how to access ssh keys while pinging dynamic inventory.

My error :
ansible -i p.py all -m pigng
172.31.35.154 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
"unreachable": true
}
172.31.34.151 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey).",
"unreachable": true

pl help how to resole this issue.

Did you find a resolution for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment