How do I extract a value for a certain key in json output in Ansible into a list?

I have the following data structure set as a fact at the end of an ansible task. I am trying to extract the SITEID's into a list.

"myoutput": [
        {
         "ID": "ZG5zLmhvc3QkLm5vbl9ETlNfaG9zdF9yb290LjAuMTUzNzE1MTcwNzgxNC5oZWxsbw", 
        "extattrs": {
            "SITEID": "10"
         }
       }, 
       {
        "ID": "r/ZG5zLmhvc3QkLm5vbl9ETlNfaG9zdF9yb290LjAuMTUzNzE1OTcyMzQzNy5ibGFo0", 
        "extattrs": {
            "SITEID": "16"
        }
    }
]

I have tried the following but this just gives me back an undefined list.

  name: write myoutput SITEID to list
   set_fact:
     mylist : "{{  host[1].extattrs | map(attribute='SITEID') | list }}"

1 answer

  • answered 2018-09-17 07:35 techraf

    It's as simple as:

    - name: write myoutput SITEID to list
      set_fact:
        mylist: "{{ myoutput | map(attribute='extattrs.SITEID') | list }}"