Still in 2.1?: Nested values always cast to string

45 views
Skip to first unread message

Barry Kaplan

unread,
Jun 28, 2016, 1:29:08 AM6/28/16
to Ansible Project
I thought one of the big improvements in 2.x was not blindly casting nested values to strings:

vars:
  a_var_number
: 200

tasks
:
 
- mymodule:
    module_args
:
      a_fixed_number
: 100
      a_var_number
: "{{ a_number }}"
      a_var_number_with_cast
: "{{ a_number | int }}"


Inside the module module_args dict gets:

    module = AnsibleModule(
        argument_spec=dict(
                   module_args=dict(required=True, type='raw')
        ),
    )

    args
= module.params['module_args']
    is_num_1
= isinstance(args['a_fixed_number'], int)         # True
    is_num_2
= isinstance(args['a_var_number'], int)           # False
    is_num_3
= isinstance(args['a_var_number_with_cast'], int) # False



Alex Leonhardt

unread,
Jun 28, 2016, 1:54:52 AM6/28/16
to ansible...@googlegroups.com
OOC, can you try single quotes? Just curious if that makes any difference...

./Alex
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ab0c1187-6552-4e02-ba43-2338d23ab889%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Barry Kaplan

unread,
Jun 28, 2016, 3:03:33 AM6/28/16
to Ansible Project
Single quotes makes no difference.

Brian Coca

unread,
Jun 28, 2016, 10:30:27 AM6/28/16
to ansible...@googlegroups.com
​The issue fixed was casting in YAML:

varname: 1

not about templating, as YAML only sees a string:

varname "{{1}}"
​^ casting to |int in the YAML definition is mostly useless, you need to cast on consumption:

option={{varname|int}}​

----------
Brian Coca

Barry Kaplan

unread,
Jun 29, 2016, 4:03:21 AM6/29/16
to Ansible Project
So if I understand Brain, that when passing dict (created from yaml) into a module or plugin, the python code must be aware of all the types of all the properties (flat or nested) and do the cast itself.

This is really fine for a module that consumes values itself. But in an example where the dict is just be converted to json and passed on to external service this will be pain. 

Is this something just has-to-be due the internal design constraints, or might the yaml types be ever be retained in the dict passed to modules?
Reply all
Reply to author
Forward
0 new messages