mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
clarifying docs with first_found (#36951)
better examlpes as this was never really a task loop, but contained inside the lookup itself
This commit is contained in:
parent
e05cad785e
commit
42d15671d0
1 changed files with 43 additions and 25 deletions
|
@ -11,6 +11,10 @@ DOCUMENTATION = """
|
||||||
short_description: return first file found from list
|
short_description: return first file found from list
|
||||||
description:
|
description:
|
||||||
- this lookup checks a list of files and paths and returns the full path to the first combination found.
|
- this lookup checks a list of files and paths and returns the full path to the first combination found.
|
||||||
|
- As all lookups, when fed relative paths it will try use the current task's location first and go up the chain
|
||||||
|
to the containing role/play/include/etc's location.
|
||||||
|
- The list of files has precedence over the paths searched.
|
||||||
|
i.e, A task in a role has a 'file1' in the play's relative path, this will be used, 'file2' in role's relative path will not.
|
||||||
options:
|
options:
|
||||||
_terms:
|
_terms:
|
||||||
description: list of file names
|
description: list of file names
|
||||||
|
@ -20,38 +24,52 @@ DOCUMENTATION = """
|
||||||
"""
|
"""
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
- name: show first existin file
|
- name: show first existing file
|
||||||
debug: var=item
|
debug: msg={{lookup('first_found', findme)}}
|
||||||
with_first_found:
|
vars:
|
||||||
|
findme:
|
||||||
- "/path/to/foo.txt"
|
- "/path/to/foo.txt"
|
||||||
- "bar.txt" # will be looked in files/ dir relative to play or in role
|
- "bar.txt" # will be looked in files/ dir relative to role and/or play
|
||||||
- "/path/to/biz.txt"
|
- "/path/to/biz.txt"
|
||||||
|
|
||||||
- name: copy first existing file found to /some/file
|
- name: |
|
||||||
copy: src={{item}} dest=/some/file
|
copy first existing file found to /some/file,
|
||||||
with_first_found:
|
looking in relative directories from where the task is defined and
|
||||||
|
including any play objects that contain it
|
||||||
|
copy: src={{lookup('first_found', findme)}} dest=/some/file
|
||||||
|
vars:
|
||||||
|
findme:
|
||||||
- foo
|
- foo
|
||||||
- "{{inventory_hostname}}
|
- "{{inventory_hostname}}
|
||||||
- bar
|
- bar
|
||||||
|
|
||||||
- name: same copy but specific paths
|
- name: same copy but specific paths
|
||||||
copy: src={{item}} dest=/some/file
|
copy: src={{lookup('first_found', findme, mypaths}} dest=/some/file
|
||||||
with_first_found:
|
vars:
|
||||||
- files:
|
findme:
|
||||||
- foo
|
- foo
|
||||||
- "{{inventory_hostname}}
|
- "{{inventory_hostname}}
|
||||||
- bar
|
- bar
|
||||||
paths:
|
mypaths:
|
||||||
- /tmp/production
|
- /tmp/production
|
||||||
- /tmp/staging
|
- /tmp/staging
|
||||||
|
|
||||||
- name: INTERFACES | Create Ansible header for /etc/network/interfaces
|
- name: INTERFACES | Create Ansible header for /etc/network/interfaces
|
||||||
template:
|
template:
|
||||||
src: "{{ item }}"
|
src: "{{ lookup('first_found', findme)}}"
|
||||||
dest: "/etc/foo.conf"
|
dest: "/etc/foo.conf"
|
||||||
with_first_found:
|
vars:
|
||||||
|
findme:
|
||||||
- "{{ ansible_virtualization_type }}_foo.conf"
|
- "{{ ansible_virtualization_type }}_foo.conf"
|
||||||
- "default_foo.conf"
|
- "default_foo.conf"
|
||||||
|
|
||||||
|
- name: read vars from first file found, use 'vars/' relative subdir
|
||||||
|
include_vars: "{{lookup('first_found', findme, paths=['vars'])}}"
|
||||||
|
vars:
|
||||||
|
findme:
|
||||||
|
- '{{ansible_os_distribution}}.yml'
|
||||||
|
- '{{ansible_os_family}}.yml'
|
||||||
|
- default.yml
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RETURN = """
|
RETURN = """
|
||||||
|
|
Loading…
Reference in a new issue