0

Context: provisioning fresh new servers.

I would like to provision them just once especially the update part. After launching the first playbook bootstrap.yml I made it leave a file in a folder for me to know that the playbook ran well.

So then in the same playbook I would have to add a condition above every task (which I did) to check for the existance of this file. If file exists skip running the playbook against all those machines who have that file.

Problem: How do I add the condition to run the playbook only if the file isn't found? I don't want to add a "when:" statement for each of my own tasks I find it silly.

Question: Does anyone have a better solution that maybe solves this with a single line or a parameter in the inventory file that I haven't thought of?

edit:

This is how i check for the file

- name: Bootstrap check
    find:
      path: /home/bot/bootstrapped-ok
    register: bootstrap

and then when condition would be:

when: bootstrap.matched == 0

so if file is not found run the entire playbook.

  • Condition on a role, import_role, include_role, block, etc., Pre checks in `pre_tasks` using `end_host`, `end_play`.... there are many possibilities. – Zeitounator Apr 01 '22 at 17:48

1 Answers1

2

I think you may be over-complicating this slightly. Would it be accurate to say "I want to bail early on a playbook without error under a certain condition?"

If so, you want "end_host"

How do I exit Ansible play without error on a condition

Just begin with a check for the file, and end_host if it's found.

  - name: Bootstrap check
    stat:
      path: /home/bot/bootstrapped-ok
    register: bootstrap

  - name: End the play if previous provisioning was successful
    meta: end_host
    when: bootstrap.stat.exists == True

  - name: Confinue if bootstrap missing
    debug:
      msg: "Hello"
Matt Blaha
  • 921
  • 6
  • 9
  • Thank you very much, this solved my issue! I was definitely over complicating it. But also wasn't aware of meta: end_host existance. Thank you! – simone.benati Apr 01 '22 at 18:12
  • Glad it helped. It is a very forest for the trees kind of problem, easy to go down the wrong path. Please do up vote/mark this answer as accepted if it fixed you up, helps me out. – Matt Blaha Apr 01 '22 at 19:23
  • I did Matt, but it tells me that I'm too new of an user and will need 15 reputation points before the upvote actually goes public.. – simone.benati Apr 01 '22 at 19:46
  • Oh of course. Welcome to Stack Overflow. – Matt Blaha Apr 01 '22 at 21:07