Here are some key Ansible modules specifically designed for file management:
-
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' -
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' -
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 -
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 -
synchronize: A wrapper aroundrsyncfor synchronizing files between the control machine and remote hosts.- name: Synchronize files to the remote host synchronize: src: /local/path/ dest: /remote/path/ -
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 -
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' -
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.
