在使用 Ansible 和 vmware_vm_info 分配 IP 地址之前如何重试?
How to retry until a IP address is assigned with Ansible and vmware_vm_info?
我想重试,直到我得到 ip_address: 与 vm_info: 主机模块 (guest_name:)。问题是有时配置的虚拟机没有启动,因此我无法获得 IP 地址
问题:
我应该如何定义我的角色,重复直到 ip_address 被定义?
---
- name: Get virtual machine info
vmware_vm_info:
hostname: '{{ vsphere_host }}'
username: '{{ vsphere_user }}'
password: '{{ vsphere_password }}'
validate_certs: false
folder: '/{{ vsphere_datacenter }}/vm'
delegate_to: localhost
register: vm_info
- debug:
var: vm_info
- name: "Update host_var with new IP Address"
set_fact:
ansible_host: "{{ item.ip_address }}"
ansible_hostname: "{{ item.guest_name }}"
regexp: "^ansible_host:"
with_items:
- "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='{{ hostname }}']"
delegate_to: localhost
这是寄存器vm_info的调试:
ok: [1-Europe-Cisco-Site1-2-pci] =>
vm_info:
changed: false
failed: false
virtual_machines:
- attributes: {}
cluster: null
esxi_hostname: 51.89.43.64
guest_fullname: Other 3.x Linux (64-bit)
guest_name: 1-Europe-Cisco-Site2-2-guest
ip_address: ''
mac_address:
- 00:50:56:91:41:b7
- 00:50:56:91:80:67
- 00:50:56:91:f2:a3
power_state: poweredOn
tags: []
uuid: 42110cfc-0b31-5de2-659d-6f8fe47e8136
vm_network: {}
- attributes: {}
cluster: null
esxi_hostname: 51.89.43.64
guest_fullname: Other 3.x Linux (64-bit)
guest_name: 1-Europe-Cisco-Site1-2-guest
ip_address: ''
mac_address:
- 00:50:56:91:07:e1
- 00:50:56:91:b0:79
- 00:50:56:91:d4:5a
power_state: poweredOn
tags: []
uuid: 4211db9d-5608-14fd-1c9b-6cfc470c29c7
vm_network: {}
- attributes: {}
cluster: null
esxi_hostname: 51.89.43.64
guest_fullname: Other 3.x Linux (64-bit)
guest_name: 1-Europe-Cisco-Site2-1-guest
ip_address: ''
mac_address:
- 00:50:56:91:89:ac
- 00:50:56:91:12:c7
- 00:50:56:91:e3:0b
- 00:50:56:91:de:b3
power_state: poweredOn
tags: []
uuid: 421126d9-e74a-2db2-b58c-0a09571d9b29
vm_network: {}
Until 只适用于一个任务,但我需要在这里定义 2 个任务:
1.get vm_info
2.loop 到 vm_info 并定义 host_ip
像下面这样使用 vmware_guest_info 模块怎么样?
在以下剧本中,vmware_guest_info 将一直执行,直到使用架构和属性选项从虚拟机获取 IP 地址。
---
- name: Example Playbook
hosts: localhost
gather_facts: false
tasks:
- name: Operate a virtual machine to powered on
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
datacenter: "{{ datacenter_name }}"
name: "{{ vm_name }}"
state: poweredon
- name: Gather a virtual machine info
vmware_guest_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
datacenter: "{{ datacenter_name }}"
name: "{{ vm_name }}"
schema: vsphere
properties:
- guest.ipAddress
retries: 60
delay: 10
until: gather_vm_info.instance.guest.ipAddress is not none
register: gather_vm_info
- debug: var=gather_vm_info
我想重试,直到我得到 ip_address: 与 vm_info: 主机模块 (guest_name:)。问题是有时配置的虚拟机没有启动,因此我无法获得 IP 地址
问题:
我应该如何定义我的角色,重复直到 ip_address 被定义?
---
- name: Get virtual machine info
vmware_vm_info:
hostname: '{{ vsphere_host }}'
username: '{{ vsphere_user }}'
password: '{{ vsphere_password }}'
validate_certs: false
folder: '/{{ vsphere_datacenter }}/vm'
delegate_to: localhost
register: vm_info
- debug:
var: vm_info
- name: "Update host_var with new IP Address"
set_fact:
ansible_host: "{{ item.ip_address }}"
ansible_hostname: "{{ item.guest_name }}"
regexp: "^ansible_host:"
with_items:
- "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='{{ hostname }}']"
delegate_to: localhost
这是寄存器vm_info的调试:
ok: [1-Europe-Cisco-Site1-2-pci] =>
vm_info:
changed: false
failed: false
virtual_machines:
- attributes: {}
cluster: null
esxi_hostname: 51.89.43.64
guest_fullname: Other 3.x Linux (64-bit)
guest_name: 1-Europe-Cisco-Site2-2-guest
ip_address: ''
mac_address:
- 00:50:56:91:41:b7
- 00:50:56:91:80:67
- 00:50:56:91:f2:a3
power_state: poweredOn
tags: []
uuid: 42110cfc-0b31-5de2-659d-6f8fe47e8136
vm_network: {}
- attributes: {}
cluster: null
esxi_hostname: 51.89.43.64
guest_fullname: Other 3.x Linux (64-bit)
guest_name: 1-Europe-Cisco-Site1-2-guest
ip_address: ''
mac_address:
- 00:50:56:91:07:e1
- 00:50:56:91:b0:79
- 00:50:56:91:d4:5a
power_state: poweredOn
tags: []
uuid: 4211db9d-5608-14fd-1c9b-6cfc470c29c7
vm_network: {}
- attributes: {}
cluster: null
esxi_hostname: 51.89.43.64
guest_fullname: Other 3.x Linux (64-bit)
guest_name: 1-Europe-Cisco-Site2-1-guest
ip_address: ''
mac_address:
- 00:50:56:91:89:ac
- 00:50:56:91:12:c7
- 00:50:56:91:e3:0b
- 00:50:56:91:de:b3
power_state: poweredOn
tags: []
uuid: 421126d9-e74a-2db2-b58c-0a09571d9b29
vm_network: {}
Until 只适用于一个任务,但我需要在这里定义 2 个任务: 1.get vm_info 2.loop 到 vm_info 并定义 host_ip
像下面这样使用 vmware_guest_info 模块怎么样?
在以下剧本中,vmware_guest_info 将一直执行,直到使用架构和属性选项从虚拟机获取 IP 地址。
---
- name: Example Playbook
hosts: localhost
gather_facts: false
tasks:
- name: Operate a virtual machine to powered on
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
datacenter: "{{ datacenter_name }}"
name: "{{ vm_name }}"
state: poweredon
- name: Gather a virtual machine info
vmware_guest_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
datacenter: "{{ datacenter_name }}"
name: "{{ vm_name }}"
schema: vsphere
properties:
- guest.ipAddress
retries: 60
delay: 10
until: gather_vm_info.instance.guest.ipAddress is not none
register: gather_vm_info
- debug: var=gather_vm_info