1

I have a set of variables which define FQDNs.

domains:
  - erp: erp.mycompany.com
  - crm: crm.mycompany.com
  - git: git.mycompany.com

Indeed, I both need to loop over them and access them namely (in a template file). So accessing them like domains.erpworks like a charm. But I can't get ansible to loop over these.

Obviously, if I do:

- name: Print domains
  debug:
    msg: test {{ item }}
  with_items:
    - "{{ domains }}"

It prints both the key and the value… And if I do:

- name: Print domains
  debug:
    msg: test {{ domains[{{ item }}] }}
  with_items:
    - "{{ domain }}"

But that doesn't work. I also tried the hashes form as mentionned in the docs, but didn't get any luck either…

1
  • 1
    Ansible seems quite complicated with nested lists, see my question here: stackoverflow.com/questions/36206551/…. Probably playbooks should not be overengineered or you should implement some custom plugins, as the syntax gets really nasty. Commented May 22, 2016 at 14:21

2 Answers 2

2

Finally, I had to use a dict.

It didn't work the first time because unlike with_items, which has the items going each on their own line, with_dict is a one liner without - before the element to loop through.

domains:
  erp:
    address: erp.mycompany.com
  crm:
    address: crm.mycompany.com
  git:
    address: git.mycompany.com

# used by letsencrypt
webserverType: apache2
withCerts: true

tasks:

- name: Print phone records
  debug:
    msg: "{{ item.value.address }}"
  with_dict: "{{ domains }}"

# I can still access a given domain by its name when needed like so:
{{ domains.erp.address }}
Sign up to request clarification or add additional context in comments.

Comments

1

Looks like you figured out your issue. Your original attempt uses a list of dictionaries that do not contain the same keys, making it difficult to access the values uniformly across each list item.

Your second solution creates a dictionary where the keys refer to other dictionaries.

Another solution than what you posted if you still wanted to use a list:

    - hosts: localhost
      vars:
        domains:
          - name: erp
            address: erp.mycompany.com
          - name: crm
            address: crm.mycompany.com
          - name: git
            address: git.mycompany.com
      tasks:
        - name: Print phone records
          debug:
            msg: "{{ item.address }}"
          with_items: "{{ domains }}"

To me this approach is simpler but your second approach works as well.

2 Comments

Thx for the answer. Nevertheless, with this approach, I don't seem to be able to reference my domains by name. If in some other part of my playbook, I need for exemple to access erp's domain name, how can I address it? domains.0.address is really error prone.
Ah you are correct. I misunderstood your use case. Your approach is probably the most reliable way if you need to get the domain items individually and not just loop through them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.