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

Variable Files

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 about Ansible variable files, which are essential for managing and organizing variables in a more structured and readable manner. This concept is particularly useful when you have a large number of variables or when variables need to be shared across multiple playbooks. By the end of this lesson, you will know how to create a variable file and use it within your playbook.

Introduction to Variable Files

Variable files in Ansible are typically written in YAML format. They allow you to keep your variable data separate from your playbook, making your Ansible code cleaner and more maintainable. This separation is particularly useful when dealing with different environments (like development, testing, and production), each with its own specific configuration.

Creating a Variable File

To manage our variables for the webservers group, we'll start by creating a variable file named webservers_vars.yml. This is a straightforward process that involves using a text editor. In this case, we can use nano, a popular command-line text editor. Open nano by typing the following command:

nano ~/code/my_vars.yml 

In the webservers_vars.yml file, we'll define a variable that specifies the path of the file we want to create on each managed node. The content of the variable file should look like this:

var_file_path: /tmp/vars_file_{{ ansible_hostname }}.txt

This YAML file defines a single variable file_path, which is a template string. The {{ ansible_hostname }} is a placeholder that Ansible replaces with the hostname of each managed node when the playbook is executed. By using this variable, we ensure that each host creates a file with its own hostname in the path, maintaining uniqueness across different nodes.

Using Variable Files in Playbooks

After creating the variable file, the next step is to incorporate it into our existing playbook, first_playbook.yml. We want Ansible to load the variables from webservers_vars.yml when the playbook is executed. To achieve this, we modify the playbook to include the variable file.

Open your playbook with the command below:

nano ~/code/first_playbook.yml

This will bring up the existing playbook content for editing. We need to add a new section at the beginning of the play that tells Ansible to use the variables defined in our file. We need to add the following code under our hosts block for the web servers:

The updated playbook should look like this:

---
- hosts: all
  vars_files:
    - my_vars.yml
  tasks:
    - name: Create an empty file
      file:
        path: "{{ var_file_path }}"
        state: touch

In this revision, the vars_files section is added just under the hosts line. This section is crucial as it instructs Ansible to load the variables defined in webservers_vars.yml.

The playbook then uses the file module in its task, utilizing the file_path variable to specify the path for the file creation. By structuring our playbook this way, we achieve a clean separation of variables from the playbook logic, enhancing readability and maintainability.

Verifying the Playbook Execution

To verify that the playbook is working correctly with the variable file, you can execute it with the following command:

ansible-playbook first_playbook.yml

After running this command, Ansible will apply the playbook to the hosts in the webservers group. It should create an empty file at the path defined in the file_path variable for each host.

Now ls your tmp directory and you should see the vars_file-ansible-controller.txt

Ansible Playbook Run with Var Files
Ansible Playbook Run with Var Files

Conclusion

You've now learned how to create and use variable files in Ansible. This approach helps keep your playbooks clean and your variables organized, especially as your Ansible projects grow in complexity. reat job! You're ready to move on to the next lesson.

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