14

One of my playbooks contains a task that installs basic Python packages:

---
  -
    name: "Install Python packages: {{ python_packages_to_install }}"
    sudo: true
    pip: name={{ item }}
    with_items: python_packages_to_install

With the following list of packages:

-
  include: python_basics.yaml
  vars:
     python_packages_to_install:
       - virtualenv
       - pss
       - requests
       - comment-builder
       - boto
       - ansible
       - uwsgitop
       - gitpull
       - ipython

The task works correctly and installs the packages:

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)

The problem is that each line is executed using a consecutive SSH command, instead of installing all the packages in a single call.

Is there a way to install multiple Python packages on an Ansible pip command?

3 Answers 3

11

Expanding on Ben's answer, you can also continue to preserve the package list as a yaml list like you have it, and do the projection to a single value when you pass it to the pip module like:

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

Keeps your playbook a little more maintainable...

6
  • I get a msg: this module requires key=value arguments (['name=virtualenv', 'pss', 'requests', 'comment-builder', 'boto', 'ansible', 'uwsgitop', 'gitpull', 'ipython']). Something is probably wrong with stringifying the list.
    – Adam Matan
    Commented Jul 14, 2015 at 16:24
  • A pair of quotes is needed - updating your answer.
    – Adam Matan
    Commented Jul 14, 2015 at 16:33
  • 1
    Oh yeah- I actually tested it with the multiline dictionary syntax, but squashed it down to the shortcut syntax for the answer (which doesn't usually need the quotes unless there's spaces, depending on Ansible version). Thanks for the fix. ;)
    – nitzmahone
    Commented Jul 14, 2015 at 19:04
  • @MattDavis Better to use the YAML syntax, Ansible's key=value syntax is quite hacky and fragile.
    – augurar
    Commented Sep 8, 2016 at 18:24
  • @augurar so I've heard (I'm on the Ansible core engineering team ;) )
    – nitzmahone
    Commented Sep 8, 2016 at 18:30
11

The Ansible pip module now supports the installation of multiple Python packages.

- name: Install multi-python packages
  pip:
    name:
      - virtualenv
      - pss
      - requests
      - comment-builder
      - boto
      - ansible
      - uwsgitop
      - gitpull
      - ipython

With version specifiers:

- 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

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

If this answer was helpful, please give it an up vote.

3

Unlike Ansible apt module, the pip module does not accept a comma-delimited list of packages. Instead, you can provide all the package names as a space delimited string, using the == syntax to specify versions:

python_packages_to_install: "virtualenv==1.11.6 pss requests comment-builder boto ansible uwsgitop gitpull ipython"

If you're like me it may strike you as ugly and impractical to manage. An alternative is to use the requirements option in the pip module to specify a requirements file. Of course you'll need to create the requirements file first, probably using a template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.