Reputation: 510
I'm trying to append a string to each value of a list in ansible, so basically am trying to install multiple pip modules offline using .whl files.
I have two files in /opt/tmp/ path
vars:
DIR: /opt/
pymongo_modules:
- pip-19.1.1-py2.py3-none-any.whl
- pymongo-3.8.0-cp27-cp27mu-manylinux1_x86_64.whl
- name: Install the latest pymongo package
pip:
name: "{{DIR}}/tmp/{{ pymongo_modules | join(' ') }}"
executable: "{{pip_path}}"
The above is not working because it's formating like below
"name": ["/opt/tmp/pip-19.1.1-py2.py3-none-any.whl pymongo-3.8.0-cp27-cp27mu-manylinux1_x86_64.whl"]
I can achieve the same with below syntax but I'm getting deprecation warning
- name: Install the latest pymongo package
pip:
name: "{{DIR}}/tmp/{{ module }}"
executable: "{{pip_path}}"
with_items:
- "{{ pymongo_modules }}"
loop_control:
loop_var: module
Expecting value:
"name": ["/opt/tmp/pip-19.1.1-py2.py3-none-any.whl", "/opt/tmp/pymongo-3.8.0-cp27-cp27mu-manylinux1_x86_64.whl"]
Upvotes: 0
Views: 613
Reputation: 4211
I've just answered a similar question over here. In summary, you can achieve this with regex_replace
and map
. It's quite a tidy method and is also in the official Ansible docs, so it seems to be recommended too.
Upvotes: 0
Reputation: 3037
Use product
filter like below. BTW, DIR
variable already ends with a /
so do not need an additional /
before tmp
.
- debug:
msg: "{{ item }}"
loop: "{{ [(DIR + 'tmp')] | product(pymongo_modules) | map('join', '/') | list }}"
Gives:
ok: [localhost] => (item=/opt/tmp/pip-19.1.1-py2.py3-none-any.whl) => {
"msg": "/opt/tmp/pip-19.1.1-py2.py3-none-any.whl"
}
ok: [localhost] => (item=/opt/tmp/pymongo-3.8.0-cp27-cp27mu-manylinux1_x86_64.whl) => {
"msg": "/opt/tmp/pymongo-3.8.0-cp27-cp27mu-manylinux1_x86_64.whl"
}
Upvotes: 1