【问题标题】:Get list of ip addesses from multiple citrix hypervisors vm's using ansible使用 ansible 从多个 citrix 虚拟机管理程序 vm 获取 IP 地址列表
【发布时间】:2020-09-13 19:58:23
【问题描述】:

我正在使用 ansible 和 xenserver_guest_info 模块从我的 citrix hypervisor 服务器上的多个虚拟机创建一个 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 列表中以供以后使用。任何帮助将不胜感激。

【问题讨论】:

  • 您说“我尝试仅访问该 ip”,但您没有显示您是如何尝试访问它的。您还注意到它们存储在一个列表中 - 如果您的一个虚拟机有多个网络接口(因此有多个 IP 地址),您想做什么?
  • @lxop 我设置了虚拟机,它们都有一个网络接口。至于访问 ip,我的意思是我在我的 yaml 剧本中做了一个“item.instance.networks.ip”,这引发了一个错误。那是因为 ip 字段存储在一个也需要遍历的列表中。我真的不知道如何在 ansible 中解决这个问题。

标签: ansible citrix xen hypervisor


【解决方案1】:

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

item.instance.networks[0].ip

如果您想使用这些 IP 地址在 VM 上执行一些 Ansible 工作,我建议您使用 add_host 为您的 playbook 中的第二次播放构建一个新清单:

- 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 }}"

【讨论】:

    【解决方案2】:

    下面的任务

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

    应该给

      ips:
      - 192.168.0.2
      - 192.168.0.3
    

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多