Updated April 11, 2023
Introduction to Ansible Variables
In Ansible, variables play a very important role, as these are the reference points which your playbooks will refer to contain data. This is like any programming language. Also, in most ways’ variable behaves the same in Ansible as in some other programming language. In Ansible, variables can be defined at various places and in many ways. Variables have specifications also like valid name, defining variables, calling a variable, Ansible special variable, array of variable and importing from another file. We will try to explore a few of these in this article.
Variables in Ansible
First, we need to understand what valid names for a variable in Ansible are. So, for this you must remember below:
- Variable name must start with a
- Variable name should contain characters, numbers or underscore
- Some of the valid variable names are like abc1,
- Some of the invalid variable names are like abc-1, abc%1, 12, abc xyz, xyz
Now, Let’s discuss the ways to define variables in Ansible. In Ansible, Variables can be defined in the following places: –
1. Inventory file
You can add Variables in inventory file which can have a scope limited to a specific host, group or all hosts in the file. This is helpful if you want to use the same variable to contain different values for different hosts defined in the same inventory
[host_list]
test1 ansible_connection=ssh ansible_user=my_user
test2 ansible_connection=ssh ansible_user=my_user1
Also, you can try to define variables for the whole group like below: –
[Host_List]
host1
host2
[Host_List:vars]
ansible_connection=ssh
ansible_user=my_user
ntp_server=ntp.test.com
2. Playbook
In Playbooks, you can define variables which are referred in same playbook’s tasks. The position of defined variables also decides the scope of variables.
Take a simple like below: –
- hosts: web
vars:
http_port: 80
- hosts: app
vars:
http_port: 8080
3. Imported files and Roles
In production environments, you will be dealing with this, as using roles is more organized and In role structure, you can define your variable in a file and include in playbook.
Now take an example, where you have a variable file named as var_name.yaml with the below contents.
name1: test1
We can include this file in a playbook and use the variable name1 as below
---
name: writing something
hosts: all
tasks:
- include_vars: yaml
- debug: msg="My name is {{ name1 }}"
4. Variables passed on command line
Variables can also be passed on command line like below using — extra-vars argument or -e in key=value format and variable separated by space.
ansible-playbook test.yaml --extra-vars "one_var=test1 other_var=test2"
Also note that all values passed in above way will be treated as strings. If you have some values in Integer, Booleans etc. then better way is to define these in JSON format, like below:
ansible-playbook test.yml --extra-vars '{"one_var":"test1","other_var":"test2"}'
Adding to above, if you have a JSON or YAML file then also you can import it in your playbook like below during run time.
ansible-playbook test.yaml --extra-vars "@file1.json"
5. Variables via Jinja2 templates
Once you have your variables defined you can use them in a playbook using Jinja2 templating system. Jinja2 provides many filter which you can use along with variable to get more tasks done on Control server end, So that Hosts not need to send unwanted information one end to another.
Like see below filter and variable use, where the variable test_var have will be passed and if no value is given to it a default value equal to 9 will be assigned to it.
{{ test_var | default(9) }}
Similarly, when passing the value of a variable is mandatory
{{ test_var | mandatory }}
In above locations, you can mention user defined variables. So now let’s discuss about the type of variable in Ansible: –
6. User Defined Variables
The variables which you define in your playbooks simply in key-value pair. These can be any value which is going to be used in your
An example is below, here we defined a variable colour and then used it later in a task simply where command module is running a command.
---
- hosts: all
vars:
colour: blue
tasks:
- name: this is just a test
command: /bin/echo "{{ colour }}"
7. System Facts
These are the default variables in which Ansible Control Server stores information after discovering from nodes. By default, ansible_facts contain all the information in JSON format after setup runs to gather a lot of information in the form of facts from remote nodes. Then you can use this information like below variable in your plays.
{{ ansible_facts['devices']['xvda']['model'] }}
{{ ansible_facts['nodename'] }}
8. Registered Variables
These are the variables in which the output of your task will be stored on Ansible Control Server. In simple word, when you want to run a command on remote computer and then store the output in a variable and use a piece of information from the output later in your plays. This kind of usage is possible by registered variables.
In fact, it is somehow like the system Facts which are discovered and fetched by setup module. Here whatever command you run its output will be saved in JSON format and then you use that information as same as you used facts.
Let’s take an example, where you run a command service https status and register its output value in a variable apache_status. Now, when you debug the variable. It will give you a lot of information about the target system and the output it received in JSON format.
---
hosts: all
gather_facts: no
tasks:
name: Get web server status
command: service httpd status
register: apache_status
name: web server status only
debug:
var: apache_status
The JSON output will have fields like stdout, stdin, changed, rc etc. Using these fields we can get use a specific value, like below, where a variable apache_status[‘stdout_lines’] contains a specific piece of information that gives only service status.
debug:
var:apache_status.stdout_lines
9. Special Variable
These are the variables which is default to the system and can not be set by the user directly. But these will contain the data related to System’s internal state like version, forks, connection method etc. Some examples of these variables are inventory_file, inventory_dir, ansible_connection, ansible_host,
Conclusion
Using Ansible variables efficiently makes you easily work with loops, in multi hosts environments, multi connection type scenarios and many more setups where the hard coding a value is adding more difficulties. Understanding variables and usage is very important and you can learn more about the Ansible variables concept from Ansible community documentation.
Recommended Articles
This is a guide to Ansible Variables. Here we discuss the introduction to Ansible variables concept along with 9 different valid variables. You may also have a look at the following articles to learn more –