ansible.utils/tests/integration/targets/utils_get_path/tasks/examples_lookup.yaml

96 lines
3.0 KiB
YAML

---
- name: Set fact
ansible.builtin.set_fact:
a:
b:
c:
d:
- 0
- 1
e:
- true
- false
- name: Retrieve a value deep inside a using a path
ansible.builtin.set_fact:
value: "{{ lookup('ansible.utils.get_path', a, path) }}"
vars:
path: b.c.d[0]
# 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: "{{ lookup('ansible.utils.get_path', look_in, look_for) }}"
vars:
look_in: "{{ hostvars[inventory_hostname] }}"
look_for: a.b.c.d[0]
# 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: "{{ lookup('ansible.utils.to_paths', a, prepend='a') }}"
- name: Retrieve the value of each path from vars
ansible.builtin.debug:
msg: "The value of path {{ path }} in vars is {{ lookup('ansible.utils.get_path', hostvars[inventory_hostname], path) }}"
loop: "{{ paths.keys() | list }}"
loop_control:
loop_var: path
# 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
# - name: Get the description of several interfaces
# ansible.builtin.debug:
# msg: "{{ lookup('ansible.utils.get_path', rekeyed, 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('')
# TASK [Get the description of several interfaces] ******************
# ok: [nxos101] => (item=by_name['Ethernet1/1'].description) => changed=false
# msg: Configured by ansible
# ok: [nxos101] => (item=by_name['Ethernet1/2'].description|upper) => changed=false
# msg: CONFIGURED BY ANSIBLE
# ok: [nxos101] => (item=by_name['Ethernet1/3'].description|default('')) => changed=false
# msg: ''