Basics of Variables

Learn how to use variables to introduce reusability in Ansible.

Overview

Using variables gives us the flexibility required in configuration management projects. Variables are essential components of Ansible playbooks and roles that enable us to parameterize our tasks, creating a dynamic, flexible, and reusable configuration.

We can parameterize tasks through variables, which in turn leaves us with playbooks and roles that are not only adaptable but also easier to maintain. Instead of hardcoding values directly into our tasks, we use variables to centrally manage the part of our configuration that’s bound to constantly change.

Here’s an example that shows a virtual host configuration in Apache with the use of relevant variables, such as web_server_port, web_server_document_root, APACHE_LOG_DIR, and web_server_index_file to keep the configuration dynamic:

Get hands-on with 1200+ tech skills courses.