如何从ansible输出中过滤掉特定行
How to filter out a particular line from ansible output
我在 运行 我的 ansible-playbook 从 playbook 输出中过滤掉 IP 地址时遇到问题。这是我的代码
- hosts: myhost
tasks:
- name: Gather all VMs from a specific folder
community.vmware.vmware_vm_info:
hostname: myhost.com
username: local
password: mypass
folder: "/VMFStorage/vm/"
validate_certs: False
delegate_to: localhost
register: vm_info
- debug:
var: vm_info
代码的输出
ok: [myhost] => {
"vm_info": {
"changed": false,
"failed": false,
"virtual_machines": [
{
"attributes": {},
"cluster": "Storage",
"datacenter": "VMFStorage",
"esxi_hostname": "161.92.211.24",
"folder": "/VMFStorage/vm/",
"guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
"guest_name": "RHEL Machine",
"ip_address": "192.168.1.47",
"mac_address": [
"00:50:56:a3:b1:e9"
],
"moid": "vm-22949",
"power_state": "poweredOn",
"tags": [],
"uuid": "4223f3fa-eeae-6a96-6af5-d68dc94199f3",
"vm_network": {
"00:50:56:a3:b1:e9": {
"ipv4": [
"192.168.1.47"
],
"ipv6": [
"fe80::250:56ff:fea3:b1e9"
]
}
}
},
{
"attributes": {},
"cluster": "Storage",
"datacenter": "VMFStorage",
"esxi_hostname": "161.92.211.22",
"folder": "/VMFStorage/vm/",
"guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
"guest_name": "Machine2",
"ip_address": "192.168.1.29",
"mac_address": [
"00:50:56:a3:b3:6d"
],
"moid": "vm-21360",
"power_state": "poweredOff",
"tags": [],
"uuid": "42239cd1-69ec-ff5d-a557-85d0bc8c9edc",
"vm_network": {
"00:50:56:a3:b3:6d": {
"ipv4": [],
"ipv6": []
}
}
},
所需的输出是捕获 Machine1 来宾名称的 IP 地址并打印该输出
192.168.1.29
我尝试了很多可能性,但找不到合适的解决方案
任何帮助将不胜感激
更新
- hosts: myhost
tasks:
- name: Gather all VMs from a specific folder
community.vmware.vmware_vm_info:
hostname: myhost.com
username: local
password: mypass
folder: "/VMFStorage/vm/"
validate_certs: False
delegate_to: localhost
register: vm_info
- set_fact:
ip: "{{ vm_info.virtual_machines|
selectattr('guest_name', 'eq', 'AlmaLinuxBUILD-Machine')|
map(attribute='ip_address')|first }}"
- debug:
var: ip
试试这个.. 如果你只想要 machine1 ip_address.
- debug:
msg: "{{ vm_info.virtual_machines[1].ip_address }}"
试试这个..如果你只想要所有 VM ip_addresses.
- debug:
msg: "{{ vm_info.virtual_machines[*].ip_address }}"
有很多选择。例如,
- Select 首先从列表中提取字典并提取属性
- debug:
var: ip
vars:
ip: "{{ vm_info.virtual_machines|
selectattr('guest_name', 'eq', 'Machine2')|
map(attribute='ip_address')|first }}"
给予
ip: 192.168.1.29
- 如果您更喜欢json_query,下面的任务会给出相同的结果
- debug:
var: ip
vars:
ip: "{{ vm_info.virtual_machines|
json_query('[?guest_name==`Machine2`].ip_address')|first }}"
- 如果列表中有很多项目需要引用,请先创建字典,例如
- debug:
var: name_ip
vars:
name_ip: "{{ vm_info.virtual_machines|
items2dict(key_name='guest_name',
value_name='ip_address') }}"
给予
name_ip:
Machine2: 192.168.1.29
RHEL Machine: 192.168.1.47
然后,使用这个字典来获取客人的IP。下面的任务也给出了相同的结果
- debug:
var: ip
vars:
ip: "{{ name_ip['Machine2'] }}"
问:“如何在同一个剧本的其他地方使用ip变量?”
答:有many options。比如放入set_facts(优先级19)
- set_facts:
ip: "{{ name_ip['Machine2'] }}"
- debug:
var: ip
我在 运行 我的 ansible-playbook 从 playbook 输出中过滤掉 IP 地址时遇到问题。这是我的代码
- hosts: myhost
tasks:
- name: Gather all VMs from a specific folder
community.vmware.vmware_vm_info:
hostname: myhost.com
username: local
password: mypass
folder: "/VMFStorage/vm/"
validate_certs: False
delegate_to: localhost
register: vm_info
- debug:
var: vm_info
代码的输出
ok: [myhost] => {
"vm_info": {
"changed": false,
"failed": false,
"virtual_machines": [
{
"attributes": {},
"cluster": "Storage",
"datacenter": "VMFStorage",
"esxi_hostname": "161.92.211.24",
"folder": "/VMFStorage/vm/",
"guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
"guest_name": "RHEL Machine",
"ip_address": "192.168.1.47",
"mac_address": [
"00:50:56:a3:b1:e9"
],
"moid": "vm-22949",
"power_state": "poweredOn",
"tags": [],
"uuid": "4223f3fa-eeae-6a96-6af5-d68dc94199f3",
"vm_network": {
"00:50:56:a3:b1:e9": {
"ipv4": [
"192.168.1.47"
],
"ipv6": [
"fe80::250:56ff:fea3:b1e9"
]
}
}
},
{
"attributes": {},
"cluster": "Storage",
"datacenter": "VMFStorage",
"esxi_hostname": "161.92.211.22",
"folder": "/VMFStorage/vm/",
"guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
"guest_name": "Machine2",
"ip_address": "192.168.1.29",
"mac_address": [
"00:50:56:a3:b3:6d"
],
"moid": "vm-21360",
"power_state": "poweredOff",
"tags": [],
"uuid": "42239cd1-69ec-ff5d-a557-85d0bc8c9edc",
"vm_network": {
"00:50:56:a3:b3:6d": {
"ipv4": [],
"ipv6": []
}
}
},
所需的输出是捕获 Machine1 来宾名称的 IP 地址并打印该输出
192.168.1.29
我尝试了很多可能性,但找不到合适的解决方案 任何帮助将不胜感激
更新
- hosts: myhost
tasks:
- name: Gather all VMs from a specific folder
community.vmware.vmware_vm_info:
hostname: myhost.com
username: local
password: mypass
folder: "/VMFStorage/vm/"
validate_certs: False
delegate_to: localhost
register: vm_info
- set_fact:
ip: "{{ vm_info.virtual_machines|
selectattr('guest_name', 'eq', 'AlmaLinuxBUILD-Machine')|
map(attribute='ip_address')|first }}"
- debug:
var: ip
试试这个.. 如果你只想要 machine1 ip_address.
- debug:
msg: "{{ vm_info.virtual_machines[1].ip_address }}"
试试这个..如果你只想要所有 VM ip_addresses.
- debug:
msg: "{{ vm_info.virtual_machines[*].ip_address }}"
有很多选择。例如,
- Select 首先从列表中提取字典并提取属性
- debug:
var: ip
vars:
ip: "{{ vm_info.virtual_machines|
selectattr('guest_name', 'eq', 'Machine2')|
map(attribute='ip_address')|first }}"
给予
ip: 192.168.1.29
- 如果您更喜欢json_query,下面的任务会给出相同的结果
- debug:
var: ip
vars:
ip: "{{ vm_info.virtual_machines|
json_query('[?guest_name==`Machine2`].ip_address')|first }}"
- 如果列表中有很多项目需要引用,请先创建字典,例如
- debug:
var: name_ip
vars:
name_ip: "{{ vm_info.virtual_machines|
items2dict(key_name='guest_name',
value_name='ip_address') }}"
给予
name_ip:
Machine2: 192.168.1.29
RHEL Machine: 192.168.1.47
然后,使用这个字典来获取客人的IP。下面的任务也给出了相同的结果
- debug:
var: ip
vars:
ip: "{{ name_ip['Machine2'] }}"
问:“如何在同一个剧本的其他地方使用ip变量?”
答:有many options。比如放入set_facts(优先级19)
- set_facts:
ip: "{{ name_ip['Machine2'] }}"
- debug:
var: ip