is it possible to maintain order within with_ loops?

1,107 views
Skip to first unread message

Guy Matz

unread,
Oct 3, 2014, 10:28:39 AM10/3/14
to ansible...@googlegroups.com
Hi! I have a custom iterator that needs to execute in the order specified in the data structure being passed to it.  It seems, however, that the order is getting lost when it enters the looping code and gets converted to a dict.

Does anyone know if there's a way to maintain the order of the elements of a loop?  I've looked at the lookup_plugins for some guidance but have not found any inspiration

Thanks a lot,
Guy

Brian Coca

unread,
Oct 3, 2014, 11:57:29 AM10/3/14
to ansible...@googlegroups.com
have you tried |sort?

--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Guy Matz

unread,
Oct 3, 2014, 1:39:25 PM10/3/14
to ansible...@googlegroups.com
not sure what you mean . . .  my data comes from json that looks like this:
deploy_env = 
{
    "cmd_build": [
        "gem install rake bundler",
        "bundle install --deployment --without development test --path $BUNDLE_DIR"
    ],
    "cmd_release": [
        "bundle exec rake deploy:release",
        "bundle exec rake db:migrate"
    ]
}

and I loop through the cmd_* hashes with my custom iterator like:
- shell: {{ base_dir }}{{ item.key }}
  with_12fcmds: deploy_env

I need the cmd_build to come before cmd_release as defined in the json, bu it seems that somewhere along the line the json is getting turned into a dict and losing its order . . .

have you tried |sort?
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CADn%2BHswakpYOn%2BepvVKsTLYPCxzTpJzCH5bG_7phOYttNQrwyQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Oct 3, 2014, 1:44:47 PM10/3/14
to ansible...@googlegroups.com
On Fri, Oct 3, 2014 at 1:39 PM, Guy Matz <gm...@matz.org> wrote:
> with_12fcmds: deploy_env


with_12fcmds: deploy_env|sort

or if its a dictionary:

with_12fcmds: deploy_env|dictsort

Adam Heath

unread,
Oct 3, 2014, 1:44:54 PM10/3/14
to ansible...@googlegroups.com
Untested, but maybe this:

==
- shell: {{ base_dir }} {{ item }}
with_items: deploy_env.keys() | sort
==

And then be sure to name your keys so they sort the way you need.
> <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To post to this group, send email to
> ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CABnTgtU-zS6Hr9_WE6w09k%3DtpgwPme1k6MwgaAWe4t2J7CugEQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CABnTgtU-zS6Hr9_WE6w09k%3DtpgwPme1k6MwgaAWe4t2J7CugEQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Guy Matz

unread,
Oct 3, 2014, 1:52:22 PM10/3/14
to ansible...@googlegroups.com
Yeah, I can't use sort because I don't want them sorted, I just want them in the order they came in in the original data.  thanks, though!!


    To post to this group, send email to

    To view this discussion on the web visit
    https://groups.google.com/d/msgid/ansible-project/CADn%2BHswakpYOn%2BepvVKsTLYPCxzTpJzCH5bG_7phOYttNQrwyQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.


--
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-project+unsubscribe@googlegroups.com <mailto:ansible-project+unsub...@googlegroups.com>.
To post to this group, send email to ansible-project@googlegroups.com <mailto:ansible-project@googlegroups.com>.
--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/542EE087.70904%40brainfood.com.

Michael DeHaan

unread,
Oct 3, 2014, 5:40:05 PM10/3/14
to ansible...@googlegroups.com
So with_items does loop over *lists* in order.

It looks like you have a hash/dictionary above - these do *not* preserve order (because they seldom do, it is their nature...)



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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CABnTgtXXsdqvVFaUu4ZfOQn-Rmp6M2%2BhMoAzy5bmsoc%2Bnnq_wA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages