Bootstrap FreeKB - Ansible - Ensure variable is defined using the assert module
Ansible - Ensure variable is defined using the assert module

Updated:   |  Ansible articles

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
  tasks:
  - name: Check if the 'foo' variable is defined
    assert:
      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"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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