ansible.utils.get_path 查找 – 使用路径检索变量中的值

注意

此查找插件是 ansible.utils 集合(版本 5.1.2)的一部分。

如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install ansible.utils

要在 playbook 中使用它,请指定:ansible.utils.get_path

ansible.utils 1.0.0 中的新功能

概要

  • 使用路径变量中检索嵌套值

  • 为了方便起见,get_path 也可用作**过滤器插件**

  • 使用以下参数:lookup('ansible.utils.get_path', var, path, wantlist)

关键字参数

这描述了查找的关键字参数。这些是以下示例中的值 key1=value1key2=value2 等:lookup('ansible.utils.get_path', key1=value1, key2=value2, ...)query('ansible.utils.get_path', key1=value1, key2=value2, ...)

参数

注释

path

字符串 / 必需

变量中检索值的路径

路径需要是有效的 Jinja 路径。

var

任意 / 必需

应从中提取值的变量。

wantlist

布尔值

如果设置为 True,则返回值始终为列表。

也可以使用 queryq 而不是 lookup 来完成。

https://docs.ansible.org.cn/ansible/latest/plugins/lookup.html.

选项

  • false

  • true

示例

- 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 {{ value }}"
  loop: "{{ paths.keys()|list }}"
  loop_control:
    label: "{{ item }}"
  vars:
    path: "{{ item }}"
    value: "{{ lookup('ansible.utils.get_path', hostvars[inventory_hostname], item) }}"

# 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: ''

返回值

描述

返回值

字符串

一个或多个匹配列表项的从零开始的索引。

如果始终需要列表,请参阅 wantlist

返回: 成功

作者

  • Bradley Thornton (@cidrblock)

提示

每个条目类型的配置条目都有从低到高的优先级顺序。例如,列表中位置较低的变量将覆盖位置较高的变量。