meza
meza copied to clipboard
Create config variables for file modes, user, group
At present many Ansible tasks look like:
- name: Copy something
copy:
src: /path/to/src/XYZ
dest: /path/to/dest/XYZ
mode: 0755
owner: someowner
group: somegroup
Where the mode, owner, and group are hard-coded. These could easily conflict with other tasks/playbooks that interact with this file or directory. It would be good to have config variables like xyz_mode = 0755
and xyz_owner = someowner
such that the above task could be rewritten:
- name: Copy something
copy:
src: /path/to/src/XYZ
dest: /path/to/dest/XYZ
mode: "{{ xyz_mode }}"
owner: "{{ xyz_owner }}"
group: "{{ xyz_group }}"
Perhaps this could be taken a step further, such that the configuration variable could be written as an object that looks like:
file_xyz:
src: /path/to/src/xyz
dest: /path/to/dest/xyz
mode: 0755
owner: someowner
group: somegroup
And the task could be rewritten:
- name: Copy something
copy:
src: "{{ file_xyz.src }}"
dest: "{{ file_xyz.dest }}"
mode: "{{ file_xyz.mode }}"
owner: "{{ file_xyz.owner }}"
group: "{{ file_xyz.group }}"
That may be taking it too far, though.
Add recursive: yes|no
to config?
Also go through roles and ensure all file mode changing tasks have a user/group/mode specified.
Even better would be:
m_paths:
data:
path: "/opt/data-meza"
owner: meza-ansible
group: wheel
mode: "0775"
servers: all
uploads:
path: "/opt/data-meza/uploads"
better_path: "{{ m_paths.data.path }}/uploads" # if possible to do something like this (prob not)
owner: apache
group: apache
mode: "0775"
servers: app-servers
Then could setup paths like:
- name: Setup paths
file:
path: "{{ item.path }}"
state: directory
mode: "{{ item.mode }}"
owner: "{{ item.owner }}"
group: "{{ item.group }}"
with_items: "{{ m_paths }}"
when: item.servers == "all" or inventory_hostname _is in any of item.servers_ # what if item.servers is a list?