Search code examples
ansibleyamlansible-inventoryansible-facts

How to get a specific line from Ansible nested register output


So I'm trying to get a specific row/line from Ansible register output. But since my output is quiet nested I can't seem to get the right value I want.

Playbook looks as followed,

---
- name: Get some piece of information
  *some Ansible module*:
    epg_info: First_EPG 
    state: query
  register: epg_info

- debug:
    var: epg_info
...

So you see I use a network module provided by Ansible to query the information of the "First_EPG" and register it inside epg_info. Next I debug it and these are the lines I get,

ok: [... . ... . ... . ...] => {
    "epg_info": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "changed": false,
                "current": [
                    {
                        "fvAEPg": {
                            "attributes": {
                                "annotation": "",
                                "dn": "THIS IS THE LINE I WANT TO HAVE",
                                "exceptionTag": "",
                                "extMngdBy": "",
                            },
                    }
                  } 
                } 
              } 
            } 

Dont mind the number of "{}" cause the list is a whole lot longer, I just narrowed it down for ease of use.

As you can see the list is quiet nested, and the only line I want to have is the "dn" one and store this value in another list.

Any ideas of how I can get only that line?


Solution

  • You can use indexing to get that value:

        - name: fetch value
          debug:
            msg: "{{ epg_info['results'][0]['current'][0]['fvAEPg']['attributes']['dn'] }}"