Reputation: 7053
I need to update some configuration, which includes a list of all the EC2 nodes in my cluster, but also each node's configuration needs to know its own EC2 public and private IP address.
For the full list, I have the following:
- name: Gather EC2 facts on all nodes
ec2_instance_facts:
region: "{{ aws.region }}"
filters:
"tag:Type": nodeType
"tag:Name": '{{ aws.ec2.nodes.name }}'
"instance-state-name": running
register: ec2_facts
Then gather all the IPs and add them to a group:
- name: Gather IPs for all hosts
add_host: hostname={{ item.public_ip_address }} groups=allIpAddresses
loop: "{{ ec2_facts.instances }}"
when:
- item.state.name == "running"
This groups variable is then used in another play and used to substitute out the values in an Ansible template:
- seeds: "{{ allIpAddresses[0] }},{{ allIpAddresses[1] }},{{ allIpAddresses[2] }},{{ allIpAddresses[3] }}"
However, I cannot seem to extract or identify which public and private IP is for the "current" host.
Upvotes: 0
Views: 1625
Reputation: 15926
Use ec2_metadata_facts to gather facts in variables associated per instance.
Alternatively, if you want to use ec2_instance_facts
you would do a lookup
based on the inventory_hostname
:
sample play:
- name: gather ec2 facts per instance
hosts: my_aws_instances
gather_facts: no
tasks:
- ec2_metadata_facts:
- name: debug
debug: var=ansible_ec2_public_ipv4
- name: debug
debug: var=ansible_ec2_local_ipv4
Upvotes: 2