Furthering Todd's message about needing more colons:

% cat foo.yml

---

- name: list to dict

  hosts: localhost

  become: false

  gather_facts: false

  vars:

    backups:

      mongodb:

      postgresql:

        - sonarqube

        - other

      /opt:

      /etc:

  tasks:

    - debug: var=backups

    - debug: var=backups.postgresql

% ansible-playbook -i localhost, foo.yml


PLAY [list to dict] 
****************************************************************************************************


TASK [debug] 
***********************************************************************************************************

ok: [localhost] => {

    "backups": {

        "/etc": null,

        "/opt": null,

        "mongodb": null,

        "postgresql": [

            "sonarqube",

            "other"

        ]

    }

}


TASK [debug] 
***********************************************************************************************************

ok: [localhost] => {

    "backups.postgresql": [

        "sonarqube",

        "other"

    ]

}


PLAY RECAP 
*************************************************************************************************************

localhost                  : ok=2    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0

Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Feb 6, 2024, at 12:20 PM, 'Rowe, Walter P. (Fed)' via Ansible Project 
<[email protected]> wrote:

It is important to understand the structure of the data. Your backups variable 
defines a list, not a dictionary.

% cat foo.yml

---
- name: list to dict
  hosts: localhost
  become: false
  gather_facts: false
  vars:
    backups:
      - mongodb
      - postgresql:
        - sonarqube
        - other
      - /opt
      - /etc
  tasks:
    - debug: var=backups


% ansible-playbook -i localhost, foo.yml

PLAY [list to dict] 
****************************************************************************************************

TASK [debug] 
***********************************************************************************************************
ok: [localhost] => {
    "backups": [
        "mongodb",
        {
            "postgresql": [
                "sonarqube",
                "other"
            ]
        },
        "/opt",
        "/etc"
    ]
}

PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0

backup[0] is a string "mongo"

backup[1] is a dictionary with one key 'postgresql'

backup[2] & [3] are strings "/opt" and "/etc"

backups[1].postgresql is a list of strings

backups[1].postgresql[0] is string "sonarqube"
backups[1].postgresql[1] is string "other"


Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Feb 6, 2024, at 8:05 AM, [email protected] <[email protected]> 
wrote:

i have this:

      backups:
        - mongodb
        - postgresql:
            - sonarqube
            - other
        - /opt
        - /etc

later i use it in template task:
- name: template backup script out
  template:
    src: backup-script.sh.j2
    dest: /root/backup.sh
    mode: 755


my jinja2 is:
{% if backups.postgresql is defined %}
  {% for db in backups.postgresql %}
     {{ db }}
   {% endfor %}
{% endif %}

expected output:
sonarqube
other

but got an empty output.
what's wrong in my template?

thank you!

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d9bd0bf0-11b4-45db-8b94-2cd445a093a9n%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/d9bd0bf0-11b4-45db-8b94-2cd445a093a9n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7910AFC7-18E1-4952-B8B9-F764461E0405%40nist.gov<https://groups.google.com/d/msgid/ansible-project/7910AFC7-18E1-4952-B8B9-F764461E0405%40nist.gov?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/D10ADC76-B429-4FF0-8148-24873BFA6AEC%40nist.gov.

Reply via email to