Ansible: Debug and iteration

128 views
Skip to first unread message

Jegan A

unread,
Jan 13, 2020, 2:22:03 AM1/13/20
to Ansible Project
Hello Team,

        I am trying to iteration register value and print that value using debug but I unable to achieve the result which I want to get it. Below is the register value in that I like to get value of "lifecycle_state" using debug. Could you please someone help me on this.


Playbook part:

       - debug: var="{{ item }}"
         with_items:  result_1.volume_attachments

Register result:

ok: [localhost] => (item=result_1.volume_attachments) => {
    "ansible_loop_var": "item",
    "item": "result_1.volume_attachments",
    "result_1.volume_attachments": [
        {
            "attachment_type": "iscsi",
            "availability_domain": "xxxxxxxxx",
            "chap_secret": null,
            "chap_username": null,
            "compartment_id": "xxxxxxxxxxx",
            "device": null,
            "display_name": "xxxxxxxxx",
            "id": "xxxxxxxxxxx",
            "instance_id": "xxxxxx",
            "ipv4": "xxxxx",
            "iqn": "xxxxxxxxxxxx",
            "is_pv_encryption_in_transit_enabled": false,
            "is_read_only": false,
            "lifecycle_state": "ATTACHED",
            "port": 3260,
            "time_created": "xxxxxxxx",
            "volume_id": "xxxxxxxxxxx"
        }
    ]
}

Vladimir Botka

unread,
Jan 13, 2020, 3:15:01 AM1/13/20
to Jegan A, ansible...@googlegroups.com
On Sun, 12 Jan 2020 23:22:03 -0800 (PST)
Jegan A <jegan...@gmail.com> wrote:

> ... I like to get value of "lifecycle_state" using
> debug.
>
> - debug: var="{{ item }}"
> with_items: result_1.volume_attachments
>
> ok: [localhost] => (item=result_1.volume_attachments) => {
> "ansible_loop_var": "item",
> "item": "result_1.volume_attachments",
> "result_1.volume_attachments": [
> {
> "attachment_type": "iscsi",
> "availability_domain": "xxxxxxxxx",
> "chap_secret": null,
> "chap_username": null,
> "compartment_id": "xxxxxxxxxxx",
> "device": null,
> "display_name": "xxxxxxxxx",
> "id": "xxxxxxxxxxx",
> "instance_id": "xxxxxx",
> "ipv4": "xxxxx",
> "iqn": "xxxxxxxxxxxx",
> "is_pv_encryption_in_transit_enabled": false,
> "is_read_only": false,
> "lifecycle_state": "ATTACHED",
> "port": 3260,
> "time_created": "xxxxxxxx",
> "volume_id": "xxxxxxxxxxx"
> }
> ]
> }

The first 2 lines of the debug output explain what's going on

"ansible_loop_var": "item",
"item": "result_1.volume_attachments",

The list 'result_1.volume_attachments' is not expanded in the 'with_items'
statement and is submitted as 'item' to debug. Then the list is expanded
var="{{ item }}".

To print the 'lifecycle_state' attribute of the items in the list try

- debug:
var: item.lifecycle_state
loop: "{{ result_1.volume_attachments }}"

Notes:
* Use 'loop' instead of with_*
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.5.html#migrating-from-with-x-to-loop
* Expand the the variable "{{ result_1.volume_attachments }}" in the 'loop'
to iterate the items of the list
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#using-variables-with-jinja2
* In 'debug' the parameter 'var' is expanded by default


HTH,
-vlado
Reply all
Reply to author
Forward
0 new messages