# question / help needed with conditionally passing variables to a module

**URL:** <https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287>\
**Category:** Ansible Project\
**Created:** [April 27, 2016, 6:12am UTC](https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287 "2016-04-27T06:12:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrser](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nrser](https://forum.ansible.com/u/nrser)\
**Post date:** [April 27, 2016, 6:12am UTC](https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287/1 "2016-04-27T06:12:35Z")

</div>

i have a role that can optionally be provided a rbenv\_gem\_version variable that should then be optionally provided to the gem module in it’s tasks.

this seems very difficult to do with a large amount of copy-pasta due to modules differentiating between being passed null (Python None) and _not being passed anything_.

this is confusing because the [Developing Modules](http://docs.ansible.com/ansible/developing_modules.html) docs state:

> If required is false, you should document default, even if the default is ‘null’ (which is the default if no parameter is supplied). Make sure default parameter in docs matches default parameter in code.

the module is question is the gem module, and even though the optional version parameter does not have a specified default, i would infer from the above docs that it would default to null.

however, it seems like invoking

`

- gem:  
name: lunchy  
`

is different than

`

- gem:  
name: lunchy  
version: null  
`

the later resulting in a bad command:

`  
/Users/nrser/.rbenv/versions/2.1.6/bin/gem install --version None --no-user-install --no-document lunchy"

`

the pertinent parts of my role are as follows:

in [nrser.rbenv\_gem/defaults/main.yml](https://github.com/nrser/ansible-nrser.rbenv_gem/blob/v0.1.2/defaults/main.yml#L5)

`rbenv_gem_version: null`

in [nrser.rbenv\_gem/tasks/manage-version.yml](https://github.com/nrser/ansible-nrser.rbenv_gem/blob/v0.1.2/tasks/manage-version.yml#L4)

`

- name: “manage {{ rbenv\_gem\_name }} gem in rbenv rubies”  
gem:  
name: “{{ rbenv\_gem\_name }}”  
user\_install: “{{ rbenv\_gem\_user\_install }}”  
state: “{{ rbenv\_gem\_state }}”  
version: “{{ rbenv\_gem\_version }}”  
executable: “{{ ansible\_env.HOME }}/.rbenv/versions/{{ item }}/bin/gem”  
when: item != ‘system’  
with\_items: “{{ rbenv\_gem\_rubies }}”

`

is there a way of fixing this besides this?

`

- name: “manage {{ rbenv\_gem\_name }} gem in rbenv rubies”  
gem:  
name: “{{ rbenv\_gem\_name }}”  
user\_install: “{{ rbenv\_gem\_user\_install }}”  
state: “{{ rbenv\_gem\_state }}”  
executable: “{{ ansible\_env.HOME }}/.rbenv/versions/{{ item }}/bin/gem”  
when: rbenv\_gem\_version and (item != ‘system’)  
with\_items: “{{ rbenv\_gem\_rubies }}”

- name: “manage {{ rbenv\_gem\_name }} gem in rbenv rubies”  
gem:  
name: “{{ rbenv\_gem\_name }}”  
user\_install: “{{ rbenv\_gem\_user\_install }}”  
state: “{{ rbenv\_gem\_state }}”  
version: “{{ rbenv\_gem\_version }}”  
executable: “{{ ansible\_env.HOME }}/.rbenv/versions/{{ item }}/bin/gem”  
when: (not rbenv\_gem\_version) and (item != ‘system’)  
with\_items: “{{ rbenv\_gem\_rubies }}”

`

besides being an annoying amount of copy-paste to maintain, this approach seems like it will spiral out of control with combinatoric complexity as a function of the number of optional variables in the module invocation.

thanks in advance,

Neil.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 27, 2016, 9:22pm UTC](https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287/2 "2016-04-27T21:22:57Z")

</div>

That is a documentation bug, the ‘default’ for version is actually ‘’ \<= empty string.

---

<div class="post-metadata">

**Author:** ![nrser](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nrser](https://forum.ansible.com/u/nrser)\
**Post date:** [April 28, 2016, 6:36am UTC](https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287/3 "2016-04-28T06:36:47Z")

</div>

so… is the empty string the default for all module params? or are you talking specifically about the version param in the gem module?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 28, 2016, 1:41pm UTC](https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287/4 "2016-04-28T13:41:09Z")

</div>

Only version, the default can be set at each module, null/None is only if that module parameter does not specify one (in args spec, docs in this case are out of sync).

---

<div class="post-metadata">

**Author:** ![nrser](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nrser](https://forum.ansible.com/u/nrser)\
**Post date:** [April 29, 2016, 7:08am UTC](https://forum.ansible.com/t/question-help-needed-with-conditionally-passing-variables-to-a-module/21287/5 "2016-04-29T07:08:26Z")

</div>

where would i see that? i’m looking here and it doesn’t seem that any default is specified, just a type=‘str’ and required=False: [https://github.com/ansible/ansible-modules-core/blob/devel/packaging/language/gem.py#L222](https://github.com/ansible/ansible-modules-core/blob/devel/packaging/language/gem.py#L222)
