Looping through Ansible Register output and printing the result as list

Viewed 120

This is the sample task which give me the output which contains a list of self IPs. I want to capture this in Result and just print only the name of self IP.

I was able to print if I give a list index, but I can't loop through it to capture it as a separate list to use it in a different task.

- name: Port Lock Down - Allow Default
  bigip_device_info:
    gather_subset:
      - self-ips
    provider: "{{provider}}"
  register: Result
- name : Display the selfips
  debug:
    var: Result
TASK [Display the Selfip config] **************************************************************************************************************************************************************************
ok: [LD-F5-01] => {
    "Result": {
        "ansible_facts": {
            "ansible_net_queried": true,
            "ansible_net_self_ips": [
                {
                    "address": "3.3.3.3",
                    "allow_access_list": [
                        "tcp:8080",
                        "udp:53",
                        "gre:0",
                        "default"
                    ],
                    "floating": "no",
                    "full_path": "/Common/test-self-ip-1",
                    "name": "test-self-ip-1",
                    "netmask": "255.255.255.0",
                    "netmask_cidr": 24,
                    "traffic_group": "/Common/traffic-group-local-only",
                    "traffic_group_inherited": "no",
                    "vlan": "/Common/test-vlan-1"
                },
                {
                    "address": "4.4.4.4",
                    "allow_access_list": "all",
                    "floating": "no",
                    "full_path": "/Common/test-self-ip-2",
                    "name": "test-self-ip-2",
                    "netmask": "255.255.255.0",
                    "netmask_cidr": 24,
                    "traffic_group": "/Common/traffic-group-local-only",
                    "traffic_group_inherited": "no",
                    "vlan": "/Common/test-vlan-1"
                }
            ]
        },
        "changed": false,
        "failed": false,
        "queried": true,
        "self_ips": [
            {
                "address": "3.3.3.3",
                "allow_access_list": [
                    "tcp:8080",
                    "udp:53",
                    "gre:0",
                    "default"
                ],
                "floating": "no",
                "full_path": "/Common/test-self-ip-1",
                "name": "test-self-ip-1",
                "netmask": "255.255.255.0",
                "netmask_cidr": 24,
                "traffic_group": "/Common/traffic-group-local-only",
                "traffic_group_inherited": "no",
                "vlan": "/Common/test-vlan-1"
            },
            {
                "address": "4.4.4.4",
                "allow_access_list": "all",
                "floating": "no",
                "full_path": "/Common/test-self-ip-2",
                "name": "test-self-ip-2",
                "netmask": "255.255.255.0",
                "netmask_cidr": 24,
                "traffic_group": "/Common/traffic-group-local-only",
                "traffic_group_inherited": "no",
                "vlan": "/Common/test-vlan-1"
            }
        ]
    }
}
- name : Display the selfips
  debug:
    var: Result.self_ips[0]['name']
TASK [Display the selfips] ********************************************************************************************************************************************************************************
ok: [LD-F5-01] => {
    "Result.self_ips[0]['name']": "test-self-ip-1"
}

Can someone help me to use the for loop to get all the self IPs names as list instead of one single output?

1 Answers

You actually do not need a loop to achieve this, you can use the map filter of Jinja instead:

Result.self_ips | map(attribute='name')

Given the task:

- debug:
    var: Result.self_ips | map(attribute='name')
  vars:
    Result:
      self_ips:
        - address: 3.3.3.3
          allow_access_list:
            - 'tcp:8080'
            - 'udp:53'
            - 'gre:0'
            - default
          floating: 'no'
          full_path: /Common/test-self-ip-1
          name: test-self-ip-1
        - address: 4.4.4.4
          allow_access_list: all
          floating: 'no'
          full_path: /Common/test-self-ip-2
          name: test-self-ip-2

This yields the result:

Result.self_ips | map(attribute='name'):
  - test-self-ip-1
  - test-self-ip-2
Related