使用 ansible 从多个 citrix hypervisors vms 获取 IP 地址列表

Get list of ip addesses from multiple citrix hypervisors vm's using ansible

我正在使用 ansible 和 xenserver_guest_info 模块从我的 citrix hypervisor 服务器上的多个虚拟机创建一个 IP 地址列表。目前我只想打印一个 ip 列表,但将这些 ip 存储在一个 ansible 列表中会更好。我计划使用 ansible 在所有这些 vm 上循环遍历此 ip 列表和 运行 命令。

这是我目前的 ansible 剧本。它循环遍历 xenserver 模块 returns 的字典输出,试图提取 ip 地址。:

- name: Manage VMs
  connection: local
  hosts: localhost

  # Hypervisor server info from vars file
  vars_files:
    - xen_vars.yml

  tasks:

  - name: Gather facts
    xenserver_guest_info:
      hostname: "{{ xen_address}}"
      username: "{{ admin_username }}"
      password: "{{ admin_password }}"
      name: "{{ item }}"
    loop: "{{ xen_machines }}"
    register: facts

  # - name: Get IP's of VM's

  - debug:
      msg: "{{item.instance.networks}}" 
    loop: "{{facts.results}}"

它会在我的服务器上给出两个虚拟机的列表时产生以下输出:

PLAY [Manage VMs] **************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Gather facts] ************************************************************
ok: [localhost] => (item=Ubuntu 20)
ok: [localhost] => (item=Ubuntu 20 2)

