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 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

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: yes
  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 == 'yes'

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 == 'yes'

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 no 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.

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 = no", the no will be passed in string format, not as a boolean, which breaks our conditional logic since it expects a boolean value for that variable, not a string value.

See the JSON example below {create_file: no}:

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

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

Now run the ad-hoc command below:

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

This will return several errors because the file can't be found - but this is what we wanted since we removed the file from the file system. This means our playbook actually worked like we wanted!

Conclusion

You have successfully created and executed a playbook that uses a boolean variable to control the creation or removal of a file. This lesson highlights the importance of using boolean values for more straightforward and readable conditional logic in Ansible playbooks. Excellent work! Stay tuned for more lessons where we will explore other Ansible functionalities.

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