Use case for include tasks-file with_items

1,396 views
Skip to first unread message

esclapes

unread,
Sep 27, 2012, 3:19:48 AM9/27/12
to ansible...@googlegroups.com
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:
  - fqdn: site1.com
    alias: www.site1.com
    repo: g...@github.com/...
    files: some/dir
  - fqdn: site2.com
    repo: g...@github.com/...
    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

Michael DeHaan

unread,
Sep 27, 2012, 7:50:10 AM9/27/12
to ansible...@googlegroups.com
>
> 1) Preferred approach: Include task file with_items (I couldn't make this
> work):
>
> # Playbook: deploy-all.yml

Yep, you can't make it work (include + with_items) presently because
it's not meant to.

with_items is only legal on a task because the definition of each task
is uniform across all hosts in the play, and if we were to include
variables here, that would change things.

Now, we could generally make that work with non host and group
specific variables, but I'd be worried people would expect it to work
with facts and such and it would confuse people too greatly.

So far, I *think* you're the first person to try with_items with an
include statement.

If that didn't raise an error, it /should/, IMHO. Just as this also should:

- include: foo.yml
some_option_that_does_not_exist: 1234

esclapes

unread,
Sep 27, 2012, 3:28:07 PM9/27/12
to ansible...@googlegroups.com


El jueves, 27 de septiembre de 2012 13:50:10 UTC+2, Michael DeHaan escribió:

with_items is only legal on a task because the definition of each task
is uniform across all hosts in the play, and if we were to include
variables here, that would change things.

I don't get what you mean by "uniform accross all hosts" my proposed use of with_items would still perform the tasks in a uniformed fashion. It is just a short hand for what you can already do on a task by task basis.
 
So far, I *think* you're the first person to try with_items with an
include statement.

Well, I "might" be the first, but got the idea from current docs: 

tasks:
  - include: wordpress.yml user=timmy
  - include: wordpress.yml user=alice
  - include: wordpress.yml user=bob
tasks:
   include: wordpress.yml user=$item
   with_items; $users


If that didn't raise an error, it /should/, IMHO.  

It does not raise any. That is for sure.

Thanks a lot, I am learning a lot with ansible. 

Michael DeHaan

unread,
Oct 10, 2012, 8:17:21 AM10/10/12
to ansible...@googlegroups.com
On Wed, Oct 10, 2012 at 5:14 AM, Kahlil Hodgson
<kahlil.h...@gmail.com> wrote:
>
>
> On Friday, 28 September 2012 05:28:07 UTC+10, esclapes wrote:
>>
>>
>> Well, I "might" be the first, but got the idea from current docs:
>>
>> tasks:
>> - include: wordpress.yml user=timmy
>> - include: wordpress.yml user=alice
>> - include: wordpress.yml user=bob
>>
>> tasks:
>> include: wordpress.yml user=$item
>> with_items; $users
>
>
> Learning ansible myself, and hoped this would work too :-(

It's possible for us to make this work, with pre-task variables.
However, in doing so, it would be a bit confusing for folks to try it
with other variables that were host specific. As such, I think it's
better to list things out and let them be explicit.

It just means the definition lives in the playbook, rather than in the
variable section.

Daniel Hokka Zakrisson

unread,
Oct 10, 2012, 8:56:21 AM10/10/12
to ansible...@googlegroups.com
... and it does work in 0.8 ;-)

Daniel

Michael DeHaan

unread,
Oct 10, 2012, 9:14:12 AM10/10/12
to ansible...@googlegroups.com
Forgot about that. So much going on with 0.8 I can't remember it all :)

Daniel is the man!

--Michael
Reply all
Reply to author
Forward
0 new messages