Philipp
Philipp

Reputation: 21

Ansible loop over multiple dictionaries/lists

In my playbook, I'd like to loop over two dictionaries (or one dictionary and one list). One is a list (or dictionary) of Domains, the other one is a dictionary that includes the aws-regions with the corresponding server-IPs to use for the DNS-Entries for latency based routing. I want to set for each domain one DNS-record for each aws-region.

- name: set DNS records for Domains
  route53:
    zone: "{{ item[0].key }}"
    record: "{{ item[0].key }}"
    value: "{{ item[1].value.server_ip }}"
    region: "{{ item[1].key }}"
    identifier: "{{ item[1].key }}"
  with_nested:
  - "{{ domain_dict }}"
  - "{{ aws_dict }}"

With two lists, the example works fine. How do I get it to work using at least one dictionary?

domain_dict: (could be a list as well)

domain_dict:
  mytest1.example:
  mytest2.example:
  mytest3.example:

aws_dict:

aws_dict:
  us-east-1:
   # some other region-related stuff like ami-id,...
    server_ip: 1.2.3.4
  us-west-1:
   # some other region-related stuff
    server_ip: 1.2.3.5    
  us-west-2:
   # some other region-related stuff
    server_ip: 1.2.3.6
  #all other aws-regions

Upvotes: 2

Views: 2283

Answers (1)

Valeriy Solovyov
Valeriy Solovyov

Reputation: 5648

A custom lookup_plugin is your best bet. Otherwise it'll be an ugly sequence of set_fact.

PS: While you ordinarily shouldn’t have to, should you wish to write your own ways to loop over arbitrary datastructures, you can read Developing Plugins for some starter information. Each of the above features are implemented as plugins in ansible, so there are many implementations to reference

Upvotes: 1

Related Questions