TASK [debug] *******************************************************************
ok: [localhost] => (item={'failed': False, 'changed': False, 'instance': {'state': 'poweredoff', 'name': 'Ubuntu 20', 'name_desc': '', 'uuid': 'cf5db672-67cf-7e8c-6951-f5959ab62e26', 'is_template': False, 'folder': '', 'hardware': {'num_cpus': 1, 'num_cpu_cores_per_socket': 1, 'memory_mb': 1024}, 'disks': [{'size': 21474836480, 'name': 'Ubuntu 20 0', 'name_desc': 'Created by template provisioner', 'sr': 'Local storage', 'sr_uuid': 'd7bb817b-281e-fd9c-33a3-54db8935d596', 'os_device': 'xvda', 'vbd_userdevice': '0'}], 'cdrom': {'type': 'iso', 'iso_name': 'ubuntu-20.04.1-desktop-amd64.iso'}, 'networks': [{'name': 'Pool-wide network associated with eth0', 'mac': 'a2:07:be:29:5f:ad', 'vif_device': '0', 'mtu': '1500', 'ip': '', 'prefix': '', 'netmask': '', 'gateway': '', 'ip6': [], 'prefix6': '', 'gateway6': ''}], 'home_server': 'citrix-mwyqyqaa', 'domid': '-1', 'platform': {'timeoffset': '0', 'videoram': '8', 'hpet': 'true', 'secureboot': 'false', 'device-model': 'qemu-upstream-compat', 'apic': 'true', 'device_id': '0001', 'vga': 'std', 'nx': 'true', 'pae': 'true', 'viridian': 'false', 'acpi': '1'}, 'other_config': {'base_template_name': 'Ubuntu Focal Fossa 20.04', 'import_task': 'OpaqueRef:3b6061a0-a204-4ed4-be20-842a359a70fa', 'mac_seed': 'f6ae87b5-2f00-0717-559e-8b624fe92f35', 'install-methods': 'cdrom,nfs,http,ftp', 'linux_template': 'true'}, 'xenstore_data': {'vm-data': '', 'vm-data/mmio-hole-size': '268435456'}, 'customization_agent': 'custom'}, 'invocation': {'module_args': {'hostname': '192.168.0.187', 'username': 'root', 'password': 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', 'name': 'Ubuntu 20', 'validate_certs': True, 'uuid': None}}, 'item': 'Ubuntu 20', 'ansible_loop_var': 'item'}) => {
    "msg": [
        {
            "gateway": "",
            "gateway6": "",
            "ip": "192.168.0.2",
            "ip6": [],
            "mac": "a2:07:be:29:5f:ad",
            "mtu": "1500",
            "name": "Pool-wide network associated with eth0",
            "netmask": "",
            "prefix": "",
            "prefix6": "",
            "vif_device": "0"
        }
    ]
}
ok: [localhost] => (item={'failed': False, 'changed': False, 'instance': {'state': 'poweredoff', 'name': 'Ubuntu 20 2', 'name_desc': '', 'uuid': 'b087832e-81f1-c091-1363-8b8ba8442c8e', 'is_template': False, 'folder': '', 'hardware': {'num_cpus': 1, 'num_cpu_cores_per_socket': 1, 'memory_mb': 1024}, 'disks': [{'size': 21474836480, 'name': 'Ubuntu 20 0', 'name_desc': 'Created by template provisioner', 'sr': 'Local storage', 'sr_uuid': 'd7bb817b-281e-fd9c-33a3-54db8935d596', 'os_device': 'xvda', 'vbd_userdevice': '0'}], 'cdrom': {'type': 'iso', 'iso_name': 'ubuntu-20.04.1-desktop-amd64.iso'}, 'networks': [{'name': 'Pool-wide network associated with eth0', 'mac': 'e6:cd:ca:ff:c3:e0', 'vif_device': '0', 'mtu': '1500', 'ip': '', 'prefix': '', 'netmask': '', 'gateway': '', 'ip6': [], 'prefix6': '', 'gateway6': ''}], 'home_server': 'citrix-mwyqyqaa', 'domid': '-1', 'platform': {'timeoffset': '0', 'videoram': '8', 'hpet': 'true', 'secureboot': 'false', 'device-model': 'qemu-upstream-compat', 'apic': 'true', 'device_id': '0001', 'vga': 'std', 'nx': 'true', 'pae': 'true', 'viridian': 'false', 'acpi': '1'}, 'other_config': {'base_template_name': 'Ubuntu Focal Fossa 20.04', 'import_task': 'OpaqueRef:3b6061a0-a204-4ed4-be20-842a359a70fa', 'mac_seed': '3c56a628-0f68-34f9-fe98-4bf2214a5891', 'install-methods': 'cdrom,nfs,http,ftp', 'linux_template': 'true'}, 'xenstore_data': {'vm-data': '', 'vm-data/mmio-hole-size': '268435456'}, 'customization_agent': 'custom'}, 'invocation': {'module_args': {'hostname': '192.168.0.187', 'username': 'root', 'password': 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', 'name': 'Ubuntu 20 2', 'validate_certs': True, 'uuid': None}}, 'item': 'Ubuntu 20 2', 'ansible_loop_var': 'item'}) => {
    "msg": [
        {
            "gateway": "",
            "gateway6": "",
            "ip": "192.168.0.3",
            "ip6": [],
            "mac": "e6:cd:ca:ff:c3:e0",
            "mtu": "1500",
            "name": "Pool-wide network associated with eth0",
            "netmask": "",
            "prefix": "",
            "prefix6": "",
            "vif_device": "0"
        }
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

我尝试只访问 ip,但它一直无法正常工作,因为它存储在列表中。

同样,我的最终目标是让 ansible 吐出一个 ip 列表,如下所示:

192.168.0.2
192.168.0.3

或者更好的是将它们存储在一个 ansible 列表中供以后使用。任何帮助将不胜感激。

如果您遇到的唯一真正问题是 IP 在列表中,并且您知道(由于您配置 VM 的方式)列表总是只有一个条目,那么只需获取第一项在列表中:

item.instance.networks[0].ip

如果你想使用这些 IP 地址在虚拟机上做一些 Ansible 工作,我建议你使用 add_host 为你的剧本中的第二个游戏构建一个新的清单:

- name: Manage VMs
  connection: local
  hosts: localhost

  # Hypervisor server info from vars file
  vars_files:
    - xen_vars.yml

  tasks:

  - name: Gather facts
    xenserver_guest_info:
      hostname: "{{ xen_address}}"
      username: "{{ admin_username }}"
      password: "{{ admin_password }}"
      name: "{{ item }}"
    loop: "{{ xen_machines }}"
    register: facts

  # - name: Get IP's of VM's

  - debug:
      msg: "{{item.instance.networks[0].ip}}" 
    loop: "{{facts.results}}"

  - name: Build inventory of VMs
    add_host:
      name: "{{ item.instance.networks[0].ip }}"
      groups: vms
      # You can add other variables per-host here if you want
    loop: "{{ xen_machines }}"
    loop_control:
      label: "{{ item.instance.name }}"

- name: Do stuff directly to the VMs
  hosts: vms  # This is the group you just created
  connection: ssh

  tasks:
    - debug:
        msg: "{{ Hello from a VM }}"

下面的任务

    - set_fact:
        ips: "{{ facts.results|
                 json_query('[].instance.networks[].ip') }}"
    - debug:
        var: ips

应该给

  ips:
  - 192.168.0.2
  - 192.168.0.3