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

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

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.
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,

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

However, this fails with the following error:

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:

- 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

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

what you want is:

    - name: Install using pip
      pip: name="{{ item.name}}" chdir="{{item.chdir|default(omit)}}"
virtualenv="{{item.virtualenv|default(omit)}}"
      with_items: pydeps

Thanks Brian, default(omit) works for me.