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.

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.

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.

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

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

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

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:




















```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!