DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Managing ACLs Using Ansible

There are some files that need to be created on all app servers in Stratos DC. The Nautilus DevOps team want these files to be owned by user root only however, they also want that the app specific user to have a set of permissions on these files. All tasks must be done using Ansible only, so they need to create a playbook. Below you can find more information about the task.

Create a playbook named playbook.yml under /home/thor/ansible directory on jump host, an inventory file is already present under /home/thor/ansible directory on Jump Server itself.

  1. Create an empty file blog.txt under /opt/dba/ directory on app server 1. Set some acl properties for this file. Using acl provide read '(r)' permissions to group tony (i.e entity is tony and etype is group).
  2. Create an empty file story.txt under /opt/dba/ directory on app server 2. Set some acl properties for this file. Using acl provide read + write '(rw)' permissions to user steve (i.e entity is steve and etype is user).
  3. Create an empty file media.txt under /opt/dba/ on app server 3. Set some acl properties for this file. Using acl provide read + write '(rw)' permissions to group banner (i.e entity is banner and etype is group).

Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure the playbook works this way, without passing any extra arguments.


🎯 The Problem

Imagine you're on a DevOps team managing multiple application servers. Your security team says:

"These config files need to be owned by root — but specific users/groups still need controlled access to them."

Doing this manually on each server is a nightmare. Doing it repeatably, idempotently, and at scale — that's where Ansible shines.

In this post, we'll build a playbook that creates files and sets POSIX ACLs on three app servers. No manual setfacl commands. No SSH-ing into each box. Just one command.


🏗️ The Scenario

Three application servers, each hosting a different role. Each server needs one file in /opt/dba/ with a specific ACL:

Server File ACL Requirement
stapp01 /opt/dba/blog.txt Group tony → read (r)
stapp02 /opt/dba/story.txt User steve → read + write (rw)
stapp03 /opt/dba/media.txt Group banner → read + write (rw)

All files must be owned by root:root.


📚 Prerequisites: What You Need to Know

What is a POSIX ACL?

ACL = Access Control List. It's an extension to the standard Linux permission model (owner/group/other). ACLs let you grant permissions to additional users or groups without changing the file's owner or group.

Example:

group:tony:r--
Enter fullscreen mode Exit fullscreen mode

→ The group tony can read this file, even though root owns it and its default group is root.

Why use ansible.posix.acl?

Because it's:

  • Idempotent — running it twice doesn't break anything
  • Cross-distro — works with setfacl under the hood
  • Readable — declarative YAML instead of shell commands

🛠️ Step 1: Set Up the Working Directory

First, SSH into the jump host and go to the Ansible workspace.

cd /home/thor/ansible
ls -la
Enter fullscreen mode Exit fullscreen mode

Output:

total 20
drwxr-xr-x 2 thor thor 4096 Nov 17 09:44 .
drwx------ 1 thor thor 4096 Nov 17 09:44 ..
-rw-r--r-- 1 thor thor   36 Nov 17 09:44 ansible.cfg
-rw-r--r-- 1 thor thor  219 Nov 17 09:44 inventory
Enter fullscreen mode Exit fullscreen mode

Let's inspect the inventory:

cat inventory
Enter fullscreen mode Exit fullscreen mode

Output:

stapp01 ansible_host=stapp01 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=stapp02 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=stapp03 ansible_ssh_pass=BigGr33n ansible_user=banner
Enter fullscreen mode Exit fullscreen mode

💡 Note: This is a flat inventory (no [group] headers). That means we can't use hosts: app in our playbook — we'll target each host by name instead.


🛠️ Step 2: Verify Connectivity to All Servers

Before touching the playbook, confirm Ansible can reach every node.

ansible -i inventory stapp01 -m ping
ansible -i inventory stapp02 -m ping
ansible -i inventory stapp03 -m ping
Enter fullscreen mode Exit fullscreen mode

Output:

stapp01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
stapp02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
stapp03 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
Enter fullscreen mode Exit fullscreen mode

✅ All three respond with pong.


🛠️ Step 3: Verify the ansible.posix Collection

The acl module lives in the ansible.posix collection, not core Ansible. Check it's installed:

ansible-galaxy collection list | grep posix
Enter fullscreen mode Exit fullscreen mode

Output:

ansible.posix                 1.5.4
Enter fullscreen mode Exit fullscreen mode

✅ Present. If it were missing, we'd install it with:

ansible-galaxy collection install ansible.posix
Enter fullscreen mode Exit fullscreen mode

🛠️ Step 4: Verify Dependencies on the Target

The acl module calls the setfacl binary under the hood. Confirm it exists on the target:

ansible -i inventory stapp01 -m shell -a "which setfacl" -b
Enter fullscreen mode Exit fullscreen mode

Output:

