1
  employees:
    sarav:
      city: Coimbatore
      email: sarav@gritfy.com
      mobile: '985643210'
  tasks:
    - name: gathering_complete_data
      ansible.builtin.debug:
         msg={{ ['sarav'] | map('extract', employees , 'email' ) }}

output:

ok: [CS1] => {
    "msg": [
        "sarav@gritfy.com"
    ]
}

The above script is successfully working. When I made the data structure:

  employees:
    sarav:
    - city: Coimbatore
      email: sarav@gritfy.com
      mobile: '985643210'

Using the Ansible map filter, I cannot gather the information(sarav's email ID).

Could anyone please share the code for fetching sarav's email ID?

1 Answers1

1

You don't need to map the filter extract. You can directly address the attribute

    - debug:
        msg: "{{ employees.sarav.email }}"

gives

  msg: sarav@gritfy.com

In the second case, you can map the attribute

    - debug:
        msg: "{{ employees.sarav | map(attribute='email') }}"

gives

  msg:
  - sarav@gritfy.com

Example of a complete playbook for testing

- hosts: localhost

vars:

employees:
  sarav:
    city: Coimbatore
    email: sarav@gritfy.com
    mobile: '985643210'

employee2:
  sarav:
  - city: Coimbatore
    email: sarav@gritfy.com
    mobile: '985643210'

tasks:

- debug:
    msg: "{{ employees.sarav.email }}"

- debug:
    msg: "{{ employee2.sarav | map(attribute='email') }}"