Search code examples
pythonamazon-web-servicesamazon-ec2ansibleansible-inventory

Using Ansible with AWS dynamic inventory (ec2.py)


Trying to use static+dynamic hosts file using ec2.py.

I've tried following guides:

Its important to say that running this below is working!

ansible -m ping tag_Ansible_Test

Meaning aws-cli, boto and ec2.py installed correctly and working.

Now, when defining the following groups (as documented) in hosts file:

[tag_Ansible_Test]

[backend-test-group:children]
tag_Ansible_Test

ansible -m ping backend-test-group -vvvv

When trying to run ansible command with the group, I get the following error:

ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
Parsed /etc/ansible/ec2.py inventory source with script plugin
 [WARNING]: Could not match supplied host pattern, ignoring: backend-test-group

 [WARNING]: No hosts matched, nothing to do

Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

What am I missing here?


Solution

  • Problem solved by setting a folder structure as follows:

    /etc/ansible/combined
    - hosts (the original file static+dynamic hosts file)
    - ec2.py
    - ec2.ini
    

    Then, altering the inventory setting in ansible.cfg:

    ...
    inventory       = /etc/ansible/combined
    ...
    

    Thanks to @KonstantinSuvorov for helping out with that!