简体   繁体   English

ansible pip:在虚拟环境中递归安装轮子

[英]ansible pip: recursively install wheels in a virtualenv

To install all wheels from a folder /tmp/prod_wheel/ I do this:要从文件夹/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

It's working well.它运作良好。

Now I have a situation where wheels are in folders I don't known the names, eg /tmp/data/*/*.whl the fileglob does not glob folders (only files).现在我的情况是,轮子位于我不知道名称的文件夹中,例如/tmp/data/*/*.whl fileglob不 glob 文件夹(仅文件)。

I use find to catch wheels, but what is the more compact way to install them in my virtualenv?我使用 find 来捕捉轮子,但是在我的 virtualenv 中安装它们的更紧凑的方法是什么?

$ echo playbook_catch_wheels.yml
---
- hosts: localhost
  tasks:

    - name: Find to catch recursively all wheels
      find:
        paths: /vagrant/vagrant/*/dist/
        patterns: '*.whl'

You can simply extract the list of paths from your find result with the map filter and pass it to pip , as you previously did with your fileglob lookup.您可以简单地使用map过滤器find结果中提取路径列表,并将其传递给pip ,就像您之前对fileglob查找所做的那样。

Taking for granted your actual find task returns what you expect (I still added recurse since you mentioned it) the following two tasks should meet your requirement:理所当然地认为您的实际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

Note that the find solution is much more portable than the fileglob lookup, as it will work on a remote host if needed.请注意, find解决方案比fileglob查找更便携,因为如果需要,它可以在远程主机上工作。 Lookups are always running locally whereas find runs on the target host.查找始终在本地运行,而查找在目标主机上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM