我如何确认使用 ansible 安装了 python 包

How would I confirm that a python package is installed using ansible

我正在写一个 ansible playbook, and I would first like to install pew,并过渡到那个 pew 环境以安装其他一些 python 库。

所以,我的剧本看起来像这样...

tasks:

# task 1
- name: install pew if necessary
  command: pip install pew

# task 2
- name: create new pew environment if necessary
  command: pew new devpi-server

# task 3
- name: transition to pew environment
  command: pew workon devpi-server

# task 4
- name: install devpi-server
  command: pip install devpi-server

# task 5
- name: run devpi server
  command: devpi-server ## various args

本着保持我的剧本幂等的精神,我想只在必要时完成所有这些任务。

所以,如果 pew 尚未安装,我只想安装它;如果它不存在,我只想创建一个新的 pew 环境;如果我们还没有,我只想在 pew 环境上工作。 ..等等等等...

有没有人对如何做到这一点有好的建议?我有点熟悉 ansible 条件,但只有当它是 "is a file downloaded or not"

我还没有确定是否安装了程序、是否加载了虚拟环境等。

您可以在 Ansible 中使用 Pip module 来确保安装某些包。

对于你的条件,我会参考:How to make Ansible execute a shell script if a package is not installed and http://docs.ansible.com/ansible/playbooks_conditionals.html#register-variables - 这些应该让你走上正确的轨道。

所以你的剧本看起来有点像:

- pip: name=pew

- name: Check if pew env exists
  command: pew command
  register: pew_env_check

- name: Execute script if pew environment doesn't exist
  command: somescript
  when: pew_env_check.stdout.find('no packages found') != -1