This is my example playbook:
---
 - name: play1
   hosts: app1
   vars_files:
   - "vars/app1.yml"

  tasks:
  - include_role:
      name: role1
    vars:
      applicationname: "{{ APPLICATIONNAME }}"
      description: "{{ DESCRIPTION }}"

Now in the role, I want to make sure applicationname is not undefined, 
empty or null.

It works when I use assert in the role:


/roles/defaults/main.yml
---
applicationname: ""
description: ""


/roles/tasks/main.yml
---
- assert:
    that:
      - (applicationname != "") and (applicationname is not none) and 
(applicationname 
is defined)
    msg: "Variable 'applicationname' is null, empty or undefined"


But this is ugly when I have lots of variables in a role.


I tried the following approach:

/roles/defaults/main.yml
---
applicationname: ""
description: ""

required_vars:
 - applicationname


/roles/tasks/main.yml
---
- assert:
    that:
      - item != ""
      - item is not none
    msg: "A Variable is null, empty or undefined. But we don't know which 
one"
  with_items: "{{ required_vars }}"

This workes, but we don't know which variable is affected, this is also 
ugly.



Another approach:

- name: check required vars
  fail: msg="Variable '{{ item }}' is not defined"
  when: item not in vars
  with_items: "{{required_vars}}"

Workes, but you cannot check the value of the variable, just if it is not 
undefined.



How do I make sure that a lot of variables are not undefined, empty or null 
when passed to a role in a simple way?


Thanks for your help.

Regards
jn







-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5a1915fa-8097-4f30-aef3-2a223f973fc2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to