如何解决使用Ansible从多个citrix管理程序vm获取IP地址列表
我正在使用ansible和xenserver_guest_info模块从citrix虚拟机管理程序服务器上的多个虚拟机创建IP地址列表。目前,我只想打印ip列表,但是将这些ip存储在ansible列表中会更好。我计划遍历此ip列表,并使用ansible在所有这些vm上运行命令。
这是我目前的剧本。它将循环遍历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','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,'name': 'Ubuntu 20 2','uuid': 'b087832e-81f1-c091-1363-8b8ba8442c8e','mac': 'e6:cd:ca:ff:c3:e0','mac_seed': '3c56a628-0f68-34f9-fe98-4bf2214a5891','item': 'Ubuntu 20 2',"ip": "192.168.0.3","mac": "e6:cd:ca:ff:c3:e0","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地址在VM上做一些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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。