Skip to content
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 integer assignment #4170

Closed
gwillem opened this issue Sep 19, 2013 · 6 comments
Closed

set_fact integer assignment #4170

gwillem opened this issue Sep 19, 2013 · 6 comments
Labels
bug This issue/PR relates to a bug.

Comments

@gwillem
Copy link

gwillem commented Sep 19, 2013

The following yields an unicode string:

set_fact: ansible_memtotal_mb={{ansible_local.lxc.memory.memtotal_mb|int}}

Perhaps utils.parse_kv() should be modified to auto cast integers, or it should honor the |int filter.

@mpdehaan
Copy link
Contributor

templating in Ansible means "produce this as a string"

If you need an integer value later in a template you can use " {{ x | int }}" to cast it.

This is because the option line is a string, and that's totally fine.

Auto-casting seems unwise.

@mrdrmmn
Copy link

mrdrmmn commented Jan 23, 2016

I just spent quite a while debuging a problem that turned out to be because of this exact issue.

I get that the option line is a string, and "guessing" at what type the value should be is a bad idea, but why not at a 'cast' or 'type' parameter so that the user can specify what type of variable it should be cast to? This seems like the best of all worlds.

It is worth noting that the current functionality already is inconsistent with the "auto-caster seems unwise" statement. The documentation specifically states:

As of 1.8, Ansible will convert boolean strings ('true', 'false', 'yes', 'no')
to proper boolean values when using the key=value syntax, however it is still
recommended that booleans be set using the complex argument style

Interestingly, this 1.8 came out 14 months after this issue was closed. Perhaps this should be revisited?

@smalleni
Copy link

+1 to revisiting this issue.

@antoineco
Copy link
Contributor

antoineco commented Jun 14, 2016

I also believe this issue should be revisited, as it prevents dynamically generated dictionaries from being unmarshalled as JSON as expected.

One example:

- name: Set cluster bootstrap size (int)
  set_fact:
    bootstrap_size: '{{ groups["cluster_servers"] | length | int }}'

- name: Enable server mode (bool)
  set_fact:
    enable_server: '{{ "True" | bool }}'

- name: Collect servers IP (list)
  set_fact:
    servers_ip: '{{ (servers_ip | default([])) + [ hostvars[item]["ansible_default_ipv4"]["address"] ] }}'
  with_items: '{{ groups["cluster_servers"] }}'

- name: Generate opts dictionary
  set_fact:
    opts_dictionary: '{{ opts_dictionary | default({}) |
                           combine( { item: hostvars[inventory_hostname][item] } )
                      }}'
  with_items:
    - 'bootstrap_size'
    - 'enable_server'
    - 'servers_ip'

- name: Debug fact
  debug:
    var: opts_dictionary

Debug output:

TASK [Debug fact] *****************************************************
ok: [cluster_server_1] => {
  "opts_dictionary": {
     "bootstrap_size": "3",   <--- not casted
     "enable_server": true,  <--- casted
     "servers_ip": [          <--- casted
         "10.0.0.1",
         "10.0.0.2",
         "10.0.0.3"
    ]
  }
}

If booleans, lists and dicts are converted I don't see why integers shouldn't, at least there should be a way to do this explicitly.

@antoineco
Copy link
Contributor

The issue I described above is actually related to #15249, not to the set_facts module which actually casts integers. Please excuse the noise.

@arodier
Copy link

arodier commented Apr 2, 2018

Same error here. I add my version of ansible:

ansible 2.4.3.0
  config file = /home/andre/.ansible.cfg
  configured module search path = [u'/home/andre/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]

@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

7 participants