Variable Files
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
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.
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 var_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 var_file_path
variable for each host.
Now ls
your tmp
directory and you should see the vars_file-ansible-controller.txt
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. Great 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.