# How to convert a dict into key-val args to be passed to a module

**URL:** <https://forum.ansible.com/t/how-to-convert-a-dict-into-key-val-args-to-be-passed-to-a-module/18725>\
**Category:** Ansible Project\
**Created:** [July 10, 2015, 5:46am UTC](https://forum.ansible.com/t/how-to-convert-a-dict-into-key-val-args-to-be-passed-to-a-module/18725 "2015-07-10T05:46:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vineet\_Naik](https://avatars.discourse-cdn.com/v4/letter/v/96bed5/32.png) [@Vineet\_Naik](https://forum.ansible.com/u/Vineet_Naik)\
**Post date:** [July 10, 2015, 5:46am UTC](https://forum.ansible.com/t/how-to-convert-a-dict-into-key-val-args-to-be-passed-to-a-module/18725/1 "2015-07-10T05:46:20Z")

</div>

Hi,

I am using the pip module to install python dependencies. What I am trying to achieve is -

To be able to define the dependencies in one of the vars\_files as follows

```auto
pydeps:
- name: requests
virtualenv: /tmp/testvenv
- name: boto
editable: yes
- requirements: /tmp/requirements.txt
virtualenv: /tmp/anothertestenv

```

Basically, the fields in the above list of dicts can be any valid options accepted by the pip module - [http://docs.ansible.com/pip\_module.html](http://docs.ansible.com/pip_module.html).  
Those options that are not required can be omitted just like they can be omitted when specifying the args to the module.

And then in tasks,

```auto
- name: Install using pip
pip: "{% for k, v in item.items() %}{{ k }}={{ v }} {% endfor %}"
with_items: pydeps

```

However, this fails with the following error:

```auto
TASK: [Install using pip] *****************************************************
fatal: [localhost] => A variable inserted a new parameter into the module args. Be sure to quote variables if they contain equal signs (for example: "{{var}}").

FATAL: all hosts have already failed -- aborting

```

None of the fields contain an equal sign.

I have also tried the following:

```auto
- name: Install using pip
pip: >
name={{ item.name }}
{{ 'chdir='~item.chdir if item.get('chdir') else '' }}
{{ 'virtualenv='~item.virtualenv if item.get('virtualenv') else '' }}
# ... and so on
with_items: pydeps

```

But it also fails with the same error if any of the key is missing for an item.

What’s the correct way to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![Mehul\_Ved](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mehul\_Ved](https://forum.ansible.com/u/Mehul_Ved)\
**Post date:** [July 11, 2015, 7:44pm UTC](https://forum.ansible.com/t/how-to-convert-a-dict-into-key-val-args-to-be-passed-to-a-module/18725/2 "2015-07-11T19:44:42Z")

</div>

You seem to be running into this issue  
[https://github.com/ansible/ansible/issues/8233](https://github.com/ansible/ansible/issues/8233)

---

<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:** [July 11, 2015, 7:57pm UTC](https://forum.ansible.com/t/how-to-convert-a-dict-into-key-val-args-to-be-passed-to-a-module/18725/3 "2015-07-11T19:57:41Z")

</div>

what you want is:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Install using pip  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pip: name="{{ item.name}}" chdir="{{item.chdir|default(omit)}}"  
virtualenv="{{item.virtualenv|default(omit)}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: pydeps

---

<div class="post-metadata">

**Author:** ![Vineet\_Naik](https://avatars.discourse-cdn.com/v4/letter/v/96bed5/32.png) [@Vineet\_Naik](https://forum.ansible.com/u/Vineet_Naik)\
**Post date:** [July 15, 2015, 4:53pm UTC](https://forum.ansible.com/t/how-to-convert-a-dict-into-key-val-args-to-be-passed-to-a-module/18725/4 "2015-07-15T16:53:11Z")

</div>

Thanks Brian, default(omit) works for me.
