What is the proper way to check for required variables?

21,142 views
Skip to first unread message

Andrew Pashkin

unread,
May 21, 2014, 1:24:08 AM5/21/14
to ansible...@googlegroups.com
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?

--
With kind regards, Andrew Pashkin.
cell phone - +7 (985) 898 57 59
Skype - waves_in_fluids
e-mail - andrew....@gmx.co.uk

Petros Moisiadis

unread,
May 21, 2014, 3:02:56 AM5/21/14
to ansible...@googlegroups.com
On 05/21/2014 08:24 AM, Andrew Pashkin wrote:
> 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?
>
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:
- fail: msg="Variable '{{ item }}' is not defined"
when: item not in hostvars[inventory_hostname]
with_items:
- myvariable1
- myvariable2

"F.L. Jonathan Araña Cruz"

unread,
May 21, 2014, 3:56:00 AM5/21/14
to ansible...@googlegroups.com
On 21/05/14 06:24, Andrew Pashkin wrote:
> 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?
>
From http://docs.ansible.com/playbooks_conditionals.html

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


my 2¢

Andrew Pashkin

unread,
May 21, 2014, 4:37:50 AM5/21/14
to ansible...@googlegroups.com
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?

Petros Moisiadis

unread,
May 21, 2014, 5:10:32 AM5/21/14
to ansible...@googlegroups.com
On 05/21/2014 11:37 AM, Andrew Pashkin wrote:
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?

You can put in the required_vars.yml file:
---
required_vars:
  - myvar1
  - myvar2

Then, in the playbook:
---
- hosts: myhosts
  vars_files:
    - /path/to/required_vars.yml
  tasks:
    - fail: msg="Variable '{{ item }}' is not defined"
      when: item not in hostvars[inventory_hostname]
      with_items: required_vars

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.

On 21.05.2014 11:02, 'Petros Moisiadis' via Ansible Project wrote:
On 05/21/2014 08:24 AM, Andrew Pashkin wrote:
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?

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:
   - fail: msg="Variable '{{ item }}' is not defined"
     when: item not in hostvars[inventory_hostname]
     with_items:
       - myvariable1
       - myvariable2


-- 
With kind regards, Andrew Pashkin.
cell phone - +7 (985) 898 57 59
Skype - waves_in_fluids
e-mail - andrew....@gmx.co.uk
--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/537C65DE.3070404%40gmx.co.uk.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
May 21, 2014, 6:11:13 PM5/21/14
to ansible...@googlegroups.com
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.




Vijay Mohan

unread,
Mar 29, 2015, 2:32:19 AM3/29/15
to ansible...@googlegroups.com
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

Giovanni Tirloni

unread,
Mar 31, 2015, 10:04:55 AM3/31/15
to ansible...@googlegroups.com
On Sat, 28 Mar 2015 22:46 -0700, Vijay Mohan <vi...@ranker.com> wrote:
> 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:

"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

benjamin...@oscaro.com

unread,
Oct 10, 2016, 9:03:55 AM10/10/16
to Ansible Project, g...@gtirloni.com

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


TOHID MANER

unread,
Apr 15, 2020, 5:47:07 AM4/15/20
to Ansible Project
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


2. In task, include your var file

- include_vars: ../vars/required_vars.yml


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

---

  - namecheck all required parameters

    failmsg="Please provide value for '{{ item }}' parameter"
    whenitem not in vars
    with_items"{{ required_vars }}"

Thanks and Regards,
Tohid


Skype - waves_in_fluids
e-mail - andrew...@gmx.co.uk
Reply all
Reply to author
Forward
0 new messages