# What is the proper way to check for required variables?

**URL:** <https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286>\
**Category:** Ansible Project\
**Created:** [May 21, 2014, 5:24am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286 "2014-05-21T05:24:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrei\_Pashkin](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Andrei\_Pashkin](https://forum.ansible.com/u/Andrei_Pashkin)\
**Post date:** [May 21, 2014, 5:24am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/1 "2014-05-21T05:24:08Z")

</div>

I want to make my playbook in a way where process will fail with an error if user was not provided required parameters, what is the way to do that in Ansible?

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 21, 2014, 7:02am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/2 "2014-05-21T07:02:56Z")

</div>

The default behavior is to fail if using a variable that is not defined.  
There is also a configuration option that disables this, and then you  
can use "{{ myvar | mandatory }}" to explicitly have the same effect.  
Keep in mind, though, that the failure occurs when reaching a task that  
uses an undefined variable. If you want to fail before any task is  
executed, you could add another task at the top of your tasks that fails  
if a mandatory variable is not defined. For example:

tasks:  
&nbsp;&nbsp;&nbsp;- fail: msg="Variable '{{ item }}' is not defined"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item not in hostvars[inventory\_hostname]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- myvariable1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- myvariable2

---

<div class="post-metadata">

**Author:** ![F.L\_Jonathan\_Arana\_C](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@F.L\_Jonathan\_Arana\_C](https://forum.ansible.com/u/F.L_Jonathan_Arana_C)\
**Post date:** [May 21, 2014, 7:56am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/3 "2014-05-21T07:56:00Z")

</div>

- fail: msg="Bailing out. this play requires 'bar'"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: bar is not defined

my 2�

---

<div class="post-metadata">

**Author:** ![Andrei\_Pashkin](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Andrei\_Pashkin](https://forum.ansible.com/u/Andrei_Pashkin)\
**Post date:** [May 21, 2014, 8:37am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/4 "2014-05-21T08:37:50Z")

</div>

Thanks for the tips, didnt know about both!

What if I want to “declare” variables somewhere, to tell user about what he can use, but leave that variables “undefined” so they will fail `is defined` test? What people usually do in this situations?  
I want to make file like `required_vars.yml` and put all required variables there, and then do something like this:

> `- name: check required variables`  
> `fail: msg="Variable '{{ item }}' is not defined"` `when: item is defined` `with\_items:` `- include: required\_vars.yml`

Is there way to do this?

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 21, 2014, 9:10am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/5 "2014-05-21T09:10:32Z")

</div>

You can put in the required\_vars.yml file: Then, in the playbook: Normally, you would also want to document the structure of your vars, so I would suggest you to write a ‘readme’ file with example definitions of the all possible variables, both mandatory and non-mandatory.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 21, 2014, 10:11pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/6 "2014-05-21T22:11:13Z")

</div>

error\_on\_undefined\_variables is the default in ansible.cfg

I _STRONGLY_ recommend leaving that setting on.

Then the whole “|mandatory” thing is also not required.

It’s there because very old ansible didn’t require variables to be defined by default, now it does, so many upgraders may just wish to change the setting.

---

<div class="post-metadata">

**Author:** ![Vijay\_Mohan](https://avatars.discourse-cdn.com/v4/letter/v/ee7513/32.png) [@Vijay\_Mohan](https://forum.ansible.com/u/Vijay_Mohan)\
**Post date:** [March 29, 2015, 5:46am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/7 "2015-03-29T05:46:54Z")

</div>

Hi Guys,

I am trying to force the ansible play to check againt the command line variable, here is my “check-requirement.yml” and required\_var:

- fail: msg=“Bailing out this play requires ‘{{ item }}’”  
when: item is not defined  
with\_items: required\_vars

I have set default/main.yml with following line:

required\_vars:

- env

Now when i run the play book without env, its skipping rather than failing: Please help me to fix this…

Here is the message:

root@test-server-s01:~/ranker-orchestration/roles# ansible-playbook tomcat-role-test.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [tomcat | fail msg=“Bailing out this play requires ‘{{ item }}’”] \*\*\*\*\*\*\*  
skipping: [localhost] =\> (item=env)

TASK: [tomcat | Update apt cache] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [tomcat | Install Tomcat 7] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [tomcat | Configure tomcat memory/java\_home configuration] \*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [tomcat | Configure tomcat server configuration, port, connections ssl etc] \*\*\*  
ok: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=6 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [March 31, 2015, 2:04pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/8 "2015-03-31T14:04:50Z")

</div>

"item" is a variable that is always defined when you use with\_items.  
Ansible will parse that and confirm it exists and is defined.

You could pass "item" through Jinja2 first, which will use the value  
it's defined to and fail if it can't locate that variable.

when: "{{ item }} is not defined"

I haven't done a serious effort to understand the Ansible/Jinja2 flow  
but the above should work.

Giovanni

---

<div class="post-metadata">

**Author:** ![benjamin.jolivot](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@benjamin.jolivot](https://forum.ansible.com/u/benjamin.jolivot)\
**Post date:** [October 10, 2016, 1:03pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/9 "2016-10-10T13:03:55Z")

</div>

Hello,

All my roles check mandatory vars before doing anything,  
that way :

- fail: msg=“Variable ‘{{ item }}’ is not defined”  
when: item not in vars  
with\_items:
- var1
- vars\_xx
- vars3

---

<div class="post-metadata">

**Author:** ![TOHID\_MANER](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@TOHID\_MANER](https://forum.ansible.com/u/TOHID_MANER)\
**Post date:** [April 15, 2020, 9:47am UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-for-required-variables/15286/10 "2020-04-15T09:47:06Z")

</div>

Hi, You can try below solution to check all required parameter (the parameter user has to specify while running the playbook/script) 1. Your var file - required\_vars.yml

required\_vars:

- var1

- var2

- var3

1. In task, include your var file

- include\_vars: …/vars/required\_vars.yml

1. Task to validate parameters (can be written in separate yml file)
