The file names as a result of a call to the find module are stored in:
- a dict, containing (among others):
- a list named
files
, each element being:- a dict, containing (among others)
- an element named
path
with the value of the absolute path name of the file.
- an element named
- a dict, containing (among others)
- a list named
Code:
- name: Find all files of a pattern
ansible.builtin.find:
paths:
- /var/tmp/find-test
patterns:
- "file-*.txt"
register: __register_find_result
changed_when: no
Output:
__register_find_result: {
"changed": false,
"examined": 49,
"failed": false,
"files": [
{
"atime": 1644591074.602834,
"ctime": 1644591071.2867982,
"dev": 64770,
...
"path": "/var/tmp/find-test/file-01.txt",
...
},
{
"atime": 1644591061.6116943,
"ctime": 1644591045.2365181,
"dev": 64770,
...
"path": "/var/tmp/find-test/file-02.txt",
...
},
{
"atime": 1715094703.749879,
"ctime": 1715095207.574177,
"dev": 64770,
...
"path": "/var/tmp/find-test/extracted/file-03.txt",
...
},
],
"matched": 3,
"msg": "All paths examined",
"skipped_paths": {}
}
You can access a single element of the variable find_result
as follows:
- name: Show the first path of variable find_result
ansible.builtin.debug:
msg: "{{ __register_find_result.files[0].path }}"
Output:
ok: [localhost] => {
"msg": "/var/tmp/file-01.txt"
}
You can copy the resulting absolute file names into a new list as follows:
- name: Set fact of absolute file names from the find result
ansible.builtin.set_fact:
__fact_filenames: >-
{{ __register_find_result.files
| map(attribute = 'path')
| sort }}
- name: Display the resulting list of absolute file names
ansible.builtin.debug:
var: __fact_filenames
Output:
TASK [Display the resulting list of absolute file names] ***********************************************************************************
ok: [localhost] =>
__fact_filenames:
- /var/tmp/find-test/extracted/file-03.txt
- /var/tmp/find-test/file-01.txt
- /var/tmp/find-test/file-02.txt
You can also filter the resulting list of absolute file names, for example for excluding a certain directory, as follows:
- name: Set fact of absolute file names from the find result, excluding files from a certain directory, for example `extracted`
ansible.builtin.set_fact:
__fact_filenames: >-
{{ __register_find_result.files
| map(attribute = 'path')
| reject('contains', 'extracted/')
| sort }}
- name: Display the resulting list of absolute file names, excluding the string 'extracted/'
ansible.builtin.debug:
var: __fact_filenames
Output:
TASK [Display the resulting list of absolute file names] ***********************************************************************************
ok: [localhost] =>
__fact_filenames:
- /var/tmp/find-test/file-01.txt
- /var/tmp/find-test/file-02.txt
You can copy the resulting relative file names into a new list as follows:
- name: Set fact of relative file names from the find result
ansible.builtin.set_fact:
__fact_filenames_relative: >-
{{ __register_find_result.files
| map(attribute = 'path')
| map('basename')
| list
| sort }}
- name: Display the resulting list of relative file names
ansible.builtin.debug:
var: __fact_filenames_relative
Output:
TASK [Display the resulting list of relative file names] ***********************************************************************************
ok: [localhost] =>
__fact_filenames_relative:
- file-01.txt
- file-02.txt
- file-03.txt
You can copy the highest absolute file name into a new variable as follows:
- name: Set fact of the highest absolute file name from the find result
ansible.builtin.set_fact:
__fact_filenames_highest: >-
{{ __register_find_result.files
| map(attribute = 'path')
| sort | last }}
- name: Display the highest absolute file name
ansible.builtin.debug:
var: __fact_filenames_highest
Output:
TASK [Display the highest absolute file name] **********************************************************************************************
ok: [localhost] =>
__fact_filenames_highest: /var/tmp/find-test/file-02.txt
You can copy the highest relative file name into a new variable as follows:
- name: Set fact of the highest file name (basename) from the find result
ansible.builtin.set_fact:
__fact_filenames_highest_relative: >-
{{ __register_find_result.files
| map(attribute = 'path')
| map('basename')
| sort | last }}
- name: Display the highest relative file name
ansible.builtin.debug:
var: __fact_filenames_highest_relative
Output:
TASK [Display the highest relative file name] **********************************************************************************************
ok: [localhost] =>
__fact_filenames_highest_relative: file-03.txt
You can copy the highest relative file name excluding files from a certain directory, for example extracted
, into a new variable, as follows:
- name: Set fact of the highest file name (basename) from the find result, excluding files from a certain directory
ansible.builtin.set_fact:
__fact_filenames_highest_relative_filtered: >-
{{ __register_find_result.files
| map(attribute = 'path')
| reject('contains', 'extracted/')
| map('basename')
| sort | last }}
- name: Display the highest relative file name, skipping those from directory 'extracted/'
ansible.builtin.debug:
var: __fact_filenames_highest_relative_filtered
Output:
TASK [Display the highest relative file name, skipping those from directory 'extracted/'] **********************************************************************************************
ok: [localhost] =>
__fact_filenames_highest_relative_filtered: file-02.txt