Introduction

I wanted to demonstrate how to conditionally skip a module/Task during execution runtime in Ansible. In this example, I will skip set_fact task in Ansible.

Bypass a set_fact Task in Ansible

You can use conditions to skip tasks in Ansible. Here’s a simple example:

  • hosts: localhost
    gather_facts: no
    vars:
    skip_fact: true # Change this to false to run the set_fact task.
    • name: Conditionally set a fact
      set_fact:
      my_fact: “This fact is set”
      when: not skip_fact # when skip_fact = true skip this task
    • name: Display the fact if it is set
      debug:
      msg: “The fact is: {{ my_fact }}”
      when: my_fact is defined
    • name: Indicate if the fact was skipped
      debug:
      msg: “The set_fact task was skipped”
      when: my_fact is not defined

Quick Summary

1. Variable: skip_fact controls whether the set_fact task is executed.

2. Condition: The set_fact task runs only if skip_fact is false.

3. Debug Output: Messages show whether the fact was set or skipped.

This is a simple and effective way to control task execution in Ansible!

Leave a comment

Trending