Various variable improvements made recently

Hi everyone,

I just pushed a lot of commits I made on the plane last night that further upgrade the usage of the “new” 1.2 variable/conditional syntax, as detailed here:

https://github.com/ansible/ansible/blob/devel/examples/playbooks/upgraded_vars.yml

Notice that with_items can now be fed the name of a variable, or even a Python expression, and it just works, no need for $foo or {{ }} at all. There are also various improvements to corner cases, so I’d consider this ready for full usage now.

There’s also a lot of other assorted cleanup.

My plan is to make the documentation for 1.2 all use the {{ new.way }}, and then determine if there is a future date where we may want to deprecate the $old.{way}

I think streamlining the docs will end a lot of user confusion.

Currently it internally flags deprecation warnings but doesn’t print them, I suspect we may choose to do the same with “when_” and only_if as well.

I also fixed some issues with parameterized playbook includes.

Hi,

I find myself doing a constant mistake when using the new 1.2 variables, namely:

something: {{ var1 }}

That is treated as JSON inside YAML syntax instead of something starting with a variable.

Although this breaks the YAML syntax, there could be a check if it starts with “{{” and contains somewhere “}}”, eg. with regex: ‘^{{[^{]+}}’.

Greetings,
gw

This is why in the case of

with_items: foo

you don’t have to surround it in {{ }} anymore.

What was the case in which you personally encountered it?

Hi,

Now that with_ and when_ work this are the only (maybe not the best) use cases:

---- hosts: all
vars:

  • cmd: /bin/echo
  • var: {{ cmd }}
    tasks:
  • command: {{ cmd }} foo bar
  • shell: {{ item }}
    with_items:
  • echo first

A solution without explicitly putting everything that is not needed into strings:

---- hosts: all
vars:

  • cmd: /bin/echo
  • var: ‘{{ cmd }}’
    tasks:
  • action: command {{ cmd }} foo bar
  • action: shell {{ item }}
    with_items:
  • echo first

Greetings,
gw

Yep, I had written a preprocessor for this yesterday but I think that’s a dangerous trap as someone will want to read the YAML later when it’s not valid.

As such, $cmd still work in this case, I don’t think I’m going to remove the basic variable referencing like this.

My other thought was to change the Jinja2 delimiter for playbooks so it’s different than what is used in templates, though that also has downsides.