I have a main playbook which imports other playbooks to perform actions in several machines.
machine1 is a hub where test results are collected, after being executed on other machines:
- name: clear state
import_playbook: playbook-clear-machines-state.yml
- name: prepare run
hosts: machine1
roles:
- prepare_test_run
- name: run_tests_in_machine2
import_playbook: playbook-run-tests-machine2.yml
ignore_error: yes
...
What I would like to guarantee is that only an instance is running at a time (i.e. no one is able to run tests if an user is already doing so).
I thought of using a lock file in machine1 as a way of preventing that, in the following manner:
- name: check temp file
hosts: machine1
tasks:
- name: get stats of lock file
stat:
path: ./ansible.lock
register: st
- fail:
msg: "tests already running"
when:
- st.stat.isreg is defined and st.stat.isreg
- name: create lock file
file:
path: ./ansible.lock
state: touch
- name: run all tests
import_playbook: playbook-main.yml
ignore_errors: yes
- name: delete lock file
hosts: machine1
tasks:
- name: delete ansible.lock file
file:
path: ./ansible.lock
state: absent
The problem here is that I need to make sure the lock file is always deleted in the end - even if there was an error at an earlier play.
How could I accomplish this? I tried to look into blocks and handlers but they don't seem appropriate to use at this level. Can anybody point me to a strategy/way to accomplish this?
Many thanks, José.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…