I'm new to Ansible. I have the following directory structure for my Ansible playbooks:
$HOME/playbooks
├── project1
├── project2
└── roles
I collect my commonly used roles such as my "nginx" role in the role directory and then I'll reference these roles in my project directories by adding the following line to each project's ansible.cfg file:
# $HOME/playbooks/project1/ansible.cfg
[defaults]
roles_path = $HOME/playbooks/roles
How do you configure your individual Ansible playbook tasks and and common role tasks when a common role task depends on which server the task is being run on? In my case, I have a "webservers.yml" playbook for configuring my web server and a "fileservers.yml" playbook for configuring my file server. My website will run on my web server but my static and media files will be served from a separate file server. I currently include the following task in my nginx role's main/task.yml file:
# $HOME/playbooks/roles/nginx/tasks/main.yml
- name: create nginx config file
template: src=nginx.conf.j2
dest=/etc/nginx/sites-available/{{ domain }}.conf
notify: restart nginx
become: True
tags: nginx
The problem is that the nginx config files on each server will be a little different since (I think) the nginx process running on my web server will need to redirect static file requests to the nginx process running on my file server. Should I leave the "create nginx config file" task in my common nginx role and have it copy a minimal template onto each server but then call that task again in my webserver and fileserver playbooks and have each one deploy a server-specific version of the config file (e.g. nginx.webserver.conf.j2 versus nginx.fileserver.conf.j2)? Should I just leave the create config file out of my main role task and just execute it in each playbook? (This approach seems wrong to me) Or is there a more robust way to do this?
Thanks!