AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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 Set File Permissions 755: chmod with file Module Guide — Video Tutorial
How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
What You'll Learn
- How to Add Execute Permission 755 on Linux file with Ansible?
- Ansible Add Execute Permission
- Main Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to Add Execute Permission 755 on Linux file with Ansible?
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Add Execute Permission
- `ansible.builtin.file`
- Manage files and file properties
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
- `path` _string_ (dest, name) - file path
- `owner` _string_ - user
- `group` _string_ - group
- `mode` _raw_ - Ex: '`0644`' or '`u=rw,g=r,o=r`'
- `state` _string_ - `file`, `absent`, `directory`, `hard`, `link`, `touch`
- setype/seuser/selevel - SELinux
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`" set the user that should own the file/directory.
The parameter "`group`" set the group that should own the file/directory.
The parameter "`mode`" set 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, hard links, symlinks, or only update the access time with the "`touch`" option.
Let me also highlight that we could also specify the SELinux properties.
Links
- [ansible.builtin.file](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html)
## Playbook
How to Add Execute Permission 755 file on Linux with Ansible Playbook.
I'm going to show you how to set the `chmod +x` of an `example.sh` Linux file with Ansible.
code
- file_permission.yml
```yaml
---
- name: file module demo
hosts: all
vars:
myscript: "~/example.sh"
tasks:
- name: set execution permission
ansible.builtin.file:
dest: "{{ myscript }}"
mode: 'a+x'
```
- example.sh
```bash
#!/bin/bash
echo "Hello World"
```
execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_permission.yml
PLAY [file module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [set execution permission] *******************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
idempotency
```bash
ansible-pilot $ ansible-playbook -i virtual
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 8 min
- Category: troubleshooting
Read the full written article: Ansible Set File Permissions 755: chmod with file Module Guide
Related Video Tutorials
- Ansible chmod: Change File Permissions with file Module (Guide) — How to change file permissions (chmod) in Ansible with the file module. Set mode, owner, group, recursive permissions on files and directories.
- Ansible Create Symlink: file Module with state=link (Guide) — How to create symbolic links (symlinks) in Ansible with the file module state=link. Create, manage, remove symlinks and hard links.
- Ansible Create File with Content: copy Module content Parameter — How to create files with content in Ansible using the copy module content parameter. Write text, YAML, JSON to files without templates.
- Ansible Create Empty File: Touch Files with file Module (Guide) — How to create empty files in Ansible with the file module state=touch. Create files, set permissions and ownership on new files.
- Ansible Rename File: Move & Rename Files with copy + file Modules — How to rename files and directories in Ansible using copy and file modules. Move files, rename with register, use command module.
- Add Secondary Groups to Linux Users with Ansible Playbook — Learn how to add secondary groups to Linux users with an Ansible playbook. This step-by-step guide includes YAML configuration and execution details.