debugging nested list

27 views
Skip to first unread message

Henry Medina

unread,
Aug 23, 2017, 10:06:51 PM8/23/17
to Ansible Project

Hello Folks,


     I have a debugging question that has left me scratching my head.  I have a vars file with the following vars (using ansible 2.3.2.0):


packages1:

  one:

    version: "one-ish"

  two:

    version: "two-ish"


##

I have a playbook with the following debugging statements:


---

hosts: localhost

  connection: local

  gather_facts: no


  vars_files:

    vars.yml


  tasks:

    debug:

        var: "{{ item.version }}"

      with_items: "{{ packages1 }}"

    debug:

        var: packages1.one.version

    debug:

        var: packages1


##

Here is the result:

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

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object'




if I just print the items:

---

hosts: localhost

  connection: local

  gather_facts: no


  vars_files:

    vars.yml


  tasks:

    debug:

        var: "{{ item }}"

      with_items: "{{ packages1 }}"

    debug:

        var: packages1.one.version

    debug:


        var: packages1



I get the following results:

ansible-playbook play.yml

 [WARNING]: Host file not found: /etc/ansible/hosts


 [WARNING]: provided hosts list is empty, only localhost is available



PLAY [localhost] ****************************************************************************************************************************************************************************************


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

ok: [localhost] => (item=two) => {

    "item": "two", 

    "two": "VARIABLE IS NOT DEFINED!"

}

ok: [localhost] => (item=one) => {

    "item": "one", 

    "one": "VARIABLE IS NOT DEFINED!"

}


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

ok: [localhost] => {

    "packages1.one.version": "one-ish"

}


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

ok: [localhost] => {

    "packages1": {

        "one": {

            "version": "one-ish"

        }, 

        "two": {

            "version": "two-ish"

        }

    }

}


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


localhost                  ok=3    changed=0    unreachable=0    failed=0 




Why can't the "with_items" loop pickup list elements and it's values?? What am I missing here?

Pshem Kowalczyk

unread,
Aug 24, 2017, 4:05:02 AM8/24/17
to Ansible Project
Your 'packages1' is a dictionary/hash not a list/array, so if you want to iterate over it you have to use with_dict.

kind regards
Pshem


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ad5dca6e-26e0-458c-adac-1b7c08f2649e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages