Thanks Vladimir. I'm sure your solution works. I've figured out a simpler solution. Just posting it here for reference - https://stackoverflow.com/a/59367345/434678
On Monday, December 16, 2019 at 5:40:42 PM UTC+5:30, Vladimir Botka wrote: > > Hi Emmanuel, > > On Mon, 16 Dec 2019 00:57:57 -0800 (PST) > Emmanuel Livingstone <[email protected] <javascript:>> 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/e1999aeb-27be-40cf-a48b-86403af3e15f%40googlegroups.com.
