module development and 'with_items'

89 views
Skip to first unread message

Dmitry Makovey

unread,
Jan 6, 2014, 7:05:55 PM1/6/14
to ansible...@googlegroups.com
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 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?

Dmitry Makovey

unread,
Jan 6, 2014, 7:18:56 PM1/6/14
to ansible...@googlegroups.com
BTW - if there's a better alternative to 'with_items' - I'm all for it, just didn't see anything close to it

Matt Martz

unread,
Jan 6, 2014, 7:20:34 PM1/6/14
to Dmitry Makovey, ansible...@googlegroups.com
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:


So, in short, it requires modifications to that __init__.py file to make it work.
-- 
Matt Martz
ma...@sivel.net
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dmitry Makovey

unread,
Jan 6, 2014, 7:32:41 PM1/6/14
to ansible...@googlegroups.com, Dmitry Makovey
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?

Dmitry Makovey

unread,
Jan 6, 2014, 7:52:59 PM1/6/14
to ansible...@googlegroups.com, Dmitry Makovey
I think I've got it:


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

Michael DeHaan

unread,
Jan 6, 2014, 8:02:45 PM1/6/14
to ansible...@googlegroups.com, Dmitry Makovey
"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




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Dmitry Makovey

unread,
Jan 7, 2014, 12:48:25 AM1/7/14
to ansible...@googlegroups.com, Dmitry Makovey


On Monday, January 6, 2014 6:02:45 PM UTC-7, Michael DeHaan wrote:
This is really a topic for ansible-devel, BTW

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

Brian Coca

unread,
Jan 7, 2014, 10:02:08 AM1/7/14
to ansible...@googlegroups.com
i'm always up for revisiting what i did wrong in my 'wantarray' patch 

Tim Chambers

unread,
Sep 30, 2015, 9:45:02 AM9/30/15
to Ansible Project
I ran into this problem today. I found with_items but with single module call 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 still refer to a constant when deciding whether to squash loops into a single call.

Tim Chambers

unread,
Sep 30, 2015, 3:57:21 PM9/30/15
to Ansible Project, droop...@gmail.com
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.

Brian Coca

unread,
Sep 30, 2015, 4:03:26 PM9/30/15
to Ansible Project, droop...@gmail.com
this is my idea dump on how to make with_ better/more configurable:

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.

--
Brian Coca

Tim Chambers

unread,
Oct 2, 2015, 3:16:32 PM10/2/15
to Ansible Project, droop...@gmail.com
FWIW, that code uses type(name) == type(''): etc., where isinstance(name,str): (same for list) would be much more Pythonic.

On Monday, January 6, 2014 at 5:52:59 PM UTC-7, Dmitry Makovey wrote:
I think I've got it:


...
 
Reply all
Reply to author
Forward
0 new messages