ansible.builtin.first_found 查找 – 从列表中返回第一个找到的文件
注意
此查找插件是 ansible-core
的一部分,包含在所有 Ansible 安装中。在大多数情况下,您可以使用简短的插件名称 first_found
。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.first_found
来方便链接到插件文档,并避免与可能具有相同查找插件名称的其他集合发生冲突。
概要
术语
参数 |
注释 |
---|---|
文件名列表。 |
关键字参数
这描述了查找的关键字参数。这些是以下示例中的值 key1=value1
、key2=value2
等:lookup('ansible.builtin.first_found', key1=value1, key2=value2, ...)
和 query('ansible.builtin.first_found', key1=value1, key2=value2, ...)
参数 |
注释 |
---|---|
文件名列表。 默认值: |
|
要查找文件的路径列表。 默认值: |
|
当 这在与 当通过 当 当 选择
|
注意
另请参见
另请参见
- 任务路径
用于相对路径/文件的搜索路径。
示例
- name: Set _found_file to the first existing file, raising an error if a file is not found
ansible.builtin.set_fact:
_found_file: "{{ lookup('ansible.builtin.first_found', findme) }}"
vars:
findme:
- /path/to/foo.txt
- bar.txt # will be looked in files/ dir relative to role and/or play
- /path/to/biz.txt
- name: Set _found_file to the first existing file, or an empty list if no files found
ansible.builtin.set_fact:
_found_file: "{{ lookup('ansible.builtin.first_found', files, paths=['/extra/path'], skip=True) }}"
vars:
files:
- /path/to/foo.txt
- /path/to/bar.txt
- name: Include tasks only if one of the files exist, otherwise skip the task
ansible.builtin.include_tasks:
file: "{{ item }}"
with_first_found:
- files:
- path/tasks.yaml
- path/other_tasks.yaml
skip: True
- name: Include tasks only if one of the files exists, otherwise skip
ansible.builtin.include_tasks: '{{ tasks_file }}'
when: tasks_file != ""
vars:
tasks_file: "{{ lookup('ansible.builtin.first_found', files=['tasks.yaml', 'other_tasks.yaml'], errors='ignore') }}"
- name: |
copy first existing file found to /some/file,
looking in relative directories from where the task is defined and
including any play objects that contain it
ansible.builtin.copy:
src: "{{ lookup('ansible.builtin.first_found', findme) }}"
dest: /some/file
vars:
findme:
- foo
- "{{ inventory_hostname }}"
- bar
- name: same copy but specific paths
ansible.builtin.copy:
src: "{{ lookup('ansible.builtin.first_found', params) }}"
dest: /some/file
vars:
params:
files:
- foo
- "{{ inventory_hostname }}"
- bar
paths:
- /tmp/production
- /tmp/staging
- name: INTERFACES | Create Ansible header for /etc/network/interfaces
ansible.builtin.template:
src: "{{ lookup('ansible.builtin.first_found', findme)}}"
dest: "/etc/foo.conf"
vars:
findme:
- "{{ ansible_virtualization_type }}_foo.conf"
- "default_foo.conf"
- name: read vars from first file found, use 'vars/' relative subdir
ansible.builtin.include_vars: "{{lookup('ansible.builtin.first_found', params)}}"
vars:
params:
files:
- '{{ ansible_distribution }}.yml'
- '{{ ansible_os_family }}.yml'
- default.yml
paths:
- 'vars'
返回值
键 |
描述 |
---|---|
找到的文件的路径 返回:成功 |