ansible.utils.to_paths (4.0.0) — filter

Flatten a complex object into a dictionary of paths and values

| "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

Flatten a complex object into a dictionary of paths and values.

Paths are dot delimited whenever possible.

Brackets are used for list indices and keys that contain special characters.

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

Using the parameters below- C(var|ansible.utils.to_paths(prepend, wantlist))

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.

#### Simple examples

- ansible.builtin.set_fact:
    a:
      b:
        c:
          d:
            - 0
            - 1
          e:
            - true
            - false
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ansible.builtin.set_fact:
    paths: "{{ a|ansible.utils.to_paths }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [ansible.builtin.set_fact] ********************************************
# ok: [nxos101] => changed=false
#   ansible_facts:
#     paths:
#       b.c.d[0]: 0
#       b.c.d[1]: 1
#       b.c.e[0]: True
#       b.c.e[1]: False

- name: Use prepend to add the initial variable name
  ansible.builtin.set_fact:
    paths: "{{ a|ansible.utils.to_paths(prepend='a') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [Use prepend to add the initial variable name] **************************
# 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


#### Using a complex object

- name: Make an API call
  uri:
    url: "https://nxos101/restconf/data/openconfig-interfaces:interfaces"
    headers:
      accept: "application/yang.data+json"
    url_password: password
    url_username: admin
    validate_certs: false
  register: result
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Flatten the complex object
  ansible.builtin.set_fact:
    paths: "{{ result.json|ansible.utils.to_paths }}"

Inputs

    
var:
    description:
    - The value of I(var) will be will be used.
    - This option represents the value that is passed to the filter plugin in pipe format.
    - For example C(config_data|ansible.utils.to_paths()), in this case C(config_data)
      represents this option.
    required: true
    type: raw

prepend:
    description: Prepend each path entry. Useful to add the initial I(var) name.
    required: false
    type: str

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