要从文件夹/tmp/prod_wheel/
安装所有车轮,请执行以下步骤:
$ cat playbook_install.yml
---
- hosts: localhost
tasks:
- name: Install all wheels
pip:
name: "{{ query('fileglob', '/tmp/prod_wheel/*.whl') }}"
virtualenv: "~/venv"
virtualenv_command: /usr/bin/python3 -m venv ~/venv
运行良好。
现在我遇到了轮子在文件夹中的情况,我不知道名字,例如/tmp/data/*/*.whl
fileglob
不会显示文件夹(仅文件)。
我使用find来抓住轮子,但是在我的virtualenv中安装轮子的更紧凑的方法是什么?
$ echo playbook_catch_wheels.yml
---
- hosts: localhost
tasks:
- name: Find to catch recursively all wheels
find:
paths: /vagrant/vagrant/*/dist/
patterns: '*.whl'
您可以像以前使用find
查找一样,简单地使用map
filter从map
结果中提取路径列表,并将其传递给pip
。
理所当然的,您的实际fileglob
任务会返回您期望的结果(自从您提到它以来,我仍然添加了find
,以下两个任务应满足您的要求:
recurse
请注意,---
- hosts: localhost
tasks:
- name: Find to catch recursively all wheels
find:
paths: /vagrant/vagrant/*/dist/
patterns: '*.whl'
recurse: true
register: wheel_search
- name: Install all found wheels
pip:
name: "{{ wheel_search.files | map(attribute='path') | list }}"
virtualenv: "~/venv"
virtualenv_command: /usr/bin/python3 -m venv ~/venv
解决方案比find
查找更具可移植性,因为如果需要,它可以在远程主机上工作。查找始终在本地运行,而查找在目标主机上运行。