Not sure what you're trying to do with subelements.
You could do this:
---
- name: Print world details
  hosts: localhost
  gather_facts: no
  vars:
    world:
      asia:
        country: india
        state:   maha
        city:    mumbai
      n_america:
        nation:   canada
        province: quebec
        city:     montreal
  tasks:
    - name: Printing details of montreal
      ansible.builtin.debug:
        msg: "{{ item.value.city | default(item.key ~ ' has no city') }}"
      loop: "{{ world | dict2items }}"
      when: item.key == 'n_america'

On Tuesday, March 14, 2023 at 8:58:27 AM UTC-4 Veera wrote:

> I have the items defined in  the file  vars/myvars.yml as below:
>
> *world:*
> *     asia:*
> *         country:  india*
> *         state:     maha         *
> *         city:        mumbai*
>          
> *     n_america:*
> *           nation:  canada*
> *           province : quebec*
> *           city:  montreal*
>
> When I try to  print them with a loop and subelements , i am missing 
> something 
>     * - name:  play to print the details*
> *       hosts: localhost*
> *       gather_facts: no*
> *       vars: *
> *            - vars/myvars.yml*
> *       tasks:*
> *           - name: printing the details of montreal*
> *              debug:*
> *                  msg: "{{  item[2] ['city']}}"*
> *               loop: "{{   world | subelements('n_america') | dict2items  
>  }}"*
>
> I receive a error like      "obj must be a list of dicts or a nested dict"}
>                     
>
> msg: with "*world .**n_america.city" will work *
> I want to print this through loop only , as i need to process other items  
> also.
>
> Thanks ,, 
>   
>

-- 
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/1a3e2cb9-7ce3-4c1f-9112-2e062b9ba28bn%40googlegroups.com.

Reply via email to