如果文件存在,Ansible如何停止playbook执行

10

如果在我的节点上存在定义文件,那么在playbook执行过程中停止它是否可能,并输出解释为什么playbook已经停止?

这是为了防止在已安装应用程序的节点上意外重新执行playbook,因为我在安装过程中生成了密码,不希望重新初始化此密码。

1个回答

15

你可以使用fail 模块来强制执行自定义的失败消息。

结合使用stat 模块来检查文件,这样应该就足够容易地工作了。

一个快速示例或单次运行的playbook可能如下所示:

- name: check for foo.conf
  stat: path=/etc/foo.conf
  register: foo

- name: fail if already run on host
  fail: msg="This host has already had this playbook run against it"
  when: foo.stat.exists

- name: create foo.conf
  file: path=/etc/foo.conf state=touch

测试代码后它没有工作,我得到了这个消息(我会继续在我的一边搜索): FAILED! => {"failed": true, "msg": "条件检查 'foo.exists' 失败。错误是:在评估条件时出错(foo.exists):'dict object' 没有属性 'exists'\n\n错误显示在 '/var/lib/awx/projects/_5__safetyportal/Safety_Cube_master/main.yml' 中的第 12 行,第 5 列,但是根据确切的语法问题,文件中的其他位置也可能发生错误。\n\n有问题的行似乎是:\n\n\n - name: fail if already run on host\n ^ 这里\n"} - vlaneo
有了'.stat.exists',它运行良好,感谢您的帮助(您能编辑您的评论来更正吗?) - vlaneo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接