如何让 Ansible 在已配置的主机上跳过运行任务? [英] How to let Ansible to skip run tasks on a host when its already configured?
问题描述
我们有 2 个剧本 Basic-env
和 Upgrade-env
来管理我们的主机.
We have 2 playbooks Basic-env
and Upgrade-env
to manager our hosts.
ansible-playbook Basic-env
正在做基本的环境设置(yum、生成密钥、安全任务)并注册服务.
The ansible-playbook Basic-env
is doing the the basic environment setup(yum, generate keys, security tasks) and register the services.
ansible-playbook Upgrade-env
正在对软件和配置进行无中断升级 (NDU).
The ansible-playbook Upgrade-env
is doing the non-disruptive Upgrade(NDU) for the software and configuration.
我们会动态地将新主机放入库存,并每 10 分钟运行一次 Basic-env
.
We will dynamic put new hosts into inventory, and run Basic-env
each 10 mins.
问题:是否可以让ansible跳过在旧主机上执行Basic-env
中的任务而只在新主机上运行它们?
Question: Is possible to let ansible skip execute the tasks which in Basic-env
at old hosts and only run them in new hosts?
注意:我们不想为每个新主机单独运行 playbook
,而是为所有主机每 10 分钟运行一次 Basic-env
.
Note: We do not want to run playbook
for each new hosts separately, instead run run Basic-env
each 10 mins for all hosts.
推荐答案
您可以简单地检查主机上的某些内容.假设您的剧本生成文件 /foo/bar
.然后在你的剧本/角色的开头有一个这样的任务:
You could simply check for something on the host. Let's say your playbook generates the file /foo/bar
. Then at the beginning of your playbook/role have a task like this:
- stat:
path: /foo/bar
register: check
然后对于您不想处理的任何任务,您可以应用这样的条件:
Then for any task which you do not want to process you can apply a condition like this:
when: not p.stat.exists
另一种(也许更简洁)的解决方案是使用本地事实.在你的剧本的最后创建一个文件 /etc/ansible/facts.d/setup_complete.fact
,内容如下:
Another (and maybe cleaner) solution is to use local facts. At the very end of your playbook create a file /etc/ansible/facts.d/setup_complete.fact
with the content:
{"setup_complete": "true"}
当 Ansible 下次运行时,ansible_local.setup_complete
将可用.那么你的情况可能看起来像
When Ansible runs the next time, the fact ansible_local.setup_complete
will be available. Then your condition could look like
when: "setup_complete" not in ansible_local
这篇关于如何让 Ansible 在已配置的主机上跳过运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!