variable misunderstanding

50 views
Skip to first unread message

gregory....@gmail.com

unread,
Feb 6, 2024, 8:05:46 AM2/6/24
to Ansible Project
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!

Brian Coca

unread,
Feb 6, 2024, 9:58:13 AM2/6/24
to ansible...@googlegroups.com
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"



--
----------
Brian Coca (he/him/yo)

Todd Lewis

unread,
Feb 6, 2024, 10:22:01 AM2/6/24
to ansible...@googlegroups.com, uto...@gmail.com
Needs more colons:
       backups:
         mongodb:
         postgresql:
             - sonarqube
             - other
         "/opt":
         "/etc":


On 2/6/24 9:57 AM, Brian Coca wrote:
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"




-- 
Todd

Rowe, Walter P. (Fed)

unread,
Feb 6, 2024, 12:20:28 PM2/6/24
to ansible...@googlegroups.com
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

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d9bd0bf0-11b4-45db-8b94-2cd445a093a9n%40googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Feb 6, 2024, 12:34:41 PM2/6/24
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages