在 Ansible 中获取输出的特定部分
Get specific parts of the output in Ansible
我只需要从 Ansible 获取输出的特定部分,但它为我提供了全部信息。我试过使用过滤失败。
这是我正在测试的剧本(它有一个循环,可能是让我失望的原因)
- 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'] }}"
这是我为一台主机获得的输出,我只需要最后一位,即 Driver Info
、Driver
、Firmware Version
、Version
和“每个主机的名称” 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"
]
}
您可以使用 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
来帮助您从 stdout
中获得的字符串中提取字典。
鉴于任务:
- debug:
var: nic_details.results | map(attribute="stdout") | map('from_yaml')
这将导致:
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
我只需要从 Ansible 获取输出的特定部分,但它为我提供了全部信息。我试过使用过滤失败。
这是我正在测试的剧本(它有一个循环,可能是让我失望的原因)
- 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'] }}"
这是我为一台主机获得的输出,我只需要最后一位,即 Driver Info
、Driver
、Firmware Version
、Version
和“每个主机的名称” 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"
]
}
您可以使用 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
来帮助您从 stdout
中获得的字符串中提取字典。
鉴于任务:
- debug:
var: nic_details.results | map(attribute="stdout") | map('from_yaml')
这将导致:
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