# Searching for eficient way to check group of variables

**URL:** <https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071>\
**Category:** Ansible Project\
**Created:** [July 5, 2013, 6:56am UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071 "2013-07-05T06:56:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [July 5, 2013, 6:56am UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071/1 "2013-07-05T06:56:00Z")

</div>

Hello,

I recently wrote role, that set up an openvz on centos host, including network configuration. For that purpose, I have variables ip, gateway, netmask, dns1 and dns2 in host\_vars/.

Variables are mandatory, I cannot set up networking withnout them, so I`m checking them out. So far I have this code:

```auto
- fail: msg="You need to define ip, gateway, netmask, dns1 and dns2 in hosts_vars"
  when: ip is not defined

- fail: msg="You need to define ip, gateway, netmask, dns1 and dns2 in hosts_vars"

  when: gateway is not defined

- fail: msg="You need to define ip, gateway, netmask, dns1 and dns2 in hosts_vars"
  when: netmask is not defined

- fail: msg="You need to define ip, gateway, netmask, dns1 and dns2 in hosts_vars"
  when: dns1 is not defined

- fail: msg="You need to define ip, gateway, netmask, dns1 and dns2 in hosts_vars"

  when: dns2 is not defined

```

Somehow I think, I`m pretty sure, I`m missing some nicer way how to do it, is it possible to have it in one fail statement?

Regards  
David Karban

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 5, 2013, 2:02pm UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071/2 "2013-07-05T14:02:20Z")

</div>

In the latest 1.3, you can use the variable in a template like this:

{{ foo | mandatory }}

This will yell when you try to use the variable that is undefined.

You can also globally turn on “all variables must be defined” warnings in ansible.cfg  
(see examples/ansible.cfg in the checkout)

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [July 5, 2013, 2:53pm UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071/3 "2013-07-05T14:53:25Z")

</div>

Thanks, I will try it out!

Dne 5.7.2013 16:02 “Michael DeHaan” \<[michael@ansibleworks.com](mailto:michael@ansibleworks.com)\> napsal(a):

---

<div class="post-metadata">

**Author:** ![David\_Golden](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@David\_Golden](https://forum.ansible.com/u/David_Golden)\
**Post date:** [July 5, 2013, 6:15pm UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071/4 "2013-07-05T18:15:40Z")

</div>

My approach:

(1) I use a single role variable that contains keys/value. E.g. for  
the "webapp" role, I call it "webapp\_args".

(2) Any globally defined variables (e.g. from host\_vars), I copy into  
a slot in webapp\_args on each role invocation

(3) In the role, I check that webapp\_args is defined

(4) In the role, I loop over mandatory items that must exist:

Example:

- name: Assert webapp\_args  
&nbsp;&nbsp;fail: msg="required webapp\_args parameters must be provided"  
&nbsp;&nbsp;when: webapp\_args is not defined

- name: Assert required webapp\_args parameters  
&nbsp;&nbsp;fail: msg="required webapp\_args[{{item}}] must be provided"  
&nbsp;&nbsp;when: webapp\_args[item] is not defined  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- app\_name  
&nbsp;&nbsp;&nbsp;&nbsp;- app\_path  
&nbsp;&nbsp;&nbsp;&nbsp;- app\_port  
&nbsp;&nbsp;&nbsp;&nbsp;- ...

I hope someday there's a way to do that more automatically in the way  
that roles work, but for now it's working fine for me.

You could probably do it with globals instead of a role argument  
structure, but I personally dislike using globals in that way, so I  
didn't do that myself.

-- David

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 5, 2013, 6:20pm UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071/5 "2013-07-05T18:20:47Z")

</div>

Also:

- fail: msg=variables not defined  
when: foo is not defined or bar is not defined or baz is not defined

should be valid.

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [July 8, 2013, 8:39am UTC](https://forum.ansible.com/t/searching-for-eficient-way-to-check-group-of-variables/13071/6 "2013-07-08T08:39:10Z")

</div>

> Also:
> 
> - fail: msg=variables not defined  
> &nbsp;&nbsp;when: foo is not defined or bar is not defined or baz is not defined

Hi Michael,

tested, works great, thanks !
