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

90 views
Skip to first unread message

Vineet Naik

unread,
Jul 10, 2015, 9:33:55 AM7/10/15
to ansible...@googlegroups.com
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

Mehul Ved

unread,
Jul 11, 2015, 3:45:07 PM7/11/15
to ansible...@googlegroups.com
You seem to be running into this issue
https://github.com/ansible/ansible/issues/8233
> --
> 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/be3a5ed3-cdc9-493b-90ae-5d0916b8a505%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
With Regards,
Mehul Ved

Brian Coca

unread,
Jul 11, 2015, 3:57:48 PM7/11/15
to ansible...@googlegroups.com
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

--
Brian Coca

Vineet Naik

unread,
Jul 15, 2015, 12:53:12 PM7/15/15
to ansible...@googlegroups.com
Thanks Brian, default(omit) works for me.
Reply all
Reply to author
Forward
0 new messages