Variables In Ansible

Variables In Ansible

Variables in Ansible

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Ansible Playbook components.

In this blog post, we will discuss variables in Ansible. Variables are a way to pass or store values. Like any other systems, Ansible also support variables. You can use variables in different ways and in different places in Ansible. Ansible variables always begin with a letter ([A-Za-z]), and can include any number of underscores (_) or numbers ([0-9]).

Variable Types:

  • inventory variables
  • play variables
  • system variables
  • extra variables

Inventory Variables:

You can define variables in your Ansible host file. Ansible supports two types of inventory variables Host Variables and Group Variables. Host variables are applied to specific host for which the variable is declared. On the other hand, Group Variables are applied to a group of hosts.

Instead of directly defining variables in your host file, you can also declare inventory variables using group_vars/ and host_vars/ files.

Note: The group_vars/ and host_vars/ directories can exist in the playbook directory OR the inventory directory. If both paths exist, variables in the playbook directory will override variables set in the inventory directory. You can access theses variables in playbook using the Jinja2 templating system {{ variable_name }}

Inventory Variables Demo:

Play Variables:

You can directly declare variables in your Playbook using vars or can access variables from other area using Jinja 2 templating.

You can also include variables in your Playbook from a file using vars_files or include_vars directives.

Play Variables Demo:

System Variables:

Ansible also has system variables which are used by Ansible for internal use. Some of the system variables for example connection variables can be defined by the user. Some system variables are also derived from a remote system called facts. Below are the three major types of system variables.

  • Magic Variables: These variables cannot be set directly by the user; Ansible will always override them to reflect internal state.
  • Facts: These are variables that contain information pertinent to the current host (inventory_hostname). They are only available if gathered first.
  • Connection Variables: Connection variables are normally used to set the specifics on how to execute actions on a target. Most of them correspond to connection plugins, but not all.

System Variables Demo:

Extra Variables:

Ansible also supports passing variables directly at the command line using the –extra-vars (or -e) argument. Variables can be defined using a single quoted string (containing one or more variables) using one of the formats below

  • key=value format: ansible-playbook release.yml –extra-vars “version=1.23.45 other_variable=foo”
  • JSON string format: ansible-playbook release.yml –extra-vars ‘{“version”:”1.23.45″,”other_variable”:”foo”}’
  • vars from a JSON or YAML file: ansible-playbook release.yml –extra-vars “@some_file.json”

Extra Variables Demo:

Note: Values passed in using the key=value syntax is interpreted as strings.

Hope you have enjoyed this article. In the next blog post, we will discuss operators in Ansible.

To get more details on Ansible, please refer below Ansible documentation.


Leave a Reply

Close Menu