So I was recently frustrated by how hard it is to do the a simple find and change permissions on a list of files using Ansible. I achieved it two ways, both work, but I want to know if someone has a better method.
My first method, I just ask Ansible to do the same as I would do in bash, like so:
tasks:
- shell: ls -1 "{{ SomeDir }}"
register: file_names
- command: chmod 754 "{{ SomeDir }}/{{ item }}"
loop: "{{ file_names.stdout_lines }}"
I came back to it today to try and find a more Ansible native way of doing the task and came up with this, but it just seems so clunky for such a simple task:
tasks:
- name: "Find all files in the directory"
find:
paths: "{{ SomeDir }}"
file_type: file
register : find_output
- name: "loop through the .files output and then pick out the .path attribute"
file:
path: "{{ item.path }}"
mode: '0754'
loop:
"{{ find_output.files }}"
I'm not great at output manipulation, as you can see. I would have loved it if I could have done something like {{ find_output.files.path }}, but I could not get lookups or flatten operations to work, maybe someone can set me straight.