Search code examples
amazon-web-servicesamazon-ec2ansibleansible-2.xansible-inventory

Ansible dynamic inventory when connecting with AWS


I have downloaded ec2.py and ec2.ini files and placed them under

/etc/ansible/inventory

and i have changed the ansible.cfg to

inventory      = /etc/ansible/inventory/ec2.py

i have environment variables set up as

export AWS_ACCESS_KEY_ID="XXXXXXXXXXXXXXX"
export AWS_SECRET_ACCESS_KEY="XXXXXXXXXXXXXXXXXXXXX"
export AWS_DEFAULT_REGION="us-east-1"
export EC2_INI_PATH="/etc/ansible/inventory/ec2.ini"

now when i run ansible -m ping all i am getting

[WARNING]: provided hosts list is empty, only localhost is available

instead of pinging all instances in my account

when i run ./ec2.py --list i get

{
   "_meta": {
   "hostvars": {}
}
}

all my instances in aws are running


Solution

  • It seems you are not using the right inventory file.

    • Check ec2.py is executable (chmod +x)
    • Run ansible with -v option to check what config file it is really using

      ansible -v -m ping all

    • Check if you have ANSIBLE_INVENTORY or ANSIBLE_HOSTS environment variables overriding expected config file. You can override inventory definition by setting

      export ANSIBLE_INVENTORY=/etc/ansible/inventory/ec2.py

    Remind that configuration files are processed in the following order

    • ANSIBLE_CONFIG (an environment variable)
    • ansible.cfg (in the current directory)
    • .ansible.cfg (in the home directory)
    • /etc/ansible/ansible.cfg

    Prior to 1.5 the order was:

    • ansible.cfg (in the current directory)
    • ANSIBLE_CONFIG (an environment variable)
    • .ansible.cfg (in the home directory)
    • /etc/ansible/ansible.cfg