How to force an instance into a failed state during provisioning phase based on certain conditions

In certain scenarios, you may wish to transition the instance provision to a failed state based on specific conditions within your workflow tasks. The following examples demonstrate various task types where this can be accomplished by exiting with code 1.

Shell

#!/bin/bash

myVar='pass'
if [ "$myVar" = "fail" ]; then
    exit 1
fi

Python

import sys

myVar ='pass'
if myVar == 'fail':
	sys.exit(1) 

PowerShell

$myVar = 'pass'
if ($myVar -eq 'fail') {
    exit 1
}

With Ansible playbooks we can use the built-in fail module to achieve the same.

Ansible Playbook

---
- hosts: all
  vars:
    myVar: pass
  tasks:
    - name: Fail Ansible Task
      ansible.builtin.fail:
        msg: The task will fail based on the value of the myVar
      when: myVar == "fail"

Here is an example where Iā€™m demonstrating the Ansible Tower task type utilizing the above ansible.builtin.fail module during the post-provision phase of the operational workflow.

When the task is put into a failed state the provision will fail as shown below.

1 Like