Reputation: 3654
If I store a string into an ansible register, and then have other tasks run only when that register contains a certain value, they are always run, regardless of the value in the register.
Why is ansible running tasks when a conditional should fail?
This is a simple example. It should only debug once (as test_foo.stdout == "Foo"
) but instead it will print both debug statements:
- name: foo
command: "echo \"Foo\""
register: test_foo
tags: [ 'foo-test' ]
- debug:
var: test_foo
when: test_foo.stdout == "Foo",
tags: [ 'foo-test' ]
- debug:
var: test_foo
when: test_foo.stdout == "Bar",
tags: [ 'foo-test' ]
Just to note, this also happens when another command call is triggered with the same conditional, not just debug calls. It will always execute both tasks.
Here's the output:
TASK [test : foo] ***********************************************************************************************************************************************************************************
changed: [localServer -> localhost] => {"changed": true, "cmd": ["echo", "Foo"], "delta": "0:00:01.002745", "end": "2020-11-17 11:22:25.733089", "rc": 0, "start": "2020-11-17 11:22:24.730344", "stderr": "", "stderr_lines": [], "stdout": "Foo", "stdout_lines": ["Foo"]}
TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:01.002745",
"end": "2020-11-17 11:22:25.733089",
"failed": false,
"rc": 0,
"start": "2020-11-17 11:22:24.730344",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:01.002745",
"end": "2020-11-17 11:22:25.733089",
"failed": false,
"rc": 0,
"start": "2020-11-17 11:22:24.730344",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
And just in case this something weirdly version specific, I'm on Ubuntu 20.04 and this is my version:
$ ansible --version
ansible 2.9.6
config file = /path/to/project/orchestration/ansible.cfg
configured module search path = ['/home/myUser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
Upvotes: 0
Views: 647
Reputation: 68044
The problem is the trailing comma. The task below works as expected and gives "msg: false"
- debug:
msg: '{{ "A" == "B" }}'
The trailing comma creates a tuple
- debug:
msg: '{{ "A" == "B", }}'
- debug:
msg: "{{ (false,)|type_debug }}"
gives
msg: (False,)
msg: tuple
Non-empty variables, including tuples, evaluate to true
- debug:
msg: OK
when: (A,)
gives
msg: OK
Upvotes: 2
Reputation: 785
I have just copied your code into my lab VM and I noticed you are using a comma at the end of the "when" statement (Which I have never seen). I have removed them and get expected output.
When variable is Foo:
PLAY [localhost] ***************************************************************************************************************
TASK [foo] *********************************************************************************************************************
changed: [localhost]
TASK [debug] *******************************************************************************************************************
ok: [localhost] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:00.006168",
"end": "2020-11-17 12:50:11.863107",
"failed": false,
"rc": 0,
"start": "2020-11-17 12:50:11.856939",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
TASK [debug] *******************************************************************************************************************
skipping: [localhost]
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
When variable is Bar:
PLAY [localhost] ***************************************************************************************************************
TASK [foo] *********************************************************************************************************************
changed: [localhost]
TASK [debug] *******************************************************************************************************************
skipping: [localhost]
TASK [debug] *******************************************************************************************************************
ok: [localhost] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Bar"
],
"delta": "0:00:00.006077",
"end": "2020-11-17 12:53:24.142058",
"failed": false,
"rc": 0,
"start": "2020-11-17 12:53:24.135981",
"stderr": "",
"stderr_lines": [],
"stdout": "Bar",
"stdout_lines": [
"Bar"
]
}
}
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
My ansible version is:
ansible 2.7.10
python version = 2.7.5
Upvotes: 1