passing non-string variable to module

96 views
Skip to first unread message

Ayelet Goldin

unread,
Aug 24, 2014, 1:24:10 AM8/24/14
to ansible...@googlegroups.com
I'm trying to figure out how to pass a non-string variable to a module


Has anyone figured it out?

turns inty_var and floaty_var within parametered module into strings:

  - name: thingy with vars
    parametered_module.py:
      thingy:
        stringy_var: "{{ item['stringy'] }}"
        inty_var: "{{ item['inty'] }}"
        floaty_var: "{{ item['floaty'] }}"
    with_items:
    - {'stringy': 'asdf', 'inty': 3, 'floaty': 123.21}

Thanks for your help!

Michael DeHaan

unread,
Aug 24, 2014, 10:32:44 AM8/24/14
to ansible...@googlegroups.com
Quick note - modules shouldn't end in ".py", either when saved or called.

But what you have is fine, you can also do.

  - name: thingy with vars
    parametered_module.py:
      thingy:
        stringy_var: "{{ item.stringy }}"
        inty_var: "{{ item.inty }}"
        floaty_var: "{{ item.floaty }}"
    with_items:
    - {'stringy': 'asdf', 'inty': 3, 'floaty': 123.21}

You may also wish to do:

- name: thingy with vars
  parameterized_module:
     thingy: "{{ item }}"
  with_items:
     - ...

And just name your hash variables stringy_var vs stringy, etc.

You can also pass variables into with_items, and define your hash elsewhere like:

with_items: my_items





--
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/913ead1a-e6e7-4bde-95b4-c9279ac7abfa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ayelet Goldin

unread,
Aug 24, 2014, 2:40:12 PM8/24/14
to ansible...@googlegroups.com
My problem is that when I look at thing['inty_var'] in the module it's a string. that is : 
- name: hard coded thingy
parametered_module.py:
thingy:
stringy_var: 'asdf'
inty_var: 3
floaty_var: 123.21
Is correctly interpreted by parametered_module as an int. But when I do this: 
- name: thingy with vars
parametered_module.py:
thingy:
stringy_var: "{{ item['stringy'] }}"
inty_var: "{{ item['inty'] }}"
floaty_var: "{{ item['floaty'] }}"
with_items:
- {'stringy': 'asdf', 'inty': 3, 'floaty': 123.21}

It sees thingy['inty_var'] as '3' and the module assumes it's a string. Is there a way to force inty_var to an int within ansible? 
I also tried: inty_var: "{{ item['inty'] | int }}" but that is still a string by the time it gets to parametered_module

Thanks for your help!

Michael DeHaan

unread,
Aug 25, 2014, 10:11:10 AM8/25/14
to ansible...@googlegroups.com
At least in any current versions of Ansible, if you are passing structured args, like so:

module_name:
    foo: "{{ x }}"

It will send JSON and keep types.

Otherwise, you can cast automatically in the module by using the variable "type=" options in the argument_spec of the module, for instance, to declare something should always be treated as an int.

Which you should be doing anyway, as that makes the playbooks cleaner by not having to cast anything in the playbook.





Ayelet Goldin

unread,
Aug 25, 2014, 8:47:08 PM8/25/14
to ansible...@googlegroups.com
I can't do this "correctly" inside the module since it's meant to be a flexible variable. 
My problem with 
module_name:
    foo: "{{ x }}"
is that I would like to use variables within foo
so I have
module_name:
    foo: 
        somevar: "{{ myvar }}"

and this definitely turns myvar into a string. 

I guess I can do some parsing of the value within my module and let you do things like 

module_name:
    foo: 
        somevar: "int: {{ myvar }}"

but I was hoping there might be a cleaner way. 

Michael DeHaan

unread,
Aug 26, 2014, 8:33:41 AM8/26/14
to ansible...@googlegroups.com
If you file a github ticket, we can do some digging about variable type preservation.

Though I still recommend in the argument_spec a "type="int"" or casting in your particular module, it makes everything easier on the caller.




Reply all
Reply to author
Forward
0 new messages