Ansible:在一个会话中安装多个Python包

Ada*_*tan 7 pip ansible ansible-playbook

我的一本playbooks包含一个安装基本Python包的任务:

---
  -
    name: "Install Python packages: {{ python_packages_to_install }}"
    sudo: true
    pip: name={{ item }}
    with_items: python_packages_to_install
Run Code Online (Sandbox Code Playgroud)

使用以下包列表:

-
  include: python_basics.yaml
  vars:
     python_packages_to_install:
       - virtualenv
       - pss
       - requests
       - comment-builder
       - boto
       - ansible
       - uwsgitop
       - gitpull
       - ipython
Run Code Online (Sandbox Code Playgroud)

该任务正常工作并安装包:

TASK: [common | Install Python packages: ['virtualenv', 'pss', 'requests', 'comment-builder', 'boto', 'ansible', 'uwsgitop', 'gitpull', 'ipython']] ***
ok: [push-prod-01] => (item=virtualenv)
ok: [push-prod-01] => (item=pss)
ok: [push-prod-01] => (item=requests)
ok: [push-prod-01] => (item=comment-builder)
ok: [push-prod-01] => (item=boto)
ok: [push-prod-01] => (item=ansible)
ok: [push-prod-01] => (item=uwsgitop)
ok: [push-prod-01] => (item=gitpull)
changed: [push-prod-01] => (item=ipython)
Run Code Online (Sandbox Code Playgroud)

问题是每一行都是使用连续的SSH命令执行的,而不是在一次调用中安装所有的包.

有没有办法在Ansible pip命令上安装多个Python包?

nit*_*one 10

扩展Ben的答案,您还可以继续将包列表保存为yaml列表,并将其投影到单个值,当您将其传递给pip模块时,如:

pip: name="{{ python_packages_to_install | join(' ') }}"

让你的剧本更易于维护......


Cyr*_*ril 10

Ansible pip 模块现在支持多 python 包安装。

- name: Install multi python packages
  pip:
    name:
      - virtualenv
      - pss
      - requests
      - comment-builder
      - boto
      - ansible
      - uwsgitop
      - gitpull
      - ipython
Run Code Online (Sandbox Code Playgroud)

使用版本说明符:

- name: Install multi python packages with version specifiers
  pip:
    name:
      - django>1.11.0,<1.12.0
      - bottle>0.10,<0.20,!=0.11
      - numpy==1.22.4
Run Code Online (Sandbox Code Playgroud)

参考:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html

如果这个答案有帮助,请点个赞。