Ansible - when variable is defined or not defined

Let's say you have a playbook that should only be executed when the "foo" variable is defined.

---
- hosts: all
  tasks:
    - name: "fail when the 'foo' variable is undefined"
      fail: 
       msg: "the 'foo' variable is undefined"
      when: "foo is undefined"

 

Invoking this playbook should return something like this. The ansible-doc fail command can be used to show the Ansible documention on the fail module.

When fail evaluates to true, all of the subsequent tasks are skipped.

PLAY [all]

TASK [fail when the 'foo' variable is undefined]
fatal: [server1.example.com]: FAILED => {"changed": false, "msg": "the 'foo' variable is undefined"}

PLAY RECAP
server1.example.com : ok=1  changed=0  unreacable=0  failed=1

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 8c9b6 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |