variable misunderstanding

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!

backups.postgresql does not exist, it is backups[1]

To get what you want the data would have to look like this:
       backups:
         mongodb
         postgresql:
             - sonarqube
             - other
         "/opt"
         "/etc"

Needs more colons:

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

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

% cat foo.yml

Furthering Todd’s message about needing more colons:

% cat foo.yml