# How to use find & exec in a Ansible Playbook?

**URL:** <https://forum.ansible.com/t/how-to-use-find-exec-in-a-ansible-playbook/1622>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [October 11, 2023, 6:37am UTC](https://forum.ansible.com/t/how-to-use-find-exec-in-a-ansible-playbook/1622 "2023-10-11T06:37:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![myservertechnik](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@myservertechnik](https://forum.ansible.com/u/myservertechnik)\
**Post date:** [October 11, 2023, 6:37am UTC](https://forum.ansible.com/t/how-to-use-find-exec-in-a-ansible-playbook/1622/1 "2023-10-11T06:37:22Z")

</div>

**How to use find & exec in a Ansible Playbook?**.  
Hello Community,

i am converting a Bash Script into an Ansible Playbook.  
How would you implement the following bash command in Ansible?

```auto
 find /home/user/ -name "some_text*" -type d -exec chmod 750 {} \;

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![myservertechnik](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@myservertechnik](https://forum.ansible.com/u/myservertechnik)\
**Post date:** [October 11, 2023, 7:47am UTC](https://forum.ansible.com/t/how-to-use-find-exec-in-a-ansible-playbook/1622/2 "2023-10-11T07:47:25Z")

</div>

I found a solution myself, I hope this can help someone in the future

```auto
---
- hosts: all,localhost
  gather_facts: no
  become: no

  tasks: 
  - name: collect files
    ansible.builtin.find:
      paths: "/home/user/"
      patterns: "some_text*"
      hidden: true
      recurse: true
      file_type: file
    register: collected_files

  - name: change rights for collected files
    ansible.builtin.file:
      path: "{{ item.path }}"
      mode: "640"
    with_items: "{{ collected_files.files }}"
    loop_control:
      label: "{{ item.path }}"

```

---

<div class="post-metadata">

**Author:** ![oranod](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/oranod/32/1434_2.png) [@oranod](https://forum.ansible.com/u/oranod)\
**Post date:** [October 11, 2023, 7:51am UTC](https://forum.ansible.com/t/how-to-use-find-exec-in-a-ansible-playbook/1622/3 "2023-10-11T07:51:01Z")

</div>

Hi @myservertechnik I was just about to reply and say use the builtin find and file modules. I was also working on an example for find.

```auto
- name: Find all directories that match text
  ansible.builtin.find:
    paths: "{{ target_directory }}"
    patterns: "some_text*"
    recurse: true
    file_type: directory

```

In this example I’ve created a variable for the path named “target\_directory” which is convenient if you need to specify that more than once. I also noticed in your original command you set `-type d` which I believe corresponds to `file_type: directory`.

As I’m sure you’re aware there are plenty of examples of `chmod` in the docs: [ansible.builtin.file module – Manage files and file properties — Ansible Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html#examples)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 10, 2023, 7:51am UTC](https://forum.ansible.com/t/how-to-use-find-exec-in-a-ansible-playbook/1622/4 "2023-11-10T07:51:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