stapp01 | CHANGED | rc=0 >>
/bin/setfacl
Enter fullscreen mode Exit fullscreen mode

✅ setfacl is available.

Next, confirm the target directory /opt/dba exists (the file module will fail if the parent directory is missing):

ansible -i inventory stapp01 -m shell -a "ls -ld /opt/dba" -b
Enter fullscreen mode Exit fullscreen mode

Output:

stapp01 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 4096 Nov 17 09:44 /opt/dba
Enter fullscreen mode Exit fullscreen mode

✅ Directory exists and is owned by root.

🐛 Gotcha to watch: If /opt/dba didn't exist, add a file: state=directory task first. It's idempotent, so it's safe to include even when the directory already exists.


🛠️ Step 5: Write the Playbook

Create the playbook at /home/thor/ansible/playbook.yml:

---
- name: Configure ACLs on App Server 1
  hosts: stapp01
  become: yes

  tasks:
    - name: Create empty file blog.txt
      file:
        path: /opt/dba/blog.txt
        state: touch
        owner: root
        group: root
        mode: '0644'

    - name: Set ACL for group tony (read)
      ansible.posix.acl:
        path: /opt/dba/blog.txt
        entity: tony
        etype: group
        permissions: r
        state: present

- name: Configure ACLs on App Server 2
  hosts: stapp02
  become: yes

  tasks:
    - name: Create empty file story.txt
      file:
        path: /opt/dba/story.txt
        state: touch
        owner: root
        group: root
        mode: '0644'

    - name: Set ACL for user steve (read + write)
      ansible.posix.acl:
        path: /opt/dba/story.txt
        entity: steve
        etype: user
        permissions: rw
        state: present

- name: Configure ACLs on App Server 3
  hosts: stapp03
  become: yes

  tasks:
    - name: Create empty file media.txt
      file:
        path: /opt/dba/media.txt
        state: touch
        owner: root
        group: root
        mode: '0644'

    - name: Set ACL for group banner (read + write)
      ansible.posix.acl:
        path: /opt/dba/media.txt
        entity: banner
        etype: group
        permissions: rw
        state: present
Enter fullscreen mode Exit fullscreen mode

🔍 Breaking Down the Key Parameters

Parameter Meaning
hosts: stapp01 Target this host by name (no groups in inventory)
become: yes Escalate to root — needed for /opt/dba and setfacl
state: touch Create file if missing, update mtime if present
entity: tony The user or group name the ACL applies to
etype: group group or user — must match the entity type
permissions: r r = read, rw = read+write
state: present Ensure the ACL entry exists

🛠️ Step 6: Syntax Check

Always validate YAML before running:

ansible-playbook -i inventory playbook.yml --syntax-check
Enter fullscreen mode Exit fullscreen mode

Output:

playbook: playbook.yml
Enter fullscreen mode Exit fullscreen mode

✅ Syntax is valid.


🛠️ Step 7: Run the Playbook

ansible-playbook -i inventory playbook.yml
Enter fullscreen mode Exit fullscreen mode

Output:

PLAY [Configure ACLs on App Server 1] ***************************************************

TASK [Gathering Facts] ******************************************************************
ok: [stapp01]

TASK [Create empty file blog.txt] *******************************************************
changed: [stapp01]

TASK [Set ACL for group tony (read)] ****************************************************
changed: [stapp01]

PLAY [Configure ACLs on App Server 2] ***************************************************

TASK [Gathering Facts] ******************************************************************
ok: [stapp02]

TASK [Create empty file story.txt] ******************************************************
changed: [stapp02]

TASK [Set ACL for user steve (read + write)] ********************************************
changed: [stapp02]

PLAY [Configure ACLs on App Server 3] ***************************************************

TASK [Gathering Facts] ******************************************************************
ok: [stapp03]

TASK [Create empty file media.txt] ******************************************************
changed: [stapp03]

TASK [Set ACL for group banner (read + write)] ******************************************
changed: [stapp03]

PLAY RECAP ******************************************************************************
stapp01  : ok=3  changed=2  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
stapp02  : ok=3  changed=2  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
stapp03  : ok=3  changed=2  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
Enter fullscreen mode Exit fullscreen mode

🎉 All three servers: failed=0.


🛠️ Step 8: Verify the ACLs

Now let's confirm the ACLs actually landed. Use getfacl through Ansible for a repeatable, centralised check.

ansible -i inventory stapp01 -m shell -a "getfacl /opt/dba/blog.txt" -b
ansible -i inventory stapp02 -m shell -a "getfacl /opt/dba/story.txt" -b
ansible -i inventory stapp03 -m shell -a "getfacl /opt/dba/media.txt" -b
Enter fullscreen mode Exit fullscreen mode

Server 1 — blog.txt

