# Use case for include tasks-file with\_items

**URL:** <https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933>\
**Category:** Ansible Project\
**Created:** [September 27, 2012, 7:19am UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933 "2012-09-27T07:19:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edu\_el\_Ligre](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Edu\_el\_Ligre](https://forum.ansible.com/u/Edu_el_Ligre)\
**Post date:** [September 27, 2012, 7:19am UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933/1 "2012-09-27T07:19:48Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 27, 2012, 11:50am UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933/2 "2012-09-27T11:50:10Z")

</div>

> 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  
&nbsp;&nbsp;some\_option\_that\_does\_not\_exist: 1234

---

<div class="post-metadata">

**Author:** ![Edu\_el\_Ligre](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Edu\_el\_Ligre](https://forum.ansible.com/u/Edu_el_Ligre)\
**Post date:** [September 27, 2012, 7:28pm UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933/3 "2012-09-27T19:28:07Z")

</div>

> 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 10, 2012, 12:17pm UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933/4 "2012-10-10T12:17:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [October 10, 2012, 12:56pm UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933/5 "2012-10-10T12:56:21Z")

</div>

Michael DeHaan wrote:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 10, 2012, 1:14pm UTC](https://forum.ansible.com/t/use-case-for-include-tasks-file-with-items/11933/6 "2012-10-10T13:14:12Z")

</div>

Forgot about that. So much going on with 0.8 I can't remember it all 🙂

Daniel is the man!

--Michael
