New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
set_fact module requires type casting (updated description) #5463
Comments
Anytime you say "{{ foo }}" the system thinks you want a string when used in a string context, so set fact doesn't really know what to do with the data. You can cast at anytime by saying {{ foo | bool }} The set_fact module should probably split on the = signs PRIOR to evaluating templates, though this may prove a little "fun" with whitespace and so forth, in any event, use the above for an easy workaround. |
You can also do the cast in the template
seems to work fine. |
This is not entirely correct. It really depends on how you use the module. When you use However if you use yaml formatting, you will generally get your desired data type: - name: Setup boolean fact
set_fact:
it_is_so: "{{some.changed}}" Using the above example, |
I can't seem to get any of the suggested solutions to work. I'm noticing the same behavior when using both the YAML & the I'm running on ansible v1.4.3 Below are all the packages installed:
|
I can confirm what @vyrak is seeing, in that with 1.4 the workarounds do not do anything. The result is always a string. What about adding a param to |
Hi! Thanks very much for your interest in Ansible. It sincerely means a lot to us. On September 26, 2014, due to enormous levels of contribution to the project Ansible decided to reorganize module repos, making it easier We split modules from the main project off into two repos, http://github.com/ansible/ansible-modules-core and http://github.com/ansible/ansible-modules-extras If you would still like this ticket attended to, and believe this problem or idea is still present in the latest version of Ansible (1.7.2) or the development branch, we will need your help in having it reopened in one of the two new repos, and instructions are provided below. We apologize that we are not able to make this transition happen seamlessly, though this is a one-time change and your help is greatly appreciated -- Both sets of modules will ship with Ansible, though they'll receive slightly different ticket handling. To locate where a module lives between 'core' and 'extras'
Additionally, should you need more help with this, you can ask questions on:
Thank you very much! |
Here's an (albeit ugly) workaround for this issue: - name: init local boolean flag
set_fact:
local_bool: no
- name: set local boolean flag
set_fact:
local_bool: yes
when: var1 == 'foo' and var2 == 'bar' |
really +1 for this workaround, been wasting hours on a boolean. Seriously. |
@mpdehaan this is still an issue and I believe it's belongs to ansible core, not some specific module. - hosts: somehost
gather_facts: no
tasks:
- set_fact:
number_of_things: "{{ groups['things'] | length | int }}"
- debug: msg="{{ number_of_things | to_json }}" Results in:
But I want integer! Types are there for a reason and some API enforce them.
Care to reopen? |
This is a breaking issue on our end too. On Ansible 1.9.1. In our case, we're attempting to set a port number in YAML and converting it to nice json in a Jinja template. The target application (Sensu) needs this to be an int, but receives a string. |
Hello, Is this issue going to be addressed somewhere in the future soon ? I am facing the same issue. |
I have the same issue with linode and ansible 1.9.2. |
why did this issue get closed? still seeing this behaviour in 1.9.4 |
+1 for the workaround. -1 for the issue not being fixed. |
Unfortunately ansible is not responsive to this issue. Not only has it not been fixed, I can't even get anyone to re-open it. I have tried no less then a half dozen times on IRC to bring this to someones attention. It has always been met with silence after I send the link. |
This is still an issue for me, I have come up against this multiple times recently and have had to resort to nasty workarounds to get things working nicely. |
+1, I have this problem as well. |
+1, same boat here, its making using the aws related modules very difficult with any sort of templated or complex var... I've been messing around with just slipping this filter in certain assignments of dictionaries: def force_cast_to_int(thing): It helps, but I hate it. |
I also just used @mhenniges solution to fix our custom (templatized) yml-variable(to json string) marathon deployment, similar to the use case described in Topface/ansible-marathon_app#1.
that I put into my ansible folder under |
Improvement over @lhoss filter to works for any type (basic types, and recursive structures made of lists and dictionaries). #
def to_int(thing):
if isinstance(thing, int):
return thing
elif isinstance(thing, basestring):
try:
return int(thing)
except ValueError:
return thing
elif isinstance(thing, dict):
casted_dict = dict()
for k,v in thing.iteritems():
casted_dict[k] = force_dict_to_int(v)
return casted_dict
elif isinstance(thing, list):
return map(force_dict_to_int, thing)
class FilterModule(object):
def filters(self):
return {'to_int': force_dict_to_int} |
This is still currently an issue with the very latest Ansible and is a pretty huge deal for me. Full reproduction:
Output:
I really appreciate the workarounds provided but it doesn't quite help in a context like the one I'm facing right now: tasks:
- name: place Kontakt libraries in the requested order
plist:
dest: "com.native-instruments.{{ item.1 }}"
values:
UserListIndex: "{{ item.0 }}"
with_indexed_items: "{{ native_instruments_kontakt_library_order }}" The value of UserListIndex: '0' Any chance we can get this re-opened please? I'll be attempting to escalate this through some Red Hat contacts we have to have it re-opened, it's a serious issue. Cheers |
+1 |
+1 - reopen please! |
But, Debug shows like this : ["ls", "-l", "[u/tmp/ Could anyone help with this please ? - We are using ansible 2.3.1.0 |
+1 |
Here's the playbook with the problem:
Here's the
other.j2
template:The issue I'm having is that the
other.j2
template has a check which assumed that theit_is_so
variable is a boolean. As it turns out, that's not the case, instead, it's a string. So even ifit_is_so
was set to "False" it would still be considered true when parsing the template.I do want to note that it is treated as a boolean if I changed it up to:
I couldn't get away with
{{some.changed}}
, because it's a YAML syntax error. This works, which is nice, but the syntax is deprecated.EDIT:
Also, the
when
statement is able to treat the "False" string as afalse
boolean value.The text was updated successfully, but these errors were encountered: