Trending September 2023 # How Does Find Module Work In Ansible With Examples? # Suggested October 2023 # Top 11 Popular | Speedmintonvn.com

Trending September 2023 # How Does Find Module Work In Ansible With Examples? # Suggested October 2023 # Top 11 Popular

You are reading the article How Does Find Module Work In Ansible With Examples? updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does Find Module Work In Ansible With Examples?

Introduction to Ansible find

Ansible find module or command as the name suggests is used to search the file, directory, or link recursively if specified and based on the single or multiple criteria like age, regular expression, or pattern, and this module is a part of the ansible-base which is included default in the ansible installation which generally works with the Unix system and to use this module with the Windows system we need to use the ansible windows module, ansible.windows.win_find.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Ansible find

The syntax for this module:

“find:”

And they can be used with single or multiple parameters, as shown below. Not all parameters are covered, but only those which are required in the regular search operation.

1. paths: List of paths to include for the search. All paths should be fully qualified.

Selects the files equal or greater than the age specified in time.

Negative age searches the files equal or less than the age specified in time.

Age can be in seconds, minutes, hours, days, or weeks (e.g. 1w – 1 week, 2d – 2days).

3. recurse: Default No.

If specified yes, then the directory searches the files recursively.

4. file_type: Searches the type based on the choice below:

file (default)

directory

link

any

5. excludes: Excludes the items based on the string or the specified pattern.

6. depth: Maximum level of the depth to search for. By default is unlimited, and if the specified recurse parameter value is no, then the depth is 1.

7. contains: Only works with the file_type=file to search the files based on their content.

8. pattern: Searches the items based on the regex pattern.

9. size:

Selects the files based on the size equal to or greater than the specified size.

If the size is negative, then find the files equal or less than the specified size.

The default size is in bytes. However, you can explicitly mention (b=bytes, k=kilobytes, m=megabytes, g=gigabytes, t=terabytes).

10. hidden: The default value is no. If specified, yes, it includes hidden files.

11. read_whole_file: The default value is no. If specified yes, the whole file is read into memory instead of the line-by-line pattern, which can impact the server performance if the file size is large.

How does find Module Work in Ansible?

The find module ships with the ansible-base, which means it is included by default for the in ansible installation, and as described in the above syntax, we can use the various parameters to search files, directories, or links.

When we work with the windows search for the items, we need to use ansible.windows.win_find module, which uses almost the same parameters as specified above.

The following playbook will search the files from the Windows servers c:temp folders.

Code:

paths: c:temp

Output:

When you run the above playbook, you won’t get any output because the output is not stored anywhere. To store the retrieved output, we need to use the register variable as shown below.

Code:

msg: “{{ myfiles }}”

Output:

Code:

Now you can see the file names in the output.

Output:

Examples of Ansible find

Given below are the examples of Ansible find:

Example #1

Recursively search the /etc. folder files older than 1 week and greater than 1kb.

Code:

Output:

Example #2

Search the *.ps1, *.txt files.

The below playbook will search the files on the multiple folders with the specific extensions (ps1 and txt) and whose size is less than 1mb, store the output in the variable, and manipulate the output by the JSON query.

Code:

An alternate way, you can use the regex pattern to search the files as shown below.

Code:

use_regex: yes

Output:

Example #3

Search all the directories from the root folder, including the hidden directories.

Code:

The above playbook searches the root folder directories, including hidden directories.

Example #4

Exclude folders.

The below playbook will search all the directories from the /etc. folders and will exclude 3 directories (python3, selinux, and lvm).

Code:

Conclusion

Ansible builtin module find and the windows module win_find work like the shell, batch, or PowerShell language that we use for searching the files or directories, but we don’t need to manipulate commands in ansible to get the desired search result; but instead, we can use the parameters that directly help us to search the files and the folders like file age, size, etc.

Recommended Articles

This is a guide to Ansible find. Here we discuss the introduction; how does find module work in ansible? And examples, respectively. You may also have a look at the following articles to learn more –

You're reading How Does Find Module Work In Ansible With Examples?

Update the detailed information about How Does Find Module Work In Ansible With Examples? on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!