Discussion:
[ansible-project] How to convert a dict into key-val args to be passed to a module
Vineet Naik
2015-07-10 05:46:20 UTC
Permalink
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 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-project+***@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.
Mehul Ved
2015-07-11 19:44:42 UTC
Permalink
You seem to be running into this issue
https://github.com/ansible/ansible/issues/8233
Post by Vineet Naik
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
```
- 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
```
```
TASK: [Install using pip]
*****************************************************
fatal: [localhost] => A variable inserted a new parameter into the module
"{{var}}").
FATAL: all hosts have already failed -- aborting
```
None of the fields contain an equal sign.
```
- 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 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
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
--
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-project+***@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/CADTL0oa%2BL0%3DShdRMuW0w47NGNpg8R3BBSRgM0rBuBm7pBAuHdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-11 19:57:41 UTC
Permalink
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
--
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-project+***@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/CAJ5XC8mBkdpoZfWNHZ-K1ukZq0WEVaaZfM3N0Spr9Qq8YLebVg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Vineet Naik
2015-07-15 16:53:11 UTC
Permalink
Thanks Brian, default(omit) works for me.
Post by Vineet Naik
- name: Install using pip
pip: name="{{ item.name}}" chdir="{{item.chdir|default(omit)}}"
virtualenv="{{item.virtualenv|default(omit)}}"
with_items: pydeps
--
Brian Coca
--
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-project+***@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/ee3f31fe-3f16-4d21-9b11-5aa8a8a842c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...