stapp01 | CHANGED | rc=0 >>
# file: opt/dba/blog.txt
# owner: root
# group: root
user::rw-
group::r--
group:tony:r--
mask::r--
other::r--
Enter fullscreen mode Exit fullscreen mode

✅ group:tony:r-- — group tony has read access.

Server 2 — story.txt

stapp02 | CHANGED | rc=0 >>
# file: opt/dba/story.txt
# owner: root
# group: root
user::rw-
user:steve:rw-
group::r--
mask::rw-
other::r--
Enter fullscreen mode Exit fullscreen mode

✅ user:steve:rw- — user steve has read+write.

Server 3 — media.txt

stapp03 | CHANGED | rc=0 >>
# file: opt/dba/media.txt
# owner: root
# group: root
user::rw-
group::r--
group:banner:rw-
mask::rw-
other::r--
Enter fullscreen mode Exit fullscreen mode

✅ group:banner:rw- — group banner has read+write.

All three files show # owner: root and # group: root — matching the requirement.


🧠 Understanding the getfacl Output

Line Meaning
user::rw- Owner (root) has read+write
group::r-- Owning group (root) has read only
group:tony:r-- / user:steve:rw- Named ACL entry — the specific entity we granted
mask::r-- / mask::rw- Effective permissions mask — caps all named ACL entries
other::r-- Everyone else: read only

Why the mask matters

The mask is the upper bound on what any named user/group ACL entry can actually do.

  • On server 1, the ACL entry needs only r, so mask::r--.
  • On servers 2 & 3, the ACL entry needs rw, so mask::rw-.

Ansible calculates the mask automatically when applying ACL entries — you don't have to specify it. That's one of the reasons ansible.posix.acl is preferable to raw shell setfacl commands.


🛠️ Step 9: Prove Idempotency

Run the playbook a second time:

ansible-playbook -i inventory playbook.yml
Enter fullscreen mode Exit fullscreen mode

You'll see the ACL tasks now report ok instead of changed:

TASK [Set ACL for group tony (read)] ****************************************************
ok: [stapp01]
Enter fullscreen mode Exit fullscreen mode

Why this matters: Idempotency means re-running the playbook won't re-apply ACLs unnecessarily. It also means the playbook is safe to use in a scheduled pipeline (e.g., cron or CI/CD) where it might run every hour without breaking anything.


🚀 Key Takeaways

Lesson Why It Matters
Target hosts individually in flat inventories hosts: stapp01 works when no groups exist
Always become: yes for root-owned paths Writing to /opt/dba requires privileges
Use state: touch for "create if missing" Won't fail if file already exists
ansible.posix.acl handles mask calculation You avoid a whole class of subtle permission bugs
Verify with getfacl Centralised checks confirm the ACLs really landed
Re-run to test idempotency Catches issues before they hit production

🎁 Bonus: Full Playbook (Copy-Paste Ready)

---
- name: Configure ACLs on App Server 1
  hosts: stapp01
  become: yes
  tasks:
    - name: Create empty file blog.txt
      file:
        path: /opt/dba/blog.txt
        state: touch
        owner: root
        group: root
        mode: '0644'
    - name: Set ACL for group tony (read)
      ansible.posix.acl:
        path: /opt/dba/blog.txt
        entity: tony
        etype: group
        permissions: r
        state: present

- name: Configure ACLs on App Server 2
  hosts: stapp02
  become: yes
  tasks:
    - name: Create empty file story.txt
      file:
        path: /opt/dba/story.txt
        state: touch
        owner: root
        group: root
        mode: '0644'
    - name: Set ACL for user steve (read + write)
      ansible.posix.acl:
        path: /opt/dba/story.txt
        entity: steve
        etype: user
        permissions: rw
        state: present

- name: Configure ACLs on App Server 3
  hosts: stapp03
  become: yes
  tasks:
    - name: Create empty file media.txt
      file:
        path: /opt/dba/media.txt
        state: touch
        owner: root
        group: root
        mode: '0644'
    - name: Set ACL for group banner (read + write)
      ansible.posix.acl:
        path: /opt/dba/media.txt
        entity: banner
        etype: group
        permissions: rw
        state: present
Enter fullscreen mode Exit fullscreen mode

🏁 Conclusion

You've just automated POSIX ACL management across three servers with ~40 lines of YAML. The playbook is:

  • ✅ Declarative — describes the end state, not the steps
  • ✅ Idempotent — safe to run repeatedly
  • ✅ Verified — we confirmed with getfacl
  • ✅ Scalable — adding a 4th server is copy-paste

The pattern here generalises: any time you need per-entity permissions on files, ansible.posix.acl is your tool. Combine it with file for creation, and you've got a clean, production-ready automation.

Top comments (0)