Ansible - assert module (ensure variable is defined)

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The assert module can be used to do something if a variable is or is not defined. Let's say the "foo" variable is not defined.

- hosts: localhost
  - name: Check if the 'foo' variable is defined
      that: foo is defined
      fail_msg: the 'foo' variable is not defined


Which should return something like this.

TASK [Check if the 'foo' variable is defined] 
fatal: [localhost]: FAILED! => {
    "assertion": "foo is defined",
    "changed": false,
    "evaluated_to": false,
    "msg": "the 'foo' variable is not defined"


On the other hand, let's say the "foo" variable is defined. In this scenario, something like this should be returned.

TASK [Check if the 'foo' variable is defined]
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"


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 ec498 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |