Hi,
I am new with using the dynamic hosts option. I am creating ec2 instances and creating a dynamic host group so I could run several different roles on them after the instances are created.
My issue is that after the instances are created my other roles are being ran only on the first host in the inventory.
site.yml
-
name: Spin up EC2 servers
hosts: ec2_instances
connection: local
gather_facts: true
roles: -
ec2
tags: -
ec2
-
hosts: dynamic_hosts
gather_facts: false
tasks: -
ping:
-
debug: var=hostvars[inventory_hostname]
-
debug: msg=dynamic_hosts
roles: -
ping
tags: -
ec2
Tasks file:
-
name: Launch Instance
ec2:
group_id: “{{ hostvars[inventory_hostname].group_id }}”
count: 1
instance_type: ‘t2.nano’
image: ‘{{ hostvars[inventory_hostname].image }}’
wait: true
region: ‘region’
keypair: keypair
instance_tags: “{{ hostvars[inventory_hostname].tags }}”
profile: profile_name
register: ec2 -
name: Add hosts group temporary inventory group
add_host:
name: “{{ item.public_ip }}”
groups: dynamic_hosts
ansible_ssh_host: “{{ item.public_ip }}”
ansible_ssh_private_key_file: file.pem
ansible_ssh_user: user
ec2_instance_ids: “{{ ec2.instance_ids }}”
with_items: ec2.instances
inventory file
[ec2_instances]
ec2test1 image=“image_id” tags=“{ ‘env’: ‘test’, ‘Name’: ‘test1’ }” group_id=‘group’
ec2test2 image=“image_id” tags=“{ ‘env’: ‘test’, ‘Name’: ‘test2’ }” group_id=‘group’
I run the playbook like this:
ansible-playbook --tags=ec2 -i inventory site.yml
The outcome is both ec2test1\2 instances are created but the ping role is being run only on the first instance in the inventory file(ec2test1).
It seems that the dynamic_hosts group that I am creating will not assign more than 1 object there.
Please assist.
Ori.