# variable misunderstanding

**URL:** <https://forum.ansible.com/t/variable-misunderstanding/38680>\
**Category:** Ansible Project\
**Created:** [February 6, 2024, 1:05pm UTC](https://forum.ansible.com/t/variable-misunderstanding/38680 "2024-02-06T13:05:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory\_Edigarov](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gregory\_Edigarov](https://forum.ansible.com/u/Gregory_Edigarov)\
**Post date:** [February 6, 2024, 1:05pm UTC](https://forum.ansible.com/t/variable-misunderstanding/38680/1 "2024-02-06T13:05:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 6, 2024, 2:57pm UTC](https://forum.ansible.com/t/variable-misunderstanding/38680/2 "2024-02-06T14:57:47Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 6, 2024, 3:21pm UTC](https://forum.ansible.com/t/variable-misunderstanding/38680/3 "2024-02-06T15:21:49Z")

</div>

Needs more colons:

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

```

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [February 6, 2024, 5:20pm UTC](https://forum.ansible.com/t/variable-misunderstanding/38680/4 "2024-02-06T17:20:08Z")

</div>

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

**% cat foo.yml**

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [February 6, 2024, 5:34pm UTC](https://forum.ansible.com/t/variable-misunderstanding/38680/5 "2024-02-06T17:34:24Z")

</div>

Furthering Todd’s message about needing more colons:

**% cat foo.yml**
