Creating subgroup of ec2 dynamic inventory

587 views
Skip to first unread message

Igor Cicimov

unread,
May 8, 2014, 2:32:53 AM5/8/14
to ansible...@googlegroups.com
Hi all,

I have the following playbook trying to create a subgroup out of my ec2 dynamic inventory with instances matching given tags: 

---
- hosts: ec2 
  gather_facts: False
  connection: local
  tasks:
     - add_host: hostname={{ hostvars[inventory_hostname]['ec2_publicIp'] }} groupname=ec2hosts server_name={{ hostvars[inventory_hostname]['ec2_public_dns_name'] }}
       when: hostvars[inventory_hostname]['ec2_region'] == 'ap-southeast-2' 
             and hostvars[inventory_hostname]['ec2_tag_Role'] ~ 'application' 
             and hostvars[inventory_hostname]['ec2_tag_Type'] == 'tomcat'

- hosts: ec2hosts
  gather_facts: True
  remote_user: user1
  sudo: True
  tasks:
    # fetch instance data from the metadata servers in ec2
    - ec2_facts:

    # show all known facts for this host
    - debug: var=hostvars[inventory_hostname]

    # just show the instance-id
    - debug: msg="{{ hostvars[inventory_hostname]['ansible_ec2_instance-id'] }}"


This is the result of its run:

PLAY [ec2] ******************************************************************** 

TASK: [add_host hostname={{hostvars[inventory_hostname]['ec2_publicIp']}} groupname=ec2hosts server_name={{hostvars[inventory_hostname]['ec2_public_dns_name']}}] *** 
skipping: [54.72.64.112]

PLAY [ec2hosts] *************************************************************** 
skipping: no hosts matched


The instances are being picked up correctly, I confirmed that with debug output, but looks like the group is not being populated correctly. I'm probably missing something here since looks like the supposed loop is not happening.

Can anyone spot what am I doing wrong here?

Thanks,
Igor

Michael DeHaan

unread,
May 9, 2014, 7:56:37 PM5/9/14
to ansible...@googlegroups.com
A few things to cleanup first:

"     - add_host: hostname={{ hostvars[inventory_hostname]['ec2_publicIp'] }} groupname=ec2hosts server_name={{ hostvars[inventory_hostname]['ec2_public_dns_name'] }}"

When you get ginormous variables like this, slow down, and define useful shortcuts in your "vars" section, like this:

vars:
    ec2ip: "{{ hostvars[inventory_hostname]['ec2_publicIp'] }}"

This allows cleaner playbooks like:

"     - add_host: hostname={{ ec2ip }} groupname=ec2hosts server_name={{ ec2dns }}"

etc

That's unrelated though.

Also this is overkill:

       when: hostvars[inventory_hostname]['ec2_region'] == 'ap-southeast-2' 

Could have just been:

    when: "ec2_region == 'ap-southeast-2'"

Ansible likes it simple.


 




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dd9eb356-287a-49ed-81cc-465623a4aa77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Igor Cicimov

unread,
May 11, 2014, 11:08:48 PM5/11/14
to ansible...@googlegroups.com
Thanks Michael your suggestions noted. I did some more testing and looks like only one single instance, assuming the last instance, from the inventory is being added to the group, and this is without any conditions. Very simple playbook example:


---
- hosts: ec2
  gather_facts: False
  connection: local
  vars:
    ec2ip: "{{ hostvars[inventory_hostname]['ec2_publicIp'] }}"
    ec2dns: "{{ hostvars[inventory_hostname]['ec2_public_dns_name'] }}"
  tasks:

     - add_host: "hostname={{ ec2ip }} groupname=ec2hosts server_name={{ ec2dns }}"

and I get only one single host in the ec2hosts group:


PLAY [ec2] ********************************************************************

TASK: [add_host hostname={{hostvars[inventory_hostname]['ec2_publicIp']}} groupname=ec2hosts server_name={{hostvars[inventory_hostname]['ec2_public_dns_name']}}] ***
ok: [instance_public_ip_obscured] => {"new_groups": ["ec2hosts"], "new_host": "instance_public_ip_obscured"}

Looks like this is not working the way I assume. From what I can see I have 52 instances in the current ec2 inventory cache:

$ cat ~/.ansible/tmp/ansible-ec2.cache | grep region | sort | uniq -c
     47         "ec2_region": "ap-southeast-2",
      5         "ec2_region": "eu-west-1",

Thanks,
Igor

Igor Cicimov

unread,
Dec 23, 2014, 11:57:36 PM12/23/14
to ansible...@googlegroups.com
Just in case someone else had similar problem, the solution was to add "serial: 1" to the playbook otherwise the execution is parallel thus only the last host preprocessed ends up in the group.

Simple example /etc/ansible/hosts-group inventory:
[group-tomcat]
10.22.0.43    server_name=app11 ec2_private_dns_name=ip-10-22-0-43
10.22.10.35    server_name=app21 ec2_private_dns_name=ip-10-22-10-35

Simple test33.yml playbook:
---
- hosts: '{{ tomcats }}'
  connection: local
  serial: 1
  gather_facts: no
  tasks:
  - add_host: hostname={{ ec2_private_dns_name }} groupname=backends server_name={{ server_name }}

The idea is to create a new in-memory group out of a group passed on in the input.

Without "serial: 1":

$ ansible-playbook -i /etc/ansible/hosts-group test33.yml --extra-vars '{ "tomcats" : "group-tomcat" }' --verbose

PLAY [group-tomcat] *******************************************************

TASK: [add_host hostname={{ ec2_private_dns_name }} groupname=backends server_name={{ server_name }}] ***
ok: [10.22.0.43] => {"new_groups": ["backends"], "new_host": "ip-10-22-0-43"}

PLAY RECAP ********************************************************************
add_host hostname={{ ec2_private_dns_name }} groupname=backends server_name={{ server_name }} --- 0.01s
10.22.0.43                : ok=1    changed=0    unreachable=0    failed=0 
10.22.10.35               : ok=1    changed=0    unreachable=0    failed=0


With "serial: 1":

$ ansible-playbook -i /etc/ansible/hosts-group test33.yml --extra-vars '{ "tomcats" : "group-tomcat" }' --verbose

PLAY [group-tomcat] *******************************************************

TASK: [add_host hostname={{ ec2_private_dns_name }} groupname=backends server_name={{ server_name }}] ***
ok: [10.22.0.43] => {"new_groups": ["backends"], "new_host": "ip-10-22-0-43"}

TASK: [add_host hostname={{ ec2_private_dns_name }} groupname=backends server_name={{ server_name }}] ***
ok: [10.22.10.35] => {"new_groups": ["backends"], "new_host": "ip-10-22-10-35"}

PLAY RECAP ********************************************************************
add_host hostname={{ ec2_private_dns_name }} groupname=backends server_name={{ server_name }} --- 0.00s
10.22.0.43                : ok=1    changed=0    unreachable=0    failed=0  
10.22.10.35               : ok=1    changed=0    unreachable=0    failed=0

The difference in the execution is obvious.
Reply all
Reply to author
Forward
0 new messages