Hi Emmanuel,
On Mon, 16 Dec 2019 00:57:57 -0800 (PST)
Emmanuel Livingstone <
livings...@gmail.com> 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