查找插件
查找插件是 Ansible 对 Jinja2 模板语言的特定扩展。您可以在 playbook 中使用查找插件从外部来源(文件、数据库、键值存储、API 和其他服务)访问数据。与所有 模板 一样,查找在 Ansible 控制机器上执行并评估。Ansible 使用标准模板系统使查找插件返回的数据可用。您可以使用查找插件加载包含来自外部来源的信息的变量或模板。您可以 创建自定义查找插件.
注意
查找执行的工作目录相对于角色或剧本,而不是相对于执行脚本的本地任务。
将
wantlist=True
传递给要在 Jinja2 模板“for”循环中使用的查找。默认情况下,出于安全原因,查找返回值被标记为不安全。如果您信任查找访问的外部来源,请传递
allow_unsafe=True
以允许 Jinja2 模板评估查找值。
警告
一些查找将参数传递给 shell。使用来自远程/不可信来源的变量时,请使用 |quote 过滤器以确保安全使用。
启用查找插件
Ansible 会启用它可以找到的所有查找插件。您可以通过将自定义查找放入与您的剧本相邻的 lookup_plugins
目录,安装的集合的 plugins/lookup/
目录中,独立角色中,或 ansible.cfg 中配置的查找目录来源之一来激活自定义查找。
使用查找插件
您可以在 Ansible 中可以使用模板的任何地方使用查找插件:在剧本中、在变量文件中或 template 模块的 Jinja2 模板中。有关使用查找插件的更多信息,请参见 查找.
vars:
file_contents: "{{ lookup('file', 'path/to/file.txt') }}"
查找是循环的组成部分。在您看到 with_
的任何地方,下划线后的部分都是查找的名称。因此,期望查找输出列表;例如,with_items
使用 items 查找
tasks:
- name: count to 3
debug: msg={{ item }}
with_items: [1, 2, 3]
您可以将查找与 过滤器、测试 甚至彼此组合以进行一些复杂的数据生成和操作。例如
tasks:
- name: Complicated chained lookups and filters
debug: msg="find the answer here:\n{{ lookup('url', 'https://google.com/search/?q=' + item|urlencode)|join(' ') }}"
with_nested:
- "{{ lookup('consul_kv', 'bcs/' + lookup('file', '/the/question') + ', host=localhost, port=2000')|shuffle }}"
- "{{ lookup('sequence', 'end=42 start=2 step=2')|map('log', 4)|list) }}"
- ['a', 'c', 'd', 'c']
版本 2.6 中的新功能。
您可以通过将 errors
设置为 ignore
、warn
或 strict
来控制所有查找插件中的错误行为。默认设置是 strict
,如果查找返回错误,则会导致任务失败。例如
忽略查找错误
- name: if this file does not exist, I do not care .. file plugin itself warns anyway ...
debug: msg="{{ lookup('file', '/nosuchfile', errors='ignore') }}"
[WARNING]: Unable to find '/nosuchfile' in expected paths (use -vvvvv to see paths)
ok: [localhost] => {
"msg": ""
}
获得警告而不是失败
- name: if this file does not exist, let me know, but continue
debug: msg="{{ lookup('file', '/nosuchfile', errors='warn') }}"
[WARNING]: Unable to find '/nosuchfile' in expected paths (use -vvvvv to see paths)
[WARNING]: An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /nosuchfile
ok: [localhost] => {
"msg": ""
}
获得致命错误(默认)
- name: if this file does not exist, FAIL (this is the default)
debug: msg="{{ lookup('file', '/nosuchfile', errors='strict') }}"
[WARNING]: Unable to find '/nosuchfile' in expected paths (use -vvvvv to see paths)
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /nosuchfile"}
强制查找返回列表:query
和 wantlist=True
版本 2.5 中的新功能。
在 Ansible 2.5 中,添加了一个名为 query
的新 Jinja2 函数,用于调用查找插件。 lookup
和 query
之间的区别主要在于 query
将始终返回一个列表。 lookup
的默认行为是返回一个逗号分隔值的字符串。可以使用 wantlist=True
显式配置 lookup
以返回列表。
此功能提供了一个更轻松、更一致的接口,用于与新的 loop
关键字交互,同时保持与其他 lookup
用法的向后兼容性。
以下示例是等效的
lookup('dict', dict_variable, wantlist=True)
query('dict', dict_variable)
如上所示,使用 query
时,wantlist=True
的行为是隐式的。
此外,还引入了 q
作为 query
的简写形式
q('dict', dict_variable)
插件列表
您可以使用 ansible-doc -t lookup -l
查看可用插件的列表。使用 ansible-doc -t lookup <plugin name>
查看特定于插件的文档和示例。