Copying Files by Extension with Ansible | Generated by AI

Home 2025.12

Question: How to use an Ansible playbook to copy *.sh, *.ksh, and *.py files from a source directory to a destination directory recursively?

Answer:

To copy only files with specific extensions (.sh, .ksh, .py) recursively from a source directory to a destination directory while preserving the directory structure, use a combination of the ansible.builtin.find and ansible.builtin.copy modules. The copy module alone does not support filtering by multiple extensions during recursive copies.

This requires two tasks (as you mentioned it can spawn into two):

  1. Use find to locate all matching files recursively.
  2. Loop over the results and copy each file, preserving the relative path.

Here is an example playbook (assuming the source and destination are on the control node/localhost; adjust for remote hosts as needed):

---
- name: Copy specific script files recursively
  hosts: localhost  # Change to your target hosts
  vars:
    source_dir: /path/to/source
    dest_dir: /path/to/destination

  tasks:
    - name: Find all .sh, .ksh, and .py files recursively
      ansible.builtin.find:
        paths: ""
        recurse: yes
        file_type: file
        patterns:
          - "*.sh"
          - "*.ksh"
          - "*.py"
      register: script_files

    - name: Copy matching files to destination, preserving directory structure
      ansible.builtin.copy:
        src: ""
        dest: "/"
        mode: preserve  # Optional: preserve original permissions
      loop: ""
      when: script_files.matched > 0

Key Explanations:

Alternative: Flatten all files into one destination directory (no subdirs preserved)

Change the dest in the copy task to:

dest: "/"

References:


Back

x-ai/grok-4.1-fast

Donate