Search code examples
ansiblebooleanconditional-statementscase-whennegation

Unable to get correct behaviour from Ansible boolean variable


Jenkins has a Boolean parameter mycheck which gets passed to ansible playbook like below:

ansible-playbook -i /var/myserver.hosts /var/testplay.yml -e mycheck=$mycheck

The above translates to the below when the checkbox is unchecked:

ansible-playbook -i /var/myserver.hosts /var/testplay.yml -e mycheck=False

cat /var/testplay.yml

- debug:
    msg: "mycheck is True"
  when: not mycheck

However, the above debug gets skipped when I was expecting the when condition to be true and print.

Can you please suggest why not mycheck is not translated to "true" and the when condition met ?


Solution

  • Cause: This is caused due to the datatype issue, By default, ansible treats any variable passed via extra-vars as Unicode string instead of associating a boolean class to the text that is passed.

    Solution: Your initial command of execution should be modified to below ansible-playbook -i /var/myserver.hosts /var/testplay.yml -e '{ mycheck: True }'