Search code examples
ansibleacl

Ansible: Set an ACL to files and directories


I know how to use the acl module in ansible. It's working like a charm but not exactly like I want.

I have a log_dir variable with the exact path to log files. My goal is to set an ACL to the files and only to the parent directories up to a base directory.

For example:

Log file: /some/highly/fancy/secured/file

Log path: /some/highly/fancy/secured

Now I want an ACL up to /some but not to (for example):

/some/otherDirectory or /some/highly/fancy/A/file

Do you know how to handle this?


Solution

  • Feels super hacky, but something like this would work. I do hope there's a more elegant solution though.

      vars:
        file: /some/highly/fancy/secured/file
    
      tasks:
        - acl:
            path: "/{{ file.split('/')[1:index+2] | join('/') }}"
            # <snip>
          loop: "{{ file.split('/')[1:] }}"
          loop_control:
            index_var: index
    

    Basic idea is to use the file path split into a list to figure out how many times to loop. Then inside the loop once again split the file path into a list, and slice it from the base folder up to the loop index, and join it again into a file path. We skip the first entry in the list because it is blank, so need to adjust the index value in the list slice.