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

Zac*_*ack 4 python conditional pip virtualenv ansible

我正在编写ansible playbook,我首先想安装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
Run Code Online (Sandbox Code Playgroud)

本着保持我的剧本具有幂等性的精神,我希望仅在必要时才执行所有这些任务。

因此,我只想在尚未安装 pew 的情况下安装它,如果它不存在,我只想创建一个新的 pew 环境,如果我们还没有,则只在 pew 环境中工作......等等。 ..

有没有人有关于如何实现这一点的好建议?我对 ansible 条件有些熟悉,但只有当它类似于“文件是否已下载”时

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

小智 5

您可以使用Ansible 中的Pip 模块来确保安装了某些包。

对于您的条件,我会参考:如果未安装软件包http://docs.ansible.com/ansible/playbooks_conditionals.html#register-variables,如何使 Ansible 执行 shell 脚本- 这些应该让您正确追踪。

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

- 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
Run Code Online (Sandbox Code Playgroud)