Ansible Pilot

Change file permission - Ansible module file

Changing File and Directory Permissions with Ansible: A Live Demo

Learn how to change file or directory permissions with Ansible. Watch a live demo and see simple Ansible code to change chmod and group of a test.txt file.
September 6, 2021
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

YouTube Video

How to change file or directory permission with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible change file/directory permission

Today we’re talking about the Ansible module file. The full name is ansible.builtin.file, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years. It works in a different variety of operating systems. It manages files and file properties. For Windows targets, use the ansible.windows.win_file module instead.

Main Parameters

This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. The parameter “owner” sets the user that should own the file/directory. The parameter “group” sets the group that should own the file/directory. The parameter “mode” sets the permissions in the UNIX way of the file/directory. The state defines the type of object we are modifying, the default is “file” but we could handle also directories, hardlink, symlink, or only update the access time with the “touch” option. Let me also highlight that we could also specify the SELinux properties.

demo

Let’s jump in a real-life playbook to change file permission with Ansible.

code

---
- name: file module demo
  hosts: all
  vars:
    myfile: "/home/devops/test.txt"
  become: false
  tasks:
    - name: check permission
      ansible.builtin.file:
        path: "{{ myfile }}"
        owner: "devops"
        group: "users"
        mode: '0777'

code with ❤️ in GitHub

Recap

Now you know how to change file or directory permission with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases