ANSIBLE:Access“;“深”;YAML库存中的变量

ANSIBLE:Access“;“深”;YAML库存中的变量,ansible,yaml,Ansible,Yaml,我有以下清单: 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:

我有以下清单:

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
我想访问这里提到的接口名称:“所有->子->虚拟机->主机->XX->网络->YY->名称->此”

我在这里或其他地方找到了几十个密码。我一直没能得到我想要的

以下是我编写的最“高级”代码:

- 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的“名称”(如果存在)

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

A:试试看

- debug:
    msg: "DEBUG : {{ hostvars[item]['netif']['net1']['name'] }}"
  loop: "{{ groups['vm'] }}"
  when: hostvars[item]['netif']['net1']['name'] is defined
这太完美了。我不能“投票”你的答案(我的分数很低),但这正是我想要的。