Reputation: 1182
In vars/main.yml
I have this:
user_data:
service:
name: hello1
description: abc
update:
name: hello
version: 1
In playbook I try to write when condition:
- name: "stop all"
systemd:
name: "does not matter.service"
state: stopped
when: user_data.service.update is defined
It works.
When i write this task with update
in when condition:
- name: "stop all"
systemd:
name: "does not matter.service"
state: stopped
when: user_data.service.update.name is defined
It is skipped
All variables are setted in vars/main.yml
or passed using extra var as a JSON.
What's wrong?
Upvotes: 1
Views: 3395
Reputation: 44615
update
is actually a reserved word (i.e. built-in method of dict object). See the following playbook. You can define an update
hash key in yaml but I did not find any way to retrieve it back correctly. The only solution I see at this point (besides firing a ticket on github ansible project) is to rename your hash key to something else.
The following playbook
---
- name: Debug that tricky thing
hosts: localhost
gather_facts: false
vars:
user_data:
service:
name: hello1
description: abc
update:
name: hello
version: 1
tasks:
- name: Show the full var
debug:
var: user_data
- name: Try to how the update hash test1
debug:
var: user_data.update
- name: Try to how the update hash test2
debug:
var: user_data["update"]
Results in
PLAY [Debug that tricky thing] ******************************************************************************************************************************************************************************************************************************************
TASK [Show the full var] ************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"user_data": {
"service": {
"description": "abc",
"name": "hello1",
"update": {
"name": "hello",
"version": 1
}
}
}
}
TASK [Try to how the update hash test1] ***************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"user_data.update": "<built-in method update of dict object at 0x7fc5d9256630>"
}
TASK [Try to how the update hash test2] ***********************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"user_data[\"update\"]": "<built-in method update of dict object at 0x7fc5d9256630>"
}
PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 1
Reputation: 21
The issue here is python interpreter does not understand the object format in the main.yml file. Convert that object in a format that can be understood by the interpreter. Usually, python objects are represented in the form of a dictionary, so you could convert it to something like this:
user_data = {'service': {'attr': 1, 'some_a': 'abc'}, 'update':{'name: 'hello', 'version': 1} }
Upvotes: 0