Hi all,
I have the following playbook trying to create a subgroup out of my ec2 dynamic inventory with instances matching given tags:
Hi all,
I have the following playbook trying to create a subgroup out of my ec2 dynamic inventory with instances matching given tags:
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.
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:
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: