# module development and 'with\_items'

**URL:** <https://forum.ansible.com/t/module-development-and-with-items/14284>\
**Category:** Ansible Project\
**Created:** [January 7, 2014, 12:05am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284 "2014-01-07T00:05:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [January 7, 2014, 12:05am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/1 "2014-01-07T00:05:55Z")

</div>

I’m playing with my module and would like to cram most operations into one transaction under ‘with\_items’ according to the docs ‘yum’ and ‘apt’ modules can combine multiple operations generated by ‘with\_items’ into one. However I’m a bit puzzled as to what is causing it to behave that way.

[http://pastebin.com/hU2csR6b](http://pastebin.com/hU2csR6b) is the code. and here’s the playbook:

- name: Testing Foo  
foo: name={{ item }} state=present  
with\_items:
- 1
- 2
- 3

I expect /tmp/foo to contain 1,2,3 (it seems like that is how yum is receiving it’s list of packages) but I end up with ‘3’ every time (result of file being overwritten 2 times). I’m definitely missing something, but what?

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [January 7, 2014, 12:18am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/2 "2014-01-07T00:18:56Z")

</div>

BTW - if there’s a better alternative to ‘with\_items’ - I’m all for it, just didn’t see anything close to it

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 7, 2014, 12:20am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/3 "2014-01-07T00:20:34Z")

</div>

There are some other internals inside of ansible/runner/ **init**.py that do the behind the scenes magic required for the with\_items stuff that is done for yum and apt:

[https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/ **init**.py#L497](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/ __init__.py#L497)

So, in short, it requires modifications to that **init**.py file to make it work.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [January 7, 2014, 12:32am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/4 "2014-01-07T00:32:41Z")

</div>

thanks for the pointer. Now _that_ makes sense why I couldn’t find anything in the modules themselves.

It also means that unless I hack that file I’m not getting my module to behave similarly.

Is there another way to join operations into a single transaction short of passing their parameters in some comma-separated special-crafted string?

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [January 7, 2014, 12:52am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/5 "2014-01-07T00:52:59Z")

</div>

I think I’ve got it:

[http://pastebin.com/NCgAhVjF](http://pastebin.com/NCgAhVjF)

now works with:

- name: Testing Foo dicts  
foo:  
name:  
one: 1  
two: 2  
three: 3  
state: present

- name: Testing Foo lists  
foo:  
name: [1, 2, 3]  
state: present

- name: Testing Foo lists  
foo:  
name:

- 1

- 2

- 3  
state: present

last two result in a simple list iteration, while the first one passes dictionary this should help with my module building… unless somebody is going to point out a better way/inapropriateness of my approach 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 7, 2014, 1:02am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/6 "2014-01-07T01:02:45Z")

</div>

"It also means that unless I hack that file I’m not getting my module to behave similarly. "

Basically we’d have to find some way of putting the metadata in the module versus having it in Runner.

This is really a topic for ansible-devel, BTW

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [January 7, 2014, 5:48am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/7 "2014-01-07T05:48:25Z")

</div>

should I be filing a github issue or [re]post to ansible-devel then?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 7, 2014, 3:02pm UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/8 "2014-01-07T15:02:08Z")

</div>

i’m always up for revisiting what i did wrong in my ‘wantarray’ patch

---

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [September 30, 2015, 12:13am UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/9 "2015-09-30T00:13:24Z")

</div>

I ran into this problem today. I found [with\_items but with single module call](https://groups.google.com/d/msg/ansible-devel/PUejFmA1gfI/4RXTUHsgBAAJ) discussed on ansible-devel. I know this thread is old, but has anything changed? I see no discussion of Brian Coca’s wantarray patch on ansible-devel. I see TaskExecutor replaced Runner and that [lines 53-55](https://github.com/ansible/ansible/blob/devel/lib/ansible/executor/task_executor.py#L53-L55) still refer to a constant when deciding whether to squash loops into a single call.

---

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [September 30, 2015, 7:57pm UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/10 "2015-09-30T19:57:21Z")

</div>

If this was StackExchange I’d upvote. Instead, I’ll leave this note of endorsement. This example is exactly what I was looking for! It isn’t specific to packaging at all. It’s a general purpose solution to passing arguments into modules. Too bad apt, yum, and pkgng have already been hacked. But let the madness not extend further! Module interfaces shouldn’t rely on with\_items. Instead, I’ll be using complex arguments.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 30, 2015, 8:03pm UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/11 "2015-09-30T20:03:19Z")

</div>

this is my idea dump on how to make with\_ better/more configurable:

[https://github.com/ansible/ansible/issues/12086](https://github.com/ansible/ansible/issues/12086)

it includes a bit more than the 'squash' feature, for now we just  
made it configurable so users can enable/disable it by module, going  
forward i would like to make it per task as in the example in the  
ticket.

---

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [October 2, 2015, 7:16pm UTC](https://forum.ansible.com/t/module-development-and-with-items/14284/12 "2015-10-02T19:16:32Z")

</div>

FWIW, that code uses type(name) == type(‘’): etc., where isinstance(name,str): (same for list) would be much more Pythonic.
