0%

0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 55min

0 / 9 lessons complete

Ansible Roles

• 2hr 40min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 42min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 23min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 39min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Conditional Statements

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn how to create and run a new Ansible playbook that will demonstrate the use of boolean variables and conditional statements.

By the end of this lesson, you'll understand how to control the execution of tasks based on the value of a boolean variable.

Creating the Playbook with Boolean Variable

Start by creating a new playbook file named conditional_test.yml.

nano conditional_test.yml

In this playbook, we will target all hosts and define a boolean variable create_file at the top, which will control whether a file should be created or removed.

---
- hosts: all
  vars_files:
    - secret.yml
  vars:
    create_file: True
  tasks:

Let's add a debug message that will show the value of our variable create_file:

    - name: Should the file exist?
      debug:
        msg: "File should exist: {{ create_file }}"

Next, add a task to create the file conditional_test_file.txt in the home directory when create_file is true:

    - name: Create the file
      file:
        path: ~/conditional_test_file.txt
        state: touch
      when: create_file == True

This task uses the file module to create a file, and it executes when create_file is true (yes). The state: touch parameter ensures that the file is created if it doesn't exist.

Now, let's add a task to remove the file if create_file is false:

    - name: Remove the file
      file:
        path: ~/conditional_test_file.txt
        state: absent
      when: not create_file == True

This task also uses the file module but with state: absent to remove the file if it exists. It executes when create_file is false (no).

Running the Playbook

With the playbook ready, you can execute it to see the conditional logic in action. To test both conditions, first run the playbook with create_file set to yes, and then change it to no and run it again.

Use this command to run the playbook:

ansible-playbook conditional_test.yml

Now you should see the debug messages stating True for the variable value, the task for creating the file should be changed, and the task for removing the file should be skipped:

Ansible Conditional Statement
Ansible Conditional Statement

Verifying the Results

After running the playbook with create_file set to yes, check that the file was created on the managed nodes:

ansible all -m command -a "ls ~/conditional_test_file.txt" -e @secret.yml
Ansible Ad-hoc LS Command
Ansible Ad-hoc LS Command

When you set create_file to False and rerun the playbook, use the same command to verify that the file has been removed. Remember that we can supply command line (called extra) variables using the -e argument. You can use this to set create_file to yes or no.

See the JSON example below {create_file: no}:

ansible-playbook conditional_test.yml -e '{create_file: False}'

This time, the create file is skipped

Ansible Playbook run with extra vars in JSON format
Ansible Playbook run with extra vars in JSON format

Notice that I am supplying the extra variables in JSON format. This is because I am passing a boolean value. If I specify -e "create_file = True", the True will be passed in string format, not as a boolean, since the variable type boolean True is not the same value as the string type "True". This breaks our conditional logic since it expects a boolean value for that variable, not a string value.

If you try this, you'll notice that the file is still deleted if you attempt to set the value like so (this passes a string value):

ansible-playbook conditional_test.yml -e "create_file=True"

But, if you pass your extra args in JSON format, it will parse the variable type of True correctly as a boolean:

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments