What are the key Ansible modules for file management?

Here are some key Ansible modules specifically designed for file management:

  1. file: Manages file properties and permissions, including creating or removing files and directories.

    - name: Ensure a directory exists
      file:
        path: /path/to/directory
        state: directory
        mode: '0755'
  2. copy: Copies files from the control machine to the target hosts.

    - name: Copy a file to the target host
      copy:
        src: /local/path/to/file.txt
        dest: /remote/path/to/file.txt
        owner: user
        group: group
        mode: '0644'
  3. template: Processes a Jinja2 template and copies it to the target host, allowing for dynamic content.

    - name: Deploy a configuration file from a template
      template:
        src: template.j2
        dest: /etc/config.conf
  4. fetch: Copies files from remote hosts back to the control machine.

    - name: Fetch a file from the remote host
      fetch:
        src: /remote/path/to/file.txt
        dest: /local/path/to/destination/
        flat: yes
  5. synchronize: A wrapper around rsync for synchronizing files between the control machine and remote hosts.

    - name: Synchronize files to the remote host
      synchronize:
        src: /local/path/
        dest: /remote/path/
  6. unarchive: Unpacks compressed files (e.g., .zip, .tar.gz) on the target host.

    - name: Unarchive a tar file
      unarchive:
        src: /local/path/to/archive.tar.gz
        dest: /remote/path/
        remote_src: no
  7. lineinfile: Manages lines in a file, allowing you to add, modify, or remove specific lines.

    - name: Ensure a line is present in a file
      lineinfile:
        path: /etc/config.conf
        line: 'new_setting=value'
  8. replace: Replaces specific strings in a file with new values.

    - name: Replace a string in a file
      replace:
        path: /etc/config.conf
        regexp: 'old_value'
        replace: 'new_value'

These modules provide a comprehensive set of tools for managing files and directories in Ansible, enabling efficient automation of file-related tasks.

0 Comments

no data
Be the first to share your comment!