netkool
netkool

Reputation: 45

Ansible fetch value from dict

I have below output from the debug, I need to set variable and loop thru commands

Debug output:

    ok: [leafsw] => {
    "msg": [
        {
            "cl_list": "AWSCL",
            "delete": [
                {
                    "list": "11111:10000",
                    "seq": 1
                },
                {
                    "list": "22222:10000",
                    "seq": 2
                }
            ],
            "name": "AWSCL",
            "permit": [
                "11111:10000",
                "22222:10000"
            ]
        },
        {
            "cl_list": "NORM_CL",
            "name": "NORM_CL",
            "permit": [
                "33333:10000",
                "44444:10000"
            ]
        }
    ]
}

I need to fetch cl_list then next task is to use "with_items" to run other commands. First: how to fetch dict value cl_list Second: Add to variable so that I can use it a loop.

I tried:

- name: Get CL Name
  debug: var="{{ item }}"
  with_items: "{{ getclname.cl_list }}"

doesn't worked, also I tried:

- name: Get CL Name
  debug: var="{{ item.cl_list }}"
  with_items: "{{ getclname }}"

What I would like: variable = ['AWSCL','NORM_CL'] so that I can use that in with_items loops

Any ideas?

Upvotes: 0

Views: 4315

Answers (1)

ilias-sp
ilias-sp

Reputation: 6685

you were almost there! try this task:

- name: get the cl_list from the variable
  debug:
    var: item.cl_list 
  with_items: 
    - "{{ my_var }}"

result:

TASK [get the cl_list from the variable] ****************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
    "item.cl_list": "AWSCL"
}
ok: [localhost] => (item=None) => {
    "item.cl_list": "NORM_CL"
}

PLAY RECAP

its ready to be processed item by item.

SECOND WAY:

you could do this to get them in a list variable:

- name: get the cl_list from the variable
  debug:
    var: my_var | map(attribute="cl_list") | list

result:

TASK [get the cl_list from the variable] ****************************************************************************************************************************************************************************
ok: [localhost] => {
    "my_var | map(attribute=\"cl_list\") | list": [
        "AWSCL", 
        "NORM_CL"
    ]
}

Upvotes: 3

Related Questions