How to let Ansible to skip run tasks on a host when its already configured?

佐手、 提交于 2019-12-24 13:04:08

问题


We have 2 playbooks Basic-env and Upgrade-env to manager our hosts.

The ansible-playbook Basic-env is doing the the basic environment setup(yum, generate keys, security tasks) and register the services.

The ansible-playbook Upgrade-env is doing the non-disruptive Upgrade(NDU) for the software and configuration.

We will dynamic put new hosts into inventory, and run Basic-env each 10 mins.


Question: Is possible to let ansible skip execute the tasks which in Basic-env at old hosts and only run them in new hosts?

Note: We do not want to run playbook for each new hosts separately, instead run run Basic-env each 10 mins for all hosts.


回答1:


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

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"}

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


来源:https://stackoverflow.com/questions/35592565/how-to-let-ansible-to-skip-run-tasks-on-a-host-when-its-already-configured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!