Hi Kai
You used the variable in a playbook, maybe that works. But I want to make
sure that the role is not being executed when someone forgets to set the
variable.
So the trick here is: "{{ APPLICATIONNAME }}" is not set.
Normaly this is a variable in vars/app1.yml
Here is the full code:
/vars/app1.yml
---
#APPLICATIONNAME: "app1"
#normally this var is set, but we want to provoke an error
playbook.yml
---
- name: play1
hosts: app1
vars_files:
- "vars/app1.yml"
tasks:
- include_role:
name: role1
vars:
applicationname: "{{ APPLICATIONNAME }}"
description: "{{ DESCRIPTION }}"
/roles/defaults/main.yml
---
required_vars:
- applicationname
applicationname: ""
description: ""
/role/tasks/main.yml
---
- debug:
var: vars
- debug:
var: applicationname
- name: check required vars when applicationname is not defined
debug: msg="Variable '{{ item }}' is not defined"
when: vars[item] is not defined
with_items: "{{ required_vars }}"
Execution:
TASK [role1 : debug] ***********************************************
ok: [server1] => {
"vars": {
"applicationname": "{{ APPLICATIONNAME }}"
}
}
TASK [role1 : debug]
********************************************************
ok: [SERVER1] => {
"applicationname": "VARIABLE IS NOT DEFINED!"
}
TASK [role1 : check required vars when applicationname is not defined]
*******************************
skipping: [server1] => (item=applicationname) => {"changed": false,
"item": "applicationname", "skip_reason": "Conditional result was False"}
Or is my approach completely wrong?
--
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/c7c16217-530d-46e1-b7f5-a213df1cc81c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.