The file module in Ansible is commonly used for various file and directory management tasks. Here are some common use cases:
-
Creating Directories: Ensure that specific directories exist on the target hosts.
- name: Create a directory file: path: /path/to/directory state: directory mode: '0755' -
Removing Files or Directories: Delete files or directories that are no longer needed.
- name: Remove a directory file: path: /path/to/directory state: absent -
Setting File Permissions: Change the permissions of files or directories to control access.
- name: Set permissions on a file file: path: /path/to/file.txt mode: '0644' -
Changing Ownership: Modify the owner and group of files or directories.
- name: Change ownership of a file file: path: /path/to/file.txt owner: user group: group -
Creating Empty Files: Create an empty file if it does not already exist.
- name: Create an empty file file: path: /path/to/empty_file.txt state: touch # Updates the timestamp or creates if not present -
Managing Symlinks: Create or remove symbolic links to files or directories.
- name: Create a symbolic link file: src: /path/to/original/file.txt dest: /path/to/symlink/file.txt state: link -
Ensuring File Absence: Ensure that a specific file does not exist on the target host.
- name: Ensure a file does not exist file: path: /path/to/unwanted_file.txt state: absent -
Setting Special File Types: Create special file types, such as sockets or devices, if needed.
- name: Create a socket file file: path: /path/to/socket state: socket
These use cases demonstrate the versatility of the file module in managing files and directories, making it an essential tool in Ansible automation workflows.
