Setting dynamic inventory hostnames from Ansible
You can use the zip
filter to combine your list of instances with a list of names, like this:
---
- hosts: localhost
gather_facts: false
vars:
tutorial_domain: example.com
ec2:
instances:
- public_ip: 1.2.3.4
- public_ip: 2.3.4.5
names:
- blue-duck
- red-panda
tasks:
- debug:
msg:
route53:
zone: "{{ tutorial_domain }}"
record: "{{ item.1 }}.{{tutorial_domain}}"
state: present
type: A
ttl: 120
value: "{{ item.0.public_ip }}"
wait: yes
loop: "{{ ec2.instances|zip(names)|list }}"
In older versions of Ansible, you would accomplish the same thing with a with_together
loop.