How to use find & exec in a Ansible Playbook?

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?

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

Thanks in advance.

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

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

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

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

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.

- name: Find all directories that match text
    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

Hope this helps.

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