I am using Ansible 2.9 and my end goal is to create a snapshot of all running vm's with the tag test. But i cant seem to access variables other than name
VMware inventory
plugin: vmware_vm_inventory
strict: False
hostname: < hostname >
username: < username >
password: < password >
validate_certs: False
with_tags: True
Playbook
---
- 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"
Debug Output
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
I can get the name of the vm with the variable: name, but i also need the state to determine if the vm is running and guest.guestState returned undefined. How can i access those variables?
Because the vars has a special character in it that jinja2 normally reserves for member access, you'll need to use the dict key syntax for that var:
- debug:
msg: guest state = {{ vars["guest.guestState"] }}