Earlier today I brought up on IRC a question about with_items taking a var list (rather than literal). It doesn’t sound like this is a desirable feature, so I’m turning to the list to ask for suggestions.
I need to pass in (via extra_vars most likely) some transient data, which is actually a list. For each item in that list, I need to perform a certain task. Any suggestions how to do that without having to invoke ansible-playbook once for each item in that list (which, in my case, would work).
Yeah, it’s desirable, the question is how to do it cleanly without introducing weird syntax. I think the question in IRC I originally bristled to was suggesting
{{ list_var }}
Maybe not. Anyway, that’s a template thing, and that’s kind of tricky.
However, is it really a template thing?
with_items: $list_var
In this case, we would NOT want to do utils.template, but would need to note that this was a variable, and in which case use (but not template) the variable.
So we could easily enough check if something was available in vars.
The question though, is how you do it, so people don’t infer it works in a way different than it actually works, and expect something more complicated to also work, when it might not
Would it be bad to support $items but not {{ items }}?
I’m fine with:
with_items: $items
which is enough different from:
with_items:
As to easily know not to template it… as:
with_items: foo
…would be illegal anyway, so templating the single value of with_items would not make sense. But I do see your point… so I’ll think some more about alternate syntax.
Would it be bad to support $items but not {{ items }}?
I think it’s much easier to only do $items, yes. I have a slight concern it becomes murky what’s templating and what is not, BUT, as long as we stress that play books are not Jinja2 templates, that is mostly ok.
In this case, we’d throw away the “$” and look for a variable, and explode otherwise.
It’s somewhat a lie, but not a terrible one.
Template evaluation in playbooks is solely there to replace variables, so if this particular variable replaces a variable in a completely different way, I think that’s acceptable, as long nobody has to know
I’m fine with:
with_items: $items
which is enough different from:
with_items:
As to easily know not to template it… as:
with_items: foo
right, this would be too foreign from other things to be valid syntax, IMHO.
OK, I’ll tackle this now. Shouldn’t be hard. $items only though.
Of course, there’s another issue… which is that --extra-vars (the most likely place to get this list from) would more likely take a comma delimited list, rather than yaml.
Should it first try to load $items as yaml, and then try items.split(“,”)?
So all the vars, if you do it in the right place, should be accessible in the data structure vars.
In which case, if the value of with_items is a string, and it starts with "$", throw away the "$" and return
WARNING: rapidly written pseudocode resembling python
if type(with_items_whatever) == string:
# TODO: throw away $ and get the varname
items = vars_or_whatever_the_data_structure_is.get(varname, None)
if items is None:
raise AnsibleError("…")
if type(items) != list:
raise AnsibleError("…")
else if type(with_items_whatever) == list:
# what it does now
--Michael
OK, I was wanting to pass in some extra-vars (comma delimited), but this will work fine, as I can get my inventory script to massage the data into a list.