ansible.utils.get_path (4.0.0) — filter

Retrieve the value in a variable using a path

| "added in version" 1.0.0 of ansible.utils"

Authors: Bradley Thornton (@cidrblock)

Install collection

Install with ansible-galaxy collection install ansible.utils:==4.0.0


Add to requirements.yml

  collections:
    - name: ansible.utils
      version: 4.0.0

Description

Use a I(path) to retrieve a nested value from a I(var).

B(get_path) is also available as a B(lookup plugin) for convenience.

Using the parameters below- C(var|ansible.utils.get_path(path, wantlist))

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ansible.builtin.set_fact:
    a:
      b:
        c:
          d:
            - 0
            - 1
          e:
            - true
            - false
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Retrieve a value deep inside a using a path
  ansible.builtin.set_fact:
    value: "{{ a|ansible.utils.get_path(path) }}"
  vars:
    path: b.c.d[0]
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [Retrieve a value deep inside a using a path] ******************
# ok: [localhost] => changed=false
#   ansible_facts:
#     value: '0'


#### Working with hostvars

- name: Retrieve a value deep inside all of the host's vars
  ansible.builtin.set_fact:
    value: "{{ look_in|ansible.utils.get_path(look_for) }}"
  vars:
    look_in: "{{ hostvars[inventory_hostname] }}"
    look_for: a.b.c.d[0]
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [Retrieve a value deep inside all of the host's vars] ********
# ok: [nxos101] => changed=false
#   ansible_facts:
#     as_filter: '0'
#     as_lookup: '0'


#### Used alongside ansible.utils.to_paths

- name: Get the paths for the object
  ansible.builtin.set_fact:
    paths: "{{ a|ansible.utils.to_paths(prepend='a') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Retrieve the value of each path from vars
  ansible.builtin.debug:
    msg: "The value of path {{ path }} in vars is {{ value }}"
  loop: "{{ paths.keys()|list }}"
  loop_control:
    label: "{{ item }}"
  vars:
    path: "{{ item }}"
    value: "{{ vars|ansible.utils.get_path(item) }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [Get the paths for the object] *******************************
# ok: [nxos101] => changed=false
#   ansible_facts:
#     paths:
#       a.b.c.d[0]: 0
#       a.b.c.d[1]: 1
#       a.b.c.e[0]: True
#       a.b.c.e[1]: False

# TASK [Retrieve the value of each path from vars] ******************
# ok: [nxos101] => (item=a.b.c.d[0]) =>
#   msg: The value of path a.b.c.d[0] in vars is 0
# ok: [nxos101] => (item=a.b.c.d[1]) =>
#   msg: The value of path a.b.c.d[1] in vars is 1
# ok: [nxos101] => (item=a.b.c.e[0]) =>
#   msg: The value of path a.b.c.e[0] in vars is True
# ok: [nxos101] => (item=a.b.c.e[1]) =>
#   msg: The value of path a.b.c.e[1] in vars is False


#### Working with complex structures and transforming results

- name: Retrieve the current interface config
  cisco.nxos.nxos_interfaces:
    state: gathered
  register: interfaces
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Get the description of several interfaces
  ansible.builtin.debug:
    msg: "{{ rekeyed|ansible.utils.get_path(item) }}"
  vars:
    rekeyed:
      by_name: "{{ interfaces.gathered|ansible.builtin.rekey_on_member('name') }}"
  loop:
    - by_name['Ethernet1/1'].description
    - by_name['Ethernet1/2'].description|upper
    - by_name['Ethernet1/3'].description|default('')

Inputs

    
var:
    description:
    - The variable from which the value should be extracted.
    - This option represents the value that is passed to the filter plugin in pipe format.
    - For example C(config_data|ansible.utils.get_path()), in this case C(config_data)
      represents this option.
    required: true
    type: raw

path:
    description:
    - The I(path) in the I(var) to retrieve the value of.
    - The I(path) needs to be a valid jinja path.
    required: true
    type: str

wantlist:
    description:
    - If set to C(True), the return value will always be a list.
    type: bool