Ansible Conditionals and Parentheses evaluate to True
Posted in Operating Systems, Linux, Tips on Tuesday, September 11, 2018 by cam
I had fun wasting hours working out how to do correct ‘when’ statements in Ansible - In end up consulting #ansible on IRC to get the answers.
Anyway I hope the following playbook makes sense to you. Note that ‘admintool’ is a valid group in my situation.
- name: Debug all the things hosts: all tasks: - set_fact: renew_cert="renew" # Valid - Should pause - name: Test 0 PASS pause: prompt="Test" seconds=1 when: '"admintool" in group_names and renew_cert == "renew"' # Valid - Should skip - name: Test 1 SKIP pause: prompt="Test" seconds=1 when: '"I-Dont-Exist" in group_names and renew_cert == "renew"' # Valid - Should skip - name: Test 2 SKIP pause: prompt="Test" seconds=1 when: - "'i-dont-exist' in group_names" - renew_cert == "renew" # Valid - Should pause - name: Test 3 PASS pause: prompt="Test" seconds=1 when: - "'admintool' in group_names" - renew_cert == "renew" # Invalid - Should skip - but eval's True - DONT USE - name: Test 4 SKIP pause: prompt="Test" seconds=1 when: ("'admintool' in group_names" and renew_cert == "renew") # Invalid - Should skip - but eval's True - DONT USE - name: Test 5 SKIP pause: prompt="Test" seconds=1 when: ("'I-dont-exist' in group_names") # Valid - Should pause - name: Test 6 PASS pause: prompt="Test" seconds=1 when: ("admintool" in group_names and renew_cert == "renew") # Valid - Should skip - name: Test 7 SKIP pause: prompt="Test" seconds=1 when: ("I-dont-exist" in group_names and renew_cert == "renew")