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)

---

  - name: check all required parameters

    fail: msg="Please provide value for '{{ item }}' parameter"
    when: item not in vars
    with_items: "{{ required_vars }}"

Thanks and Regards,
Tohid



On Wednesday, May 21, 2014 at 2:07:50 PM UTC+5:30, 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?
> 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 - [email protected] <javascript:>
>
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/9787f066-c699-4b60-9da5-7d4e1920681e%40googlegroups.com.

Reply via email to