Correct way to use fail module and negative conditionals

712 views
Skip to first unread message

Frank Ly

unread,
Dec 19, 2015, 11:18:52 PM12/19/15
to Ansible Project
I am trying to use the fail module at the very beginning of my playbook to check if the target server is a specific distribution and version. This way I can stop the playbook from running early on before any making any chances.

playbook:
---
- hosts: all

  tasks
:
   
- debug: msg='distro is {{ ansible_distribution }} and version is {{ ansible_distribution_major_version }}'

   
- name: check if target server is CentOS 6
      fail
: msg='You have attempted to run this playbook against an unsupported platform!'
     
when: (ansible_distribution != "CentOS" and ansible_distribution_major_version != "6")

   
- name: check if target server is CentOS 6 or Ubuntu 14
      fail
: msg='unsupported platform detected!'
     
when: (ansible_distribution != "CentOS" and ansible_distribution_major_version != "6") or (ansible_distribution != "Ubuntu" and ansible_distribution_major_version != "14")

   
- debug: msg="Supported platform detected!"

playbook execution:

ansible
-playbook test.yml -i inventory -u root -k
SSH password
:

PLAY
[all] ********************************************************************

GATHERING FACTS
***************************************************************
ok
: [192.168.245.11]

TASK
: [debug msg='distro is {{ ansible_distribution }} and version is {{ ansible_distribution_major_version }}'] ***
ok
: [192.168.245.11] => {
   
"msg": "distro is CentOS and version is 6"
}

TASK
: [check if target server is CentOS 6] ***********************
skipping
: [192.168.245.11]

TASK
: [check if target server is CentOS 6 or Ubuntu 14] ***********************
failed
: [192.168.245.11] => {"failed": true}
msg
: unsupported platform detected!

FATAL
: all hosts have already failed -- aborting

PLAY RECAP
********************************************************************
           to
retry, use: --limit @/tmp/test.retry

192.168.245.11               : ok=2    changed=0    unreachable=0    failed=1  


Where am I going wrong ?

Frank Ly

unread,
Dec 23, 2015, 4:44:35 PM12/23/15
to Ansible Project
anyone able to help ?

Josh Smift

unread,
Dec 23, 2015, 5:00:03 PM12/23/15
to ansible...@googlegroups.com
FL> - name: check if target server is CentOS 6 or Ubuntu 14
FL> fail: msg='unsupported platform detected!'
FL> when: (ansible_distribution != "CentOS" and
FL> ansible_distribution_major_version != "6") or (ansible_distribution !=
FL> "Ubuntu" and ansible_distribution_major_version != "14")

This logic seems wrong to me. With

"msg": "distro is CentOS and version is 6"

then your when turns into

when: (False and False) or (True and True)

aka

when: False or True

aka True. Right?

I think you want something more like

when: NOT (CentOS 6 or Ubuntu 14)

than like

when: (not CentOS 6) or (not Ubuntu 14)

Because you're always going to be not one of those things -- you need to
negate the whole or.

-Josh (j...@care.com)






This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

Frank Ly

unread,
Jan 5, 2016, 7:53:53 PM1/5/16
to Ansible Project
Thanks for clarifying that for me Josh.

What I did was create a variable that contains the supported distributions and have my when condition check against it:

vars:
  supported_distros
: "{{ ansible_distribution }} {{ ansible_distribution_major_version }}"

tasks
:
 
- name: check if server is running a supported distro
    fail
: msg='Unsupported platform!'
   
when: platform not in ["Ubuntu 14","CentOS 6"]

That did the tricks!



On Saturday, December 19, 2015 at 11:18:52 PM UTC-5, Frank Ly wrote:
Reply all
Reply to author
Forward
0 new messages