如何让Ansible只运行一步1x



我有一个如下所示的Ansible步骤

- name: pipenv install of bluebird workers, pg helpers, boto helpers
shell: pipenv --python 3.6 install "bluebird-workers>=3.0.24" "pg_helpers>=1.0.9" "boto_helpers>=1.0.28"

上面的步骤在剧本的每一次运行中都保持重新运行。如何使上述步骤只运行一次。我想要它安装1x的语义(另一个任务进行更新——该任务每次都应该重新运行(

尽可能避免使用shell模块

shell模是幂等模;它无法知道它是否在之前运行,因为你可以从shell中运行任何

一个更干净的方法是使用pip模块。

tasks:
- name: Install python libraries.
pip:
name:
- bluebird-workers>=3.0.24
- pg_helpers>=1.0.9
- boto_helpers>=1.0.28
virtualenv: /path/to/your/virtualenv

您需要创建一个可以搜索的工件,例如一个文件。检查Ansible任务中是否存在该文件,如果存在,请不要运行任务。

一个简单的演示策略是:

---
- hosts: localhost
tasks:
- name: test if file is present
stat:
path: /var/pip_installed
register: stat_result
- name: Install something
shell: ls -l > /var/pip_installed creates=/var/pip_installed
become: true
when: stat_result.stat.exists == false

您可以用shell命令替换ls -l

以下是两次运行的输出:

运行一个

$ansible剧本test_ps.yml[警告]:提供的主机列表为空,只有localhost可用。请注意,隐式localhost与"all"不匹配

播放[localhost]***

任务[收集事实]****ok:[localhost]

TASK[测试文件是否存在]****ok:[localhost]

TASK[安装一些东西]****更改:[localhost]

播放回顾***

localhost:ok=3已更改=1无法访问=0失败=0

运行两个

PLAY[localhost]***

任务[收集事实]****ok:[localhost]

TASK[测试文件是否存在]****ok:[localhost]

TASK[安装一些东西]****跳过:[localhost]

播放回顾***

localhost:ok=2已更改=0无法访问=0失败=0

您还可以搜索您知道已安装pip的文件,或者创建自己的文件。

最新更新