I try to generate with ansible a folder structure for a webserver with users and linked projects.
The playbook works. But with more users and projects it gets quite complex to handle.
How can I simplify my code or vars sections?
I read a hint about using blocks so that all tasks inside it could use the same loop.
Is that the way to go?
The docs on blocks don't have a lot of examples. Any hint or link to examples is most appreciated.
Of course if you have any other pointers, please let me know.
---
- hosts: localhost
become: yes
become_method: sudo
# check_mode: yes
vars:
project_base_path: /usr/src/project
www_base_path: "/var/www/{{ansible_hostname}}"
projects:
mars: "{{ project_base_path }}/mars"
venusnew: "{{ project_base_path }}/venus/new"
venusold: "{{ project_base_path }}/venus/old"
users:
goku:
present: true
pw: #vault
luffy:
present: true
pw: #vault
user_projects:
- uname: goku
uproject:
- lname: mars
spath: "{{ projects.mars }}"
- uname: luffy
uproject:
- lname: venusnew
spath: "{{ projects.venusnew }}"
- lname: project
spath: "{{ projects.venusold }}"
tasks:
- name: check www path | Check if the path is available or create it
file:
path: "{{ www_base_path }}"
owner: www-data
group: www-data
state: directory
- name: check user www directory | Check if the directory of the user is available or create it
file:
owner: www-data
group: www-data
state: directory
path: "{{ www_base_path }}/{{ item.key}}"
with_dict: "{{ users }}"
- name: check projects in usr src | Check if the directory of the user is available or create it
file:
owner: www-data
group: www-data
state: directory
path: "{{ item.value}}"
with_dict: "{{ projects }}"
- name: link projects to users | Create links for the users
file:
state: link
# force: yes
owner: www-data
group: www-data
src: "{{ item.1.spath }}"
dest: "{{ www_base_path }}/{{ item.0.uname }}/{{ item.1.lname }}"
with_subelements:
- "{{ user_projects }}"
- uproject