Hi there,
I am trying to deploy several drupal sites with a playbook. So far, I am only trying out some ansible features and I am stuck with how best structure my playbooks. The approach I would prefer is not working so I am hoping you can help me out.
Before going into the detail, let me explain why I prefer approach 1) over 2):
- Less verbose, just use with_items once when including the tasks-file
- Variable names would make more sense because I would be able to use ${site} everywhere instead of ${item} (in templates for instance)
- Included task-file can be reused for just deploying one site instead of many (it will depend on how you include it in the play-book)
Here you have the details, please let me know if I am missing something:
I use a sites_available.yml file that describes the different sites that should be deployed. Something like:
---
sites_available:
files: some/dir
files: some/other/dir
...
Then, I just have to execute some tasks with each one of this $sites_available. I tried two different ways of using with_items:
1) Preferred approach: Include task file with_items (I couldn't make this work):
# Playbook: deploy-all.yml
---
... hosts, user and all ...
tasks:
- include: tasks/deploy-drupal.yml site=${item}
with_items: ${sites_available}
... handlers and all ...
And then in the included file:
# Tasks file: deploy-drupal.yml
---
... some tasks ...
- name: Copy conf file to sites-available for ${site}
action: template src=templates/etc-apache2-sites-available-items.j2 dest=/etc/apache2/sites-available/${site.fqdn}.conf
notify: restart apache
... some more tasks ...
Problem: it ignores with_items, the above task would just create one ${site.fqdn}.conf file without any variable substitution.
2) Working approach: just include the task file (without with_items) and then do with_items in each one of the tasks
# Playbook: deploy.yml
---
... hosts, user and all ...
tasks:
- include: tasks/deploy-drupal-with-items.yml
... handlers and all ...
# Tasks file: deploy-drupal-with-items.yml
---
... some tasks (all using with_items)...
- name: Copy conf file to sites-available for ${item}
action: template src=templates/etc-apache2-sites-available-items.j2 dest=/etc/apache2/sites-available/${item.fqdn}.conf
with_items: ${sites_available}
notify: restart apache
... some more tasks (all using with_items)...
Thanks,
Eduardo