Created
April 23, 2017 16:06
-
-
Save rgodishela/318b60d36fa9bb36ef932784436d053f to your computer and use it in GitHub Desktop.
AWS Dynamic inventory creation using Ansible.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you find a resolution for this issue?