Ansible vmware_vm_inventory:如何访问动态主机变量?
Ansible vmware_vm_inventory: How to access dynamic host variables?
我正在使用 Ansible 2.9,我的最终目标是创建所有 运行 带有标签 test[=32= 的虚拟机的快照].
但我似乎无法访问 name
以外的变量
VMware 清单
plugin: vmware_vm_inventory
strict: False
hostname: < hostname >
username: < username >
password: < password >
validate_certs: False
with_tags: True
剧本
---
- name: Create snapshot for running vm's with tag - test
hosts: test
gather_facts: False
vars_files:
- vcenter-vars.yml
tasks:
- debug:
var: vars
- debug:
var: guest.guestState
- name: Make Snapshot
vmware_guest_snapshot:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
datacenter: "{{ datacenter_name }}"
name: "{{ name }}"
folder: .
state: present
snapshot_name: snap1
description: snap1_description
validate_certs: no
delegate_to: localhost
when: guest.guestState == "running"
调试输出
PLAY [Create snapshot for running vm's with tag - test] ********************************************************************************************************************************************************************************************************************************************
TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************************
ok: [UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638] => {
"vars": {
"ansible_check_mode": false,
"ansible_dependent_role_names": [],
"ansible_diff_mode": false,
"ansible_facts": {},
"ansible_forks": 5,
"ansible_host": "10.32.112.10",
"ansible_inventory_sources": [
"/home/usrmainadmin/vmware_test/test.vmware.yml"
],
"ansible_play_batch": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"ansible_play_hosts": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"ansible_play_hosts_all": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"ansible_play_name": "Create snapshot for running vm's with tag - test",
"ansible_play_role_names": [],
"ansible_playbook_python": "/usr/bin/python3.6",
"ansible_role_names": [],
"ansible_run_tags": [
"all"
],
"ansible_skip_tags": [],
"ansible_verbosity": 0,
"ansible_version": {
"full": "2.9.7",
"major": 2,
"minor": 9,
"revision": 7,
"string": "2.9.7"
},
"config.cpuHotAddEnabled": false,
"config.cpuHotRemoveEnabled": false,
"config.hardware.numCPU": 4,
"config.instanceUuid": "503ec98c-2c65-c8ef-c8d4-cf7dd86958ec",
"config.name": "UniFi Controller",
"config.template": false,
"datacenter_name": "Global-e",
"environment": [],
"group_names": [
"poweredOn",
"test",
"ubuntu64Guest"
],
"groups": {
### A lot of groups
},
"guest.guestId": "ubuntu64Guest",
"guest.guestState": "running",
"guest.hostName": "unifi-controller",
"guest.ipAddress": "10.32.112.10",
"hostvars": {
## A huge list of all found vm's found by the inventory (also one's without the tag)
},
"inventory_dir": "/home/usrmainadmin/vmware_test",
"inventory_file": "/home/usrmainadmin/vmware_test/test.vmware.yml",
"inventory_hostname": "UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638",
"inventory_hostname_short": "UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638",
"name": "UniFi Controller",
"omit": "__omit_place_holder__244f702061bd95e53eba844e6bbce396f8033152",
"play_hosts": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"playbook_dir": "/home/usrmainadmin/vmware_test",
"role_names": [],
"runtime.maxMemoryUsage": 8192,
"vcenter_pass": < password >,
"vcenter_server": < hostname >",
"vcenter_user": "< username >"
}
}
TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************************
ok: [UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638] => {
"guest.guestState": "VARIABLE IS NOT DEFINED!"
}
TASK [Make Snapshot] *******************************************************************************************************************************************************************************************************************************************************************************
fatal: [UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638]: FAILED! => {"msg": "The conditional check 'guest.guestState == \"running\"' failed. The error was: error while evaluating conditional (guest.guestState == \"running\"): 'guest' is undefined\n\nThe error appears to be in '/home/usrmainadmin/vmware_test/test-vcenter.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: guest.guestState\n - name: Make Snapshot\n ^ here\n"}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************
UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
我可以通过变量获取虚拟机的名称:名称,但我还需要状态来确定虚拟机是 运行 还是 [=24] =]guest.guestState 返回未定义。
我如何访问这些变量?
因为 vars 中有一个特殊字符,jinja2 通常为成员访问保留该字符,因此您需要对该 var 使用 dict 键语法:
- debug:
msg: guest state = {{ vars["guest.guestState"] }}
我正在使用 Ansible 2.9,我的最终目标是创建所有 运行 带有标签 test[=32= 的虚拟机的快照]. 但我似乎无法访问 name
以外的变量VMware 清单
plugin: vmware_vm_inventory
strict: False
hostname: < hostname >
username: < username >
password: < password >
validate_certs: False
with_tags: True
剧本
---
- name: Create snapshot for running vm's with tag - test
hosts: test
gather_facts: False
vars_files:
- vcenter-vars.yml
tasks:
- debug:
var: vars
- debug:
var: guest.guestState
- name: Make Snapshot
vmware_guest_snapshot:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
datacenter: "{{ datacenter_name }}"
name: "{{ name }}"
folder: .
state: present
snapshot_name: snap1
description: snap1_description
validate_certs: no
delegate_to: localhost
when: guest.guestState == "running"
调试输出
PLAY [Create snapshot for running vm's with tag - test] ********************************************************************************************************************************************************************************************************************************************
TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************************
ok: [UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638] => {
"vars": {
"ansible_check_mode": false,
"ansible_dependent_role_names": [],
"ansible_diff_mode": false,
"ansible_facts": {},
"ansible_forks": 5,
"ansible_host": "10.32.112.10",
"ansible_inventory_sources": [
"/home/usrmainadmin/vmware_test/test.vmware.yml"
],
"ansible_play_batch": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"ansible_play_hosts": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"ansible_play_hosts_all": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"ansible_play_name": "Create snapshot for running vm's with tag - test",
"ansible_play_role_names": [],
"ansible_playbook_python": "/usr/bin/python3.6",
"ansible_role_names": [],
"ansible_run_tags": [
"all"
],
"ansible_skip_tags": [],
"ansible_verbosity": 0,
"ansible_version": {
"full": "2.9.7",
"major": 2,
"minor": 9,
"revision": 7,
"string": "2.9.7"
},
"config.cpuHotAddEnabled": false,
"config.cpuHotRemoveEnabled": false,
"config.hardware.numCPU": 4,
"config.instanceUuid": "503ec98c-2c65-c8ef-c8d4-cf7dd86958ec",
"config.name": "UniFi Controller",
"config.template": false,
"datacenter_name": "Global-e",
"environment": [],
"group_names": [
"poweredOn",
"test",
"ubuntu64Guest"
],
"groups": {
### A lot of groups
},
"guest.guestId": "ubuntu64Guest",
"guest.guestState": "running",
"guest.hostName": "unifi-controller",
"guest.ipAddress": "10.32.112.10",
"hostvars": {
## A huge list of all found vm's found by the inventory (also one's without the tag)
},
"inventory_dir": "/home/usrmainadmin/vmware_test",
"inventory_file": "/home/usrmainadmin/vmware_test/test.vmware.yml",
"inventory_hostname": "UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638",
"inventory_hostname_short": "UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638",
"name": "UniFi Controller",
"omit": "__omit_place_holder__244f702061bd95e53eba844e6bbce396f8033152",
"play_hosts": [
"UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638"
],
"playbook_dir": "/home/usrmainadmin/vmware_test",
"role_names": [],
"runtime.maxMemoryUsage": 8192,
"vcenter_pass": < password >,
"vcenter_server": < hostname >",
"vcenter_user": "< username >"
}
}
TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************************
ok: [UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638] => {
"guest.guestState": "VARIABLE IS NOT DEFINED!"
}
TASK [Make Snapshot] *******************************************************************************************************************************************************************************************************************************************************************************
fatal: [UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638]: FAILED! => {"msg": "The conditional check 'guest.guestState == \"running\"' failed. The error was: error while evaluating conditional (guest.guestState == \"running\"): 'guest' is undefined\n\nThe error appears to be in '/home/usrmainadmin/vmware_test/test-vcenter.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: guest.guestState\n - name: Make Snapshot\n ^ here\n"}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************
UniFi Controller_423eea90-9d28-d823-a22e-d98eca9c9638 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
我可以通过变量获取虚拟机的名称:名称,但我还需要状态来确定虚拟机是 运行 还是 [=24] =]guest.guestState 返回未定义。 我如何访问这些变量?
因为 vars 中有一个特殊字符,jinja2 通常为成员访问保留该字符,因此您需要对该 var 使用 dict 键语法:
- debug:
msg: guest state = {{ vars["guest.guestState"] }}