过滤调试信息
Filter debug msg
我正在使用 Ansible 2.9.13 并且我有这个剧本:
---
- hosts: localhost
connection: local
vars:
ansible_python_interpreter: /usr/bin/env python3
vars_files:
- vars.yml
tasks:
- name: Get Tags from given VM Name
vmware_vm_info:
validate_certs: no
hostname: '{{ vcenter_server }}'
username: '{{ vcenter_user }}'
password: '{{ vcenter_pass }}'
folder: '{{ provision_folder }}'
delegate_to: localhost
register: vm_info
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478']"
当我 运行 它时,我得到这个输出:
ok: [localhost] => {
"msg": [
{
"attributes": {},
"cluster": "xxx01",
"esxi_hostname": "xxxx",
"guest_fullname": "Microsoft Windows 10 (64-bit)",
"guest_name": "C97A1612171478",
"ip_address": "10.x.x.x",
"mac_address": [
"0x:x:x:x:xd:x"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "420xxaf-xxx-xe2-9xe-a5xxxxxa3c",
"vm_network": {
"0x:x:x:xa:x:x": {
"ipv4": [
"169.x.x.x"
],
"ipv6": [
"x::x:x:x:xc"
]
},
"x:x:x:x:x0:x1": {
"ipv4": [
"169.x.x.x"
],
"ipv6": [
"x::x7:xf:x:x"
]
},
"0x:5x:x:x:ax:x": {
"ipv4": [
"10.x.x.x"
],
"ipv6": [
"x::1xx:x:8xx:x"
]
}
}
}
]
}
如何过滤输出以使其仅显示 "ip_address": "10.x.x.x"
。
最后只有10.x.x.x
.
我尝试了一些方法,在消息代码中添加密钥 ip_address
,但所有方法都给了我一个错误。
我可以使用 Python 过滤消息,但如果有办法使用 Ansible 获取它,我想知道如何实现。
我无法正确测试,但请尝试使用以下代码fiddle:
- debug:
msg: "{{ item.ip_address | json_query(query) }}"
loop: "{{ vm_info.virtual_machines }}"
vars:
query: "[?guest_name=='C97A1612171478']"
如果您想在没有 loop
的情况下获取此信息:
- 如果您因此需要一个对象:
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478'] | [0].{ip_address: ip_address}"
会产生
{
"ip_address": "10.x.x.x"
}
- 如果您需要一个字符串作为结果:
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478'] | [0].ip_address"
会产生
"10.x.x.x"
我正在使用 Ansible 2.9.13 并且我有这个剧本:
---
- hosts: localhost
connection: local
vars:
ansible_python_interpreter: /usr/bin/env python3
vars_files:
- vars.yml
tasks:
- name: Get Tags from given VM Name
vmware_vm_info:
validate_certs: no
hostname: '{{ vcenter_server }}'
username: '{{ vcenter_user }}'
password: '{{ vcenter_pass }}'
folder: '{{ provision_folder }}'
delegate_to: localhost
register: vm_info
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478']"
当我 运行 它时,我得到这个输出:
ok: [localhost] => {
"msg": [
{
"attributes": {},
"cluster": "xxx01",
"esxi_hostname": "xxxx",
"guest_fullname": "Microsoft Windows 10 (64-bit)",
"guest_name": "C97A1612171478",
"ip_address": "10.x.x.x",
"mac_address": [
"0x:x:x:x:xd:x"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "420xxaf-xxx-xe2-9xe-a5xxxxxa3c",
"vm_network": {
"0x:x:x:xa:x:x": {
"ipv4": [
"169.x.x.x"
],
"ipv6": [
"x::x:x:x:xc"
]
},
"x:x:x:x:x0:x1": {
"ipv4": [
"169.x.x.x"
],
"ipv6": [
"x::x7:xf:x:x"
]
},
"0x:5x:x:x:ax:x": {
"ipv4": [
"10.x.x.x"
],
"ipv6": [
"x::1xx:x:8xx:x"
]
}
}
}
]
}
如何过滤输出以使其仅显示 "ip_address": "10.x.x.x"
。
最后只有10.x.x.x
.
我尝试了一些方法,在消息代码中添加密钥 ip_address
,但所有方法都给了我一个错误。
我可以使用 Python 过滤消息,但如果有办法使用 Ansible 获取它,我想知道如何实现。
我无法正确测试,但请尝试使用以下代码fiddle:
- debug:
msg: "{{ item.ip_address | json_query(query) }}"
loop: "{{ vm_info.virtual_machines }}"
vars:
query: "[?guest_name=='C97A1612171478']"
如果您想在没有 loop
的情况下获取此信息:
- 如果您因此需要一个对象:
会产生- debug: msg: "{{ vm_info.virtual_machines | json_query(query) }}" vars: query: "[?guest_name=='C97A1612171478'] | [0].{ip_address: ip_address}"
{ "ip_address": "10.x.x.x" }
- 如果您需要一个字符串作为结果:
会产生- debug: msg: "{{ vm_info.virtual_machines | json_query(query) }}" vars: query: "[?guest_name=='C97A1612171478'] | [0].ip_address"
"10.x.x.x"