Hi Emmanuel,

On Mon, 16 Dec 2019 00:57:57 -0800 (PST)
Emmanuel Livingstone <[email protected]> wrote:

>   vars:
>     parent:
>       child1: true
>   tasks:
>   - name: "Check if variable is defined"
>     fail:
>       msg: "mandatory variable {{ item }} not passed as extra args"
>     when: item not in vars
>     loop:
>     - parent
>     - parent.child1
>     - parent.child2
> 
> skipping: [localhost] => (item=parent) 
> failed: [localhost] (item=parent.child1) => {"ansible_loop_var": "item", 
> "changed": false, "item": "parent.child1", "msg": "mandatory variable 
> parent.child1 not passed as extra args when invoking playbook"}

'json_query' returns 'False' when the parameter is not defined. 'set_fact'
before the evaluation to register the variable with 'hostvars'. For example

    - set_fact:
        parent: "{{ parent }}"

    - fail:
        msg: "mandatory variable {{ item }} not passed as extra args"
      when: not hostvars[inventory_hostname]|json_query(item)
      loop:
        - parent
        - parent.child1
        - parent.child2

Cheers,

        -vlado

-- 
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/20191216131024.7368cd13%40gmail.com.

Attachment: pgpxjVwDZ84_L.pgp
Description: OpenPGP digital signature

Reply via email to