Getting ready

IP updation inside the inventory file after the dynamic launch of  instance can be achieved by the EC2 dynamic inventory management. To setup dynamic inventory management, you need two files:

https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini

An ec2.py file is a Python script, which is responsible for fetching details of the EC2 instance, whereas the ec2.ini file is configuration file which is used by ec2.py. For example, if you mention private IP in ec2.ini, then while executing ec2.py, it will show you private IP. To use these files, your system should have the following packages:

  • Python-pip & Boto 
  • jq
  • GNU sed (it comes by default with Linux)

Now, you have to perform the following settings in your system:

  • Download the IAM user credentials ( AWS access key and AWS secret key). That IAM key should have the permission to create the resources that is mentioned previously in the main heading section. 
  • Install awscli and configure it with the IAM key that you just downloaded.
  • Download ec2.py and ec2.ini and make it globally available because we will use these files in the helper script. The role of the helper script is to run the ec2.py script and get the IP. Once you get the IP,  update the inventory file:
    # wget     https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inven    tory/ec2.py
# wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inven tory/ec2.ini
# chmod a+x ec2.py && chmod a+x ec2.ini
####We are Copying the files to /usr/bin/ to make it globally available###
# cp –rvpf ec2.* /usr/bin/
  • Install Boto and verify ec2.py by running it:
    # pip install boto
###Verify by running it. It will take some time, so wait a little bit####

# ec2.py --list
  • Download Json Parser jq:
    # wget http://stedolan.github.io/jq/download/linux64/jq
# chmod a+x jq && cp –rvpf jq /usr/bin

Now, you are good to run the playbook.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.150.109