首页 > 解决方案 > 使用 ansible 从多个 citrix 虚拟机管理程序 vm 中获取 ip 地址列表

问题描述

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

这是我目前的ansible剧本。它遍历 xenserver 模块返回的字典输出,试图提取 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 列表中以供以后使用。任何帮助将非常感激。

标签: ansiblecitrixxenhypervisor

解决方案


下面的任务

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

应该给

  ips:
  - 192.168.0.2
  - 192.168.0.3

推荐阅读