Ansible Playbook: Defining Variables Example

Steven Jul 09, 2026

Ansible, a powerful open-source automation and configuration management tool, often uses variables to make playbooks dynamic and reusable. Defining variables in Ansible playbooks allows you to store values that can be used across tasks, making your playbooks more efficient and maintainable. Let's explore how to define variables in Ansible playbooks with practical examples.

Getting started with Ansible Playbooks | Red Hat Ansible Automation Platform | 2.4
Getting started with Ansible Playbooks | Red Hat Ansible Automation Platform | 2.4

Ansible variables can be defined in several ways, including in-line, in a separate file, or even from external sources like environment variables or facts. In this article, we'll focus on defining variables in-line and in separate files, as they are the most common methods.

4 Ansible playbooks you should try
4 Ansible playbooks you should try

Defining Variables In-line

In-line variables are defined directly in the playbook using the vars: keyword. This method is useful when you only need to use the variable in a single task or play.

Mastering Ansible: A Collection of Essential Labs 🚀
Mastering Ansible: A Collection of Essential Labs 🚀

Here's an example of defining an in-line variable and using it in a task:

```yaml --- - name: In-line variable example hosts: localhost tasks: - name: Print greeting debug: msg: "Hello, {{ my_var }}!" vars: my_var: "World" ```

Using Variables in Multiple Tasks

Master Ansible Conditionals and Loops | Efficient Playbook Creation
Master Ansible Conditionals and Loops | Efficient Playbook Creation

While in-line variables are useful for single tasks, they can become cumbersome if you need to use the same variable in multiple tasks. In such cases, it's better to define the variable at the play level.

Here's how you can define a variable at the play level and use it in multiple tasks:

```yaml --- - name: Play-level variable example hosts: localhost vars: my_var: "World" tasks: - name: Print greeting debug: msg: "Hello, {{ my_var }}!" - name: Print farewell debug: msg: "Goodbye, {{ my_var }}!" ```

Using Variables in Multiple Plays

an info sheet describing the different types of variable and diffrent effects in text
an info sheet describing the different types of variable and diffrent effects in text

If you need to use the same variable in multiple plays, you can define it at the playbook level. This allows you to reuse the variable throughout the entire playbook.

Here's an example of defining a variable at the playbook level:

```yaml --- - name: Playbook-level variable example hosts: localhost vars: my_var: "World" tasks: - name: Print greeting debug: msg: "Hello, {{ my_var }}!" - name: Another play using the same variable hosts: localhost tasks: - name: Print farewell debug: msg: "Goodbye, {{ my_var }}!" ```

Defining Variables in Separate Files

Ansible Loops
Ansible Loops

Defining variables in separate files is a more organized and maintainable approach, especially when dealing with a large number of variables. Ansible uses YAML files to store variables, with the file extension .yml or .yaml.

Here's how you can define variables in a separate file (vars.yml) and use them in a playbook:

Ansible variables: choosing the right location , https://sharethelinks.com/ansible-variables-...
Ansible variables: choosing the right location , https://sharethelinks.com/ansible-variables-...
Ansible Cheat Sheet
Ansible Cheat Sheet
Constant and variable
Constant and variable
the area of different triangles worksheet is shown in black and white, with instructions for
the area of different triangles worksheet is shown in black and white, with instructions for
How to use Ansible Vault to Protect Ansible Playbooks
How to use Ansible Vault to Protect Ansible Playbooks
an exercise sheet with different numbers and symbols for the same set of operations, including one variable
an exercise sheet with different numbers and symbols for the same set of operations, including one variable
Ansible for Beginners: Hands-on-lab
Ansible for Beginners: Hands-on-lab
a table with two different types of numbers on it and the words discrete variable
a table with two different types of numbers on it and the words discrete variable
Ansible Succinctly - Free Download Books
Ansible Succinctly - Free Download Books
a poster with the words, so what is a variable? an activity for variable instruction
a poster with the words, so what is a variable? an activity for variable instruction
a piece of paper with writing on it that says, variable and even odd numbers
a piece of paper with writing on it that says, variable and even odd numbers
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Random Variables and Distributions
Random Variables and Distributions
a diagram with different types of variable variable variable variable variable variable variable variable variable variable variable variable
a diagram with different types of variable variable variable variable variable variable variable variable variable variable variable variable
a white board with writing on it next to a clock and some other things in front of it
a white board with writing on it next to a clock and some other things in front of it
a piece of paper with writing on it that says, eliminating a variable - example
a piece of paper with writing on it that says, eliminating a variable - example
📊 Dependent vs. Independent Variable
📊 Dependent vs. Independent Variable
Continuous variable
Continuous variable
Variables & Expressions Power Point and Notes
Variables & Expressions Power Point and Notes
an open book with instructions on how to use the model for writing and creating something
an open book with instructions on how to use the model for writing and creating something

```yaml # vars.yml my_var: "World" ``` ```yaml --- - name: Variable file example hosts: localhost vars_files: - vars.yml tasks: - name: Print greeting debug: msg: "Hello, {{ my_var }}!" ```

Loading Variables from Multiple Files

You can load variables from multiple files by listing them under the vars_files: keyword. This allows you to organize your variables into separate files based on their purpose or scope.

Here's an example of loading variables from multiple files:

```yaml --- - name: Multiple variable files example hosts: localhost vars_files: - vars.yml - secrets.yml tasks: - name: Print greeting debug: msg: "Hello, {{ my_var }}!" ```

Using Environment Variables as Variables

Ansible also allows you to use environment variables as playbook variables. This can be useful when you want to keep sensitive data like passwords or API keys out of your playbooks.

Here's an example of using an environment variable as a playbook variable:

```yaml --- - name: Environment variable example hosts: localhost vars: my_var: "{{ lookup('env', 'MY_VAR') }}" tasks: - name: Print greeting debug: msg: "Hello, {{ my_var }}!" ```

In this example, the value of MY_VAR is retrieved from the environment and used as the value of the my_var variable.

Ansible provides a powerful and flexible way to define and use variables in playbooks. By understanding how to define variables, you can create dynamic, reusable, and maintainable playbooks that streamline your automation workflows. Happy automating!