Ansible ignoring conditional
ansible
Solution
Here's your conditional:
- when: ubuntu_installed.skipped is not defined and ubuntu_installed.stdout != "" and ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
Your precedence is incorrect: A and B and C or D means is (A and B and C) or D, which is not what you want. (note Ansible conditionals use Jinja2 expressions, which reflect Python conditionals, so here are the precedence rules).
Lazy programmers add parenthesis for clarity; here's the revised conditional:
- when: (ubuntu_installed.skipped is not defined and ubuntu_installed.stdout != "") and (ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu')
PS- you showed the output to one command, `debug`, but it's helpful to give alllll the output. That's `ansible-playbook -vvv`.
Problem
I am working on a ansible playbook for my servers (mix of ubuntu and centos) and when trying to transfer some config files for monit if a program is installed I am running into an issue. It works perfectly on my centos machines but the ubuntu one's it transfers the template no matter what completely ignoring the conditional. ``` --- - name: Check for Sendmail (Ubuntu) shell: dpkg-query -W -f='${Status} ${Version}\n' sendmail register: ubuntu_installed when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' changed_when: False failed_when: "'FAILED' in ubuntu_installed.stderr" - debug: var=ubuntu_installed - name: Check for Sendmail (CentOs) shell: rpm -qa | grep sendmail register: cent_installed when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' changed_when: False failed_when: "'FAILED' in cent_installed.stderr" - name: Transfer Monit config files for Sendmail (Ubuntu) template: src=monit/templates/sendmail.j2 dest=/etc/monit/conf.d/sendmail owner=root group=root mode=644 when: ubuntu_installed.skipped is not defined and ubuntu_installed.stdout != "" and ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' - name: Transfer Monit config files for Sendmail (CentOs) template: src=monit/templates/sendmail.j2 dest=/etc/monit.d/sendmail owner=root group=root mode=644 when: cent_installed.skipped is not defined and cent_installed.stdout != "" and ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' ``` I have the debug in there to check to make sure the variable is right. When I run the playbook I get... ``` TASK: [monit | debug var=ubuntu_installed] ************************************ ok: [server1] => { "item": "", "ubuntu_installed": { "changed": false, "cmd": "dpkg-query -W -f='${Status} ${Version}\\n' sendmail ", "delta": "0:00:00.012985", "end": "2014-07-11 16:56:12.688509", "failed": false, "failed_when_result": false, "invocation": { "module_args": "dpkg-query -W -f='${Status} ${Version}\\n' sendmail", "module_name": "shell" }, "item": "", "rc": 1, "start": "2014-07-11 16:56:12.675524", "stderr": "dpkg-query: no packages found matching sendmail", "stdout": "", "stdout_lines": [] } } ``` It is completely ignoring the conditional `ubuntu_installed.stdout != ""`