AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible file Module: Create Files, Directories, Symlinks (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: security-compliance

How to use Ansible file module to create files, directories, and symlinks. Set permissions, ownership, and state. Remove files and manage filesystem objects with playbook examples.

Ansible file Module: Create Files, Directories, Symlinks (Complete Guide)

The ansible.builtin.file module manages filesystem objects — create directories, set permissions, create symlinks, and remove files. It's one of the most commonly used modules in Ansible playbooks.

Create a Directory

Create Nested Directories

Create Multiple Directories

Create an Empty File

Touch Without Changing Existing Content

Create Symlinks

Hard Links

Set Permissions and Ownership

Special Permissions

Remove Files and Directories

Set File Attributes

Conditional File Operations

Common Patterns

Deploy Application Directory Structure

Cleanup Old Files

file Module Parameters Referencepath (required) — Target path • state — file, directory, link, hard, touch, absent • src — Source for links • owner — File owner • group — File group • mode — Permissions (e.g., '0755', 'u+rwx,g+rx,o+rx') • recurse — Apply owner/group/mode recursively (directories only) • attributes — File attributes (chattr flags) • follow — Follow symlinks (default: yes) • force — Force link creation even if source doesn't exist

FAQ

How do I create a directory in Ansible?

Use ansible.builtin.file with state: directory: ansible.builtin.file: path=/opt/myapp state=directory mode='0755'. Ansible automatically creates parent directories.

How do I set file permissions in Ansible?

Use the mode parameter on the file module: ansible.builtin.file: path=/opt/myapp/script.sh mode='0755'. Use octal notation (always quote it) or symbolic notation like u+rwx,g+rx.

What is the difference between touch and file state?

state: touch creates the file if it doesn't exist and updates timestamps (like the touch command). state: file only sets attributes on existing files — it fails if the file doesn't exist.

How do I create a symlink in Ansible?

Use state: link with src and dest: ansible.builtin.file: src=/opt/app/v2 dest=/opt/app/current state=link. Use state: hard for hard links.

How do I delete a file or directory in Ansible?

Set state: absent: ansible.builtin.file: path=/tmp/old-file state=absent. This works for files, directories (recursively), and symlinks.

Conclusion

The ansible.builtin.file module is essential for managing filesystem objects in Ansible. Use it to create directories, set permissions, manage symlinks, and clean up files — all idempotently.

Related ArticlesAnsible copy Module: Copy Files to Remote HostsAnsible template Module: Deploy Jinja2 TemplatesAnsible find Module: Search for Files

Category: security-compliance

Browse all Ansible tutorials · AnsiblePilot Home