How to Work With Ansible Variables?
Variables are temporary storage of data.In order to make things dynamic, we can use variables in Ansible.
There are three variables available in Ansible
- Global variables-These variables defined in the configuration file(ansible.cfg)
- Inventory variables
- Playbook Variables
Inventory Variables can be of two subtypes
- Host Variable.
- Group Variable
The rule is that the global variables will override Inventory variable and playbook variables. In order to edit global variable we need to open ansible.cfg in vi editor.
>vi ansible.cfg
Here we can see a set of default values. If we add some variables or uncomment anything, the variable will become global.
In order to edit Inventory variables, we need to edit host file in vi editor.
>vi host
We can see here,all variables are Inventory variable.So whatever defined in inventory is referred as inventory level variables.
Host vs Group Variables
In Inventory,we get two subtypes of Inventory variables.They are host and group variable. When both are defined, host will take priority as individual group will finally get resolved to individual hosts.
Example:
[Web] webserver1.techtravelhub.com webserver2.techtravelhub.com [db] dbserver1.techtravelhub.com dbserver2.techtravelhub.com [servers:children] Web db
The hostvars
The hostvars allows us to fetch variables about all the hosts that the in the current play. To access hostvars below is the syntax-
${hostvars.hostname.fact} Or {{hostvars.hostname.fact}}
Hostvars variables make more abstract template from our environment.
The groups variable
The groups variable consists of a list of all hosts in the inventory grouped by the inventory group. The groups variable allows us to access to all hosts that are configured.
The groups variable actually does not contain the actual hosts in the group. It just contains the string representation of the host names present in the inventory.
We need to use nested variable expression to fetch the hostvars variable if required.
The group_names variable
The group_names variable contains a list of strings with the names of all groups the current host is in. This variable is mostly used for skipping a task or in a template as a condition.
The inventory_hostname variable
inventory_hostname stores the hostname of the server as recorded in the inventory.This is useful when we are doing the initial setup of a machine and changing the hostname.This can only be used if we opt to avoid setup module.
The inventory_hostname _short variable
inventory_hostname _short is similar to inventory_hostname but includes the characters up to the first dot.
Like
host.example.com will return host.
The inventory_dir variable
inventory_dir says the path name of the directory where inventory file is present.
The inventory_file variable
The inventory_file variable stores the path of the directory where the inventory file is present along with the file name.
How to find a particular file using variable?
All modules available in ansible take variables as part of their arguments in a {{ and }} manner.We can select and load a particular file based on a variable.
- - - -name:configure httpd using correct architecture hosts:all user:ansadmin tasks: -name:copy in the correct httpd config file copy: src:"files/httpd.{{ansible_architecture}}.conf" dest:"/etc/httpd/httpd.cfg" …
Environment variables:
Few Linux flavors can take advantages of certain environment variables. Internally Ansible sets the environment variable into the python code. It implies that all other ansible modules that uses environment variables can use them once they are set.
Modules like get_url, yum, apt etc. uses environment variable. We can use environment variable for the below written cases-
- Running application installers.
- Adding extra items to the path where using the shell module.
- Loading libraries from a place not included in the system library search path.
- Using an LD_PRELOAD hack while running a module.
The variable the ansible supports are as follows
- String
- Numbers
- Float
- List
- Dictionary
- Boolean
Naming convention of Ansible variables
Any ansible variable should start with a letter.The name can be built with-
- Letters(like package _name)/package.
- Numbers (like package _names5)
- Underscore (like user_input_package)
Invalid ansible variable names could be—
- Having multiple words(like package name)
- Putting a dot in between of the variable ( like host.part)
- Plan number ( like – 3)
- Putting a hyphen in between of the variable.(like host-part)
Variable in playbook
The syntax to provide variables in the playbook in the format- vars: key.The key will have a key value pair where key takes the variable name and the value is the actual value.
- -hosts:web vars: -package-name:"httpd" . . . ...
Variables in a global file
If we want to separate task and data, then we need to create separate .yml files.We can create many files containing different sets of data. The data needs to provide in the same way we put in playbook that is vars: key.The key will have a key and value pair. The key will contain the key value and value will contain actual value.
- # Environment variables Servers: server_ip:"{{lookup('env','IP')}}" server_port:"{{lookup('env','port')}}" ...
We need to specify the file name in the var section in the playbook.
- -hosts:web vars_files -myvar1.yml -myvar2.yml . . . -myvarN.yml