vlaneo
vlaneo

Reputation: 123

Ansible stop playbook if file present

Is it possible to stop a playbook during his execution if a define file is present on my node and also output to explain why the playbook has stopped?

It is to prevent an accidental re-execution of my playbook on a node that has my application already installed because I generate a password during this install and I don't want to reinitialise this password.

Upvotes: 11

Views: 15190

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56839

You can use the fail module to force a failure with a custom failure message.

Couple this with a check for a file, using the stat module, and this should work easily enough for you.

A quick example or a one run playbook might look something like this:

- 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

Upvotes: 16

Related Questions