Using a "-e force=yes" flag

Hi,

First of all, a very happy new year to all of you.

I am trying to find a way to make it possible to add “-e force=yes” to an ansible-playbook command line to force certain things, like unpacking an archive, even if it has not changed. I want to add “when: force” to such tasks.

My set-up is role-based, so the first thing a tried is adding force: false in defaults/main.yml for every role that uses the variable. Unfortunately, if I use “-e force=yes” the variable is not magically interpreted as a boolean. So I could use “when: force|bool”, but I tried to avoid that by setting "forced: “{{ force | default(False) | bool }}” and using the “forced” variable instead of “force”. But it seems that variables set in vars/main.yml are always converted to strings. See the files at https://gist.github.com/jcassee/8217906 and a full Ansible environment at https://letscrate.com/f/jcassee/github/ansible-issue.tar.gz

Two question:

  1. Is the conversion to strings a bug or expected behaviour?
  2. What is the simplest way to tie “-e force=yes” to “when: force”?

Regards,
Joost

It’s quite true that we don’t know “yes” means a string when specified as a string to -e, and it would seem wrong to automatically boolean-ify it. However, you can pass it in as JSON if you like, as -e does take JSON.

“But it seems that variables set in vars/main.yml are always converted to strings”

This is not the case.

If you do things like x: “{{y}}”

Then yes, it doesn’t know y is a bool unless you cast it.

This may provide a reasonable answer.

It's quite true that we don't know "yes" means a string when specified as a
string to -e, and it would seem wrong to automatically boolean-ify it.
However, you can pass it in as JSON if you like, as -e does take JSON.

Good idea, thanks.

"But it seems that variables set in vars/main.yml are always converted to
strings"

This is not the case.

If you do things like x: "{{y}}"

Then yes, it doesn't know y is a bool unless you cast it.

With casting, you mean using the bool filter when using the variable
("{{ x | bool }}")? Or would you expect this to work in vars/main.yml:

x: "{{ y | bool} }"

Regards,
Joost