# passing non-string variable to module

**URL:** <https://forum.ansible.com/t/passing-non-string-variable-to-module/16023>\
**Category:** Ansible Project\
**Created:** [August 24, 2014, 5:24am UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023 "2014-08-24T05:24:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayelet\_Goldin](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Ayelet\_Goldin](https://forum.ansible.com/u/Ayelet_Goldin)\
**Post date:** [August 24, 2014, 5:24am UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023/1 "2014-08-24T05:24:10Z")

</div>

I’m trying to figure out how to pass a non-string variable to a module

[https://gist.github.com/uncreative/adfe926267d0ee5b3de3](https://gist.github.com/uncreative/adfe926267d0ee5b3de3)

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!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 24, 2014, 2:32pm UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023/2 "2014-08-24T14:32:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ayelet\_Goldin](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Ayelet\_Goldin](https://forum.ansible.com/u/Ayelet_Goldin)\
**Post date:** [August 24, 2014, 6:40pm UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023/3 "2014-08-24T18:40:12Z")

</div>

My problem is that when I look at thing[‘inty\_var’] in the module it’s a string. that is :

```auto

  - 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:

```auto

  - 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!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 25, 2014, 2:11pm UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023/4 "2014-08-25T14:11:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ayelet\_Goldin](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Ayelet\_Goldin](https://forum.ansible.com/u/Ayelet_Goldin)\
**Post date:** [August 26, 2014, 12:47am UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023/5 "2014-08-26T00:47:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 26, 2014, 12:33pm UTC](https://forum.ansible.com/t/passing-non-string-variable-to-module/16023/6 "2014-08-26T12:33:37Z")

</div>

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.
