Skip to content

professorxplorer/Ansible-Dynamic-Inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Ansible Dynamic Inventory for AWS EC2

Pre-requisites:

  1. Ansible Server - Get Click here to install on RHEL 8 and click here to install on Amazon Linux

Setup

Optional Step to add ssh to all ec2 instances in on go ! or to setup ssh in ansible hosts Click here

To get help on dynamic inventory please follow Ansible Official Document To get help on dynamic inventory please follow AWS Official Document

  1. Download ec2.py and ec2.ini files
  2. Create IAM Programmatic access user with EC2 full access on AWS console if you are using ec2 instance as your ansible server IAMusersAdd user EC2Select-your-serverActionsSecurityModify IAM roleAdd or Update IAM role
  3. Export IAM user credentials on the Ansible server. if you are using your own machine as a server Get an IAM role and get secret keys from AWS account.
  4. Install AWS CLI on your machine. sudo apt-get update sudo apt-get install awscli
  5. Configure aws cli aws configure Enter your aws Access and secret Access keys AWS Access Key ID [****************DQ7G]: AWS Secret Access Key [****************W9mW]: Default region name [us-east-1]:
export AWS_ACCESS_KEY_ID='1bc123'
export AWS_SECRET_ACCESS_KEY='abc123'
  1. install python-pip and boto3

Install Python

sudo apt-get install python

Install pip

sudo apt install python3-pip

Install Python Boto3 using PIP

pip install boto3 pip3 - version 4. To export keys permanently make sure that you have installed pip and boto and add credentials ~/.boto file 5. add executing permissions to ec2.py script

chmod 755 ec2.py
  1. test the script
./ec2.py - list
  1. List out servers that are running on ap-south-1a AZ
ansible -i ec2.py ap-south-1a - list-hosts
  1. How to Run playbook with tags

Option 1

  1. We can run ansible-playbook for our sever with some specific tag by giving hosts name as a tag in our playbook
- name: Aerospike Dynamic Inventory
hosts: tag_dev_view
gather_facts: no
# vars_files:
  1. then we can run our playbook as ansible-playbook -i ec2.py main.yml in this as we are giving tags in playbook tag is the combination of tag_dev_view tag → tag dev → key view →value

Option 2

  1. In this we can give multiple tags to our ansible server to identify environment and name Because we could have the same name in multiple environments.
  2. For this we can get our hosts to all AWS we don't have any hosts as it's dynamic inventory.
  3. Now we will be providing tags in command while running the ansible-playbook

To ping server with tag

ansible -i ec2.py - limit "tag_App_backend:&tag_Environment_staging:&tag_Usage_clock_worker" -m ping all

To run the playbook

ansible-playbook -i ec2.py - limit "tag_App_backend:&tag_Environment_staging:&tag_Usage_clock_worker" main.yml

Authors

The module is maintained by Professor Xplorer

License

Licensed. See LICENSE for full details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages