Question

Given that Ansible processes all variables through Jinja2, and doing something like this is possible:

- name: Debug sequence item value
  debug: msg={{ 'Item\:\ %s'|format(item) }}
  with_sequence: count=5 format="%02d"

Which correctly interpolates the string as:

ok: [server.name] => (item=01) => {"item": "01", "msg": "Item: 01"}
ok: [server.name] => (item=02) => {"item": "02", "msg": "Item: 02"}
ok: [server.name] => (item=03) => {"item": "03", "msg": "Item: 03"}
ok: [server.name] => (item=04) => {"item": "04", "msg": "Item: 04"}
ok: [server.name] => (item=05) => {"item": "05", "msg": "Item: 05"}

Why then doesn't this work:

- name: Debug sequence item value
  debug: msg={{ 'Item\:\ %02d'|format(int(item)) }}
  with_sequence: count=5

This apparently causes some sort of parsing issue which results in our desired string being rendered verbose:

ok: [server.name] => (item=01) => {"item": "01", "msg": "{{Item\\:\\ %02d|format(int(item))}}"}

Noting that in the above example item is a string because the default format of with_sequence is %d, and format() doesn't cast the value of item to the format required by the string interpolation %02d, hence the need to cast with int().

Is this a bug or am I missing something?

Était-ce utile?

La solution

It took me a couple of tries to get this right, but try this, instead:

debug: msg={{ 'Item\:\ %02d'|format(item|int) }}

Jinja2 is a bit funny.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top