How to let Ansible to skip run tasks on a host whe

2019-08-15 07:22发布

问题:

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