0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Variable Precedence

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 explore the concept of variable precedence in Ansible. Understanding how Ansible prioritizes variables is crucial for managing complex playbooks and ensuring that your automation behaves as expected.

By the end of this lesson, you'll have a clear understanding of the different levels at which you can define variables in Ansible and how the precedence rules affect their values.

Understanding Variable Precedence

Variable precedence in Ansible is the order in which the system decides which value to use when you have the same variable defined in multiple places. Ansible uses a specific hierarchy to resolve this, and understanding this hierarchy is key to effectively managing and utilizing variables in your playbooks.

Here's a simplified view of the variable precedence hierarchy in Ansible, from highest (most prioritized) to lowest:

It's important to remember that while this hierarchy exists, good practice in Ansible is to keep variable usage clear and understandable. Over-reliance on variable precedence can make your playbooks hard to read and maintain.

Understanding the Task

We will create a task in an Ansible playbook that touches a file on the remote server. The name of the file will be determined by a variable named file_name. We will define this variable in two places:

Through this exercise, you will see how the command-line variable takes precedence over the inventory variable.

Setting Up the Inventory File

Modify your inventory file to include the file_name variable with a value of inventory. This will be used when in a task that creates a file that uses the variable to help us understand which variable took precedence. Here's an example of how your inventory file should look in YAML format:

webservers:
  hosts:
    managed-node-1:
      ansible_become_pass: "{{ become_passwords.managed_node_1 }}"
      file_name: "inventory"
    managed-node-2:
      ansible_become_pass: "{{ become_passwords.managed_node_2 }}"
      file_name: "inventory"
controllers:
  hosts:
    ansible-controller:
      ansible_become_pass: "{{ become_passwords.ansible_controller }}"
      file_name: "inventory"

Creating the Playbook

Now, let's create a playbook named create_file.yml with a task to touch a file with the path "~/{{ file_name }}_variable_took_precedence":

---
- hosts: all
  vars_files:
    - secret.yml
  tasks:
    - name: Touch a file with a dynamic name
      file:
        path: "~/{{ file_name }}_variable_took_precedence"
        state: touch

In this playbook:

Since we are not setting become to true or yes, we will expect this file to be located in our paulh user's home directory NOT in the /root directory.

Running the Playbook with Variable Precedence

To demonstrate variable precedence, run the playbook twice: once without any extra variables and once with an extra variable. This will show how the command-line variable overrides the inventory variable.

First, run the playbook without extra variables:

ansible-playbook create_file.yml

This command will create a file named inventory in the home directory of the user on each managed node, as the file_name variable is being pulled from the inventory file.

Ansible Variable Precedence from Inventory
Ansible Variable Precedence from Inventory

Next, run the playbook with an extra variable:

ansible-playbook create_file.yml -e "file_name=cmd"

This time, a file named cmd will be created in the home directory, demonstrating that the extra variable provided at the command line takes precedence over the inventory variable.

Ansible CMD Taking Precedence over Inventory Variable
Ansible CMD Taking Precedence over Inventory Variable

Homework

Take this on as an exercise to do on your own: Assign a variable at the playbook level and see if it takes precedence over the inventory / cmd arguments.

Conclusion

You have completed a practical exercise on variable precedence in Ansible. This exercise demonstrates the importance of understanding how variables are prioritized in Ansible, allowing you to control playbook behavior dynamically.

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