gpt4 book ai didi

ANSIBLE : Access "deep" variables in YAML inventory

转载 作者:行者123 更新时间:2023-12-04 10:45:02 30 4
gpt4 key购买 nike

我有以下库存:

all:
children:
# Declare the front container group
srv0:
hosts:
ocp-prod-srv0:
ansible_host: 123.123.123.123
# Declare the database and file container group
srv1:
hosts:
ocp-prod-srv1:
ansible_host: 192.168.200.3
webapis:
hosts:
ocp-prod-srv2:
ansible_host: 192.168.200.4
# Declare the application container group
webapps:
hosts:
ocp-prod-srv3:
ansible_host: 192.168.200.5
# The app instance on this host will execute the batch
is_worker: true
ocp-prod-srv4:
ansible_host: 192.168.200.6
# The app instance on this host will execute the batch
is_worker: false
# Declare the forge where the webapp binaries are built
jenkins:
hosts:
localhost:

# The proxmox containers. Necessary to configure the proxmox, provision the containers and use as a jump
proxmox_cluster:
hosts:
proxmox:
# IP of the proxmox ** master ** (to manage proxmox clustering)
ansible_host: 141.141.141.141
vars:
# Name of the proxmox node to be used to create the containers
node_name: prxmxsrv1
host_name: 141.141.141.141
# Declaration of network interfaces to use for the environment
proxmox_bridges:
- name: vmbr200
mask: 255.255.255.0
subnet: 192.168.200.0/24
address: 192.168.200.1
subnet_bits: 24

# Define which hosts need to go through the jump to be accessed by Ansible
gatewayed:
hosts:
ocp-prod-srv1:
ocp-prod-srv2:
ocp-prod-srv3:
ocp-prod-srv4:

# Define the configuration parameters of each container (id, name, HD, RAM, inets, etc...)
vm:
hosts:
ocp-prod-srv0:
vm_id: 2002
vm_name: ocp-prod-srv0
netif:
net0:
name: eth0
bridge: vmbr0
ip: 123.123.123.123/32
# Virtual KVM mac adress, configured on provider's IP failover
hwaddr: 52:54:00:00:ba:cc
# Public IP of physical server
gw: 141.141.141.1
net1:
name: eth1
bridge: vmbr200
# The private IP
ip: 192.168.200.2/24
# Max number of cores that can be used
cores: 4
# Max load of the proxmox CPUs (by core % : 2.5 = 2.5 cores)
cpus: 4
diskspace: 10
memory: 2048

ocp-prod-srv1:
vm_id: 2003
vm_name: ocp-prod-srv1
netif:
net0:
name: eth0
bridge: vmbr200
gw: 192.168.200.2
ip: 192.168.200.3/24
# Max number of cores that can be used
cores: 6
# Max load of the proxmox CPUs (by core % : 2.5 = 2.5 cores)
cpus: 4
diskspace: 600
memory: 49152

我想访问这里提到的接口(interface)名称:“all->children->vm->hosts->XX->netif->YY->name->THIS”

我尝试了几十个在这里或其他地方找到的代码。从来没有得到我想要的东西。

这是我做的最“高级”的代码:
- debug:
msg: "DEBUG : {{ hostvars[item]['netif'] }}"
with_items:
- "{{ groups['vm'] }}"

这给了我以下结果:
TASK [commons/iptables : debug] *******************************************************************************************************************************************************************************************************************************************
ok: [proxmox] => (item=ocp-prod-srv3) => {
"msg": "DEBUG : {u'net0': {u'gw': u'192.168.200.2', u'bridge': u'vmbr200', u'name': u'eth0', u'ip': u'192.168.200.5/24'}}"
}
ok: [proxmox] => (item=ocp-prod-srv0) => {
"msg": "DEBUG : {u'net1': {u'bridge': u'vmbr200', u'name': u'eth1', u'ip': u'192.168.200.2/24'}, u'net0': {u'hwaddr': u'52:54:00:00:ba:cc', u'bridge': u'vmbr0', u'name': u'eth0', u'gw': u'141.141.141.1', u'ip': u'123.123.123.123'}}"
}
ok: [proxmox] => (item=ocp-prod-srv4) => {
"msg": "DEBUG : {u'net0': {u'gw': u'192.168.200.2', u'bridge': u'vmbr200', u'name': u'eth0', u'ip': u'192.168.200.6/24'}}"
}
ok: [proxmox] => (item=ocp-prod-srv1) => {
"msg": "DEBUG : {u'net0': {u'gw': u'192.168.200.2', u'bridge': u'vmbr200', u'name': u'eth0', u'ip': u'192.168.200.3/24'}}"
}
ok: [proxmox] => (item=ocp-prod-srv2) => {
"msg": "DEBUG : {u'net0': {u'gw': u'192.168.200.2', u'bridge': u'vmbr200', u'name': u'eth0', u'ip': u'192.168.200.4/24'}}"
}

如何打印主机的 net1 的“名称”(如果存在)?

最佳答案

Q: "How can I print the "name" of net1 of a host (if it exists) ?"



答:试试
- debug:
msg: "DEBUG : {{ hostvars[item]['netif']['net1']['name'] }}"
loop: "{{ groups['vm'] }}"
when: hostvars[item]['netif']['net1']['name'] is defined

关于ANSIBLE : Access "deep" variables in YAML inventory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59743411/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com