is it possible to maintain order within with_ loops?

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

have you tried |sort?

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

Untested, but maybe this:

with_12fcmds: deploy_env|sort

or if its a dictionary:

with_12fcmds: deploy_env|dictsort

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

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