Home > Blockchain >  Get specific parts of the output in Ansible
Get specific parts of the output in Ansible

Time:05-20

I need to get only specific parts of the output from Ansible, but it is giving me the whole information. I've tried using filtering unsuccessfully.

This is the playbook I am testing with (it has a loop and is probably what is throwing me off)

- name: PLAYBOOK -> Testing
  hosts: esxi
  gather_facts: no
  vars_files:
    -  vars.yml
  vars:
    vmnic:
     - vmnic0
     - vmnic1

  tasks:
    -  name: Get NIC driver/firmware details - shell
       shell: esxcli network nic get -n {{ item }} | grep -e Driver -e Firmware -e Version -e Name
       loop: "{{ vmnic }}"
       register: nic_details

    -  name: Output NIC driver/firmware details
       debug:  var=item.stdout_lines
       loop: "{{ nic_details['results'] }}"

This is the output I get for one host, I only need the last bit i.e. Driver Info, Driver, Firmware Version, Version and `Name of each VMNIC.

ok: [srv-pocte02.test.local] => (item={'changed': True, 'end': '2022-05-19 15:50:50.326514', 'stdout': '   Driver Info: \n         Driver: igbn\n         Firmware Version: 1.61.0:0x8000090e\n         Version: 1.4.1\n   Name: vmnic0', 'cmd': 'esxcli network nic get -n vmnic0 | grep -e Driver -e Firmware -e Version -e Name', 'stderr': '', 'start': '2022-05-19 15:50:49.515808', 'invocation': {'module_args': {'stdin_add_newline': True, 'argv': None, 'stdin': None, 'removes': None, 'creates': None, 'warn': False, '_uses_shell': True, 'executable': None, 'chdir': None, 'strip_empty_ends': True, '_raw_params': 'esxcli network nic get -n vmnic0 | grep -e Driver -e Firmware -e Version -e Name'}}, 'rc': 0, 'msg': '', 'delta': '0:00:00.810706', 'stdout_lines': ['   Driver Info: ', '         Driver: igbn', '         Firmware Version: 1.61.0:0x8000090e', '         Version: 1.4.1', '   Name: vmnic0'], 'stderr_lines': [], 'failed': False, 'item': 'vmnic0', 'ansible_loop_var': 'item'}) => {
    "ansible_loop_var": "item",
    "item": {
        "ansible_loop_var": "item",
        "changed": true,
        "cmd": "esxcli network nic get -n vmnic0 | grep -e Driver -e Firmware -e Version -e Name",
        "delta": "0:00:00.810706",
        "end": "2022-05-19 15:50:50.326514",
        "failed": false,
        "invocation": {
            "module_args": {
                "_raw_params": "esxcli network nic get -n vmnic0 | grep -e Driver -e Firmware -e Version -e Name",
                "_uses_shell": true,
                "argv": null,
                "chdir": null,
                "creates": null,
                "executable": null,
                "removes": null,
                "stdin": null,
                "stdin_add_newline": true,
                "strip_empty_ends": true,
                "warn": false
            }
        },
        "item": "vmnic0",
        "msg": "",
        "rc": 0,
        "start": "2022-05-19 15:50:49.515808",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "   Driver Info: \n         Driver: igbn\n         Firmware Version: 1.61.0:0x8000090e\n         Version: 1.4.1\n   Name: vmnic0",
        "stdout_lines": [
            "   Driver Info: ",
            "         Driver: igbn",
            "         Firmware Version: 1.61.0:0x8000090e",
            "         Version: 1.4.1",
            "   Name: vmnic0"
        ]
    },
    "item.stdout_lines": [
        "   Driver Info: ",
        "         Driver: igbn",
        "         Firmware Version: 1.61.0:0x8000090e",
        "         Version: 1.4.1",
        "   Name: vmnic0"
    ]
}
ok: [srv-pocte02.test.local] => (item={'start': '2022-05-19 15:50:50.867894', 'msg': '', 'cmd': 'esxcli network nic get -n vmnic1 | grep -e Driver -e Firmware -e Version -e Name', 'rc': 0, 'invocation': {'module_args': {'stdin_add_newline': True, 'stdin': None, 'removes': None, 'strip_empty_ends': True, '_uses_shell': True, 'creates': None, 'warn': False, 'chdir': None, 'executable': None, '_raw_params': 'esxcli network nic get -n vmnic1 | grep -e Driver -e Firmware -e Version -e Name', 'argv': None}}, 'changed': True, 'stderr': '', 'end': '2022-05-19 15:50:51.706813', 'stdout': '   Driver Info: \n         Driver: igbn\n         Firmware Version: 1.61.0:0x8000090e\n         Version: 1.4.1\n   Name: vmnic1', 'delta': '0:00:00.838919', 'stdout_lines': ['   Driver Info: ', '         Driver: igbn', '         Firmware Version: 1.61.0:0x8000090e', '         Version: 1.4.1', '   Name: vmnic1'], 'stderr_lines': [], 'failed': False, 'item': 'vmnic1', 'ansible_loop_var': 'item'}) => {
    "ansible_loop_var": "item",
    "item": {
        "ansible_loop_var": "item",
        "changed": true,
        "cmd": "esxcli network nic get -n vmnic1 | grep -e Driver -e Firmware -e Version -e Name",
        "delta": "0:00:00.838919",
        "end": "2022-05-19 15:50:51.706813",
        "failed": false,
        "invocation": {
            "module_args": {
                "_raw_params": "esxcli network nic get -n vmnic1 | grep -e Driver -e Firmware -e Version -e Name",
                "_uses_shell": true,
                "argv": null,
                "chdir": null,
                "creates": null,
                "executable": null,
                "removes": null,
                "stdin": null,
                "stdin_add_newline": true,
                "strip_empty_ends": true,
                "warn": false
            }
        },
        "item": "vmnic1",
        "msg": "",
        "rc": 0,
        "start": "2022-05-19 15:50:50.867894",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "   Driver Info: \n         Driver: igbn\n         Firmware Version: 1.61.0:0x8000090e\n         Version: 1.4.1\n   Name: vmnic1",
        "stdout_lines": [
            "   Driver Info: ",
            "         Driver: igbn",
            "         Firmware Version: 1.61.0:0x8000090e",
            "         Version: 1.4.1",
            "   Name: vmnic1"
        ]
    },
    "item.stdout_lines": [
        "   Driver Info: ",
        "         Driver: igbn",
        "         Firmware Version: 1.61.0:0x8000090e",
        "         Version: 1.4.1",
        "   Name: vmnic1"
    ]
}

CodePudding user response:

You can use the map filter to extract one field of a list of dictionaries. It can also be used to map one filter to each items of the list, for example, a from_yaml that could help you have dictionaries out of the string you get in stdout.

Given the task:

- debug:
    var: nic_details.results | map(attribute="stdout") | map('from_yaml')

This would result in:

nic_details.results | map(attribute="stdout") | map('from_yaml'):
- Driver Info:
    Driver: igbn
    Firmware Version: 1.61.0:0x8000090e
    Version: 1.4.1
  Name: vmnic0
- Driver Info:
    Driver: igbn
    Firmware Version: 1.61.0:0x8000090e
    Version: 1.4.1
  Name: vmnic1
  • Related