0

actually, I'd like to know how to use the value of a nested variable in a loop. I have a list with nested objects, e.g.:

kommunen:
  weimar:
    tld: stadt-weimar.de
    desc: Kommune Weimar
    admins:
      fritzfriemel:
        vorname: Fritz
        name: Friemel
        mail: f.friemel@stadt-weimar.de
      frankfrokel:
        vorname: Frank
        name: Frokel
        mail: f.frokel@stadt-weimar.de

In Ansible, I created a task to use the values of the key "vorname".

- name: "Account Daten testen"
  tags: testusers
  debug:
    msg={{ item[0] }}
  with_subelements:
    - '{{ kommunen }}'
    - admins

Am I mixing up things here?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
sebbe
  • 1
  • 2
  • 2
    `I have a list` <= no, you have a dict and you are using concepts made for lists. See (non exhaustive list of documentation to read): https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#list-variables, https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-dictionary, https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#transforming-dictionaries-into-lists, https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#transforming-lists-into-dictionaries – Zeitounator Jun 30 '22 at 09:53

1 Answers1

0

Am I mixing up things here?

As already mentioned in the comments, yes, it seems so. So you may proceed further with the there linked documentation and examples.

The following examples show how you could create a simple list of the first level of your dictionary kommunen by using data manipulation and the Jinja 2 filter list.

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    kommunen:
      weimar:
        tld: stadt-weimar.de
        desc: Kommune Weimar
        admins:
          fritzfriemel:
            vorname: Fritz
            name: Friemel
            mail: f.friemel@stadt-weimar.de
          frankfrokel:
            vorname: Frank
            name: Frokel
            mail: f.frokel@stadt-weimar.de
      erfurt:
        admins:

  tasks:

  - name: "Account Daten testen"
    debug:
      msg: "{{ item }}"
    loop: "{{ kommunen | list }}"

  - name: Show admins
    debug:
      msg: "{{ kommunen[item].admins }}"
    loop: "{{ kommunen | list }}"

resulting into an output of

TASK [Account Daten testen] **********
ok: [localhost] => (item=erfurt) =>
  msg: erfurt
ok: [localhost] => (item=weimar) =>
  msg: weimar

TASK [Show admins] *******************
ok: [localhost] => (item=erfurt) =>
  msg: ''
ok: [localhost] => (item=weimar) =>
  msg:
    frankfrokel:
      mail: f.frokel@stadt-weimar.de
      name: Frokel
      vorname: Frank
    fritzfriemel:
      mail: f.friemel@stadt-weimar.de
      name: Friemel
      vorname: Fritz

Documentation

Further Q&A


Is it mandatory to transfer the dict into a list, or can I point to an object directly?

This will depend on what you try to achieve. If you like to iterate over it, yes. If you have already some information from elsewhere you could use

  - name: Set CITY
    set_fact:
      CITY: "weimar"

  - name: Show admins
    debug:
      msg: "{{ kommunen[CITY].admins[item].name }}"
    loop: "{{ kommunen[CITY].admins | list }}"

resulting into an output of

TASK [Set CITY] *************************
ok: [localhost]

TASK [Show admins] **********************
ok: [localhost] => (item=fritzfriemel) =>
  msg: Friemel
ok: [localhost] => (item=frankfrokel) =>
  msg: Frokel
U880D
  • 8,601
  • 6
  • 24
  • 40
  • What I'd like to do is to use elements of a deeper level of the dictionary, e.g. mail, name and vorname. Is it mandatory to transfer the dict into a list, or can I point to an object directly? – sebbe Jun 30 '22 at 12:11
  • @sebbe, regarding "_Can I point to `name` or `vorname` directly?_", to do so you would need to know how the nodes in the dictionary are named. I've added an example which shows it for the city, but not for `admins[item]`. Maybe an other or simpler data structure can be used as I don't see an advantage why the data set of the admin is named like his content `fritzfriemel` = `$vorname + $name`. It is mixing up data and meta data which it shouldn't. – U880D Jun 30 '22 at 13:00
  • thank you, @U88D. Thank you very much for your clarification. In the meantime, the data structure changed a bit. The data comes via csv files. I end up with a variable like this: – sebbe Aug 25 '22 at 11:52