Variables in Ansible
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Ansible Playbook components.
https://cloudaffaire.com/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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/etc/ansible/hosts ------------------- [localserver] system1 [webserver] #inventory host variables system2 name1=myvar1 [dbserver] system3 [remoteserver] system2 system3 [remoteserver:vars] #inventory group variables name3=myvar2 ----------------------- |
Instead of directly defining variables in your host file, you can also declare inventory variables using group_vars/ and host_vars/ files.
1 2 3 4 5 6 7 8 9 10 11 |
/etc/ansible/host_vars/system3 ----------------------- --- name2: myvar3 ----------------------- /etc/ansible/group_vars/remoteserver ----------------------- --- Name4: myvar5 ----------------------- |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
##------------------------- ## Ansible : Variables ## ##------------------------- ## Systems used for this demo # hostnames ip os role # --------- ------------ -------- ------------ # system1 192.168.0.10 Centos 7 Control Node # system2 192.168.0.20 Centos 7 Managed Node One # system3 192.168.0.30 Centos 7 Managed Node Two ## Login to the control node (192.168.0.10) ##----------------------- ## Inventory Variables ## ##----------------------- ## inventory variables: host variables, group variables ## create inventory file with host variables and group variables sudo vi /etc/ansible/hosts ------------------- [localserver] system1 [webserver] #inventory host variables system2 name1=myvar1 [dbserver] system3 [remoteserver] system2 system3 [remoteserver:vars] #inventory group variables name3=myvar2 ----------------------- :wq ## inventory variables: host_vars/ and group_vars/ ## create host_vars and group_vars directory sudo mkdir -p /etc/ansible/host_vars/ /etc/ansible/group_vars/ ## inventory host_vars from file sudo vi /etc/ansible/host_vars/system3 ----------------------- --- name2: myvar3 ----------------------- :wq ## inventory group_vars from file sudo vi /etc/ansible/group_vars/remoteserver ----------------------- --- name5: myvar4 ----------------------- :wq ## Create playbook vi myplaybook.yml ---------------------- - hosts: webserver gather_facts: false tasks: - name: Inventory host and group variables debug: msg: - "host var from inventory: {{ name1 }}" - "group var from inventory: {{ name3 }}" - hosts: system3 gather_facts: false tasks: - name: Inventory host and group variables using host_vars and group_vars files debug: msg: - "host var from inventory host_vars files: {{ name2 }}" - "group var from inventory group_vars files: {{ name4 }}" ---------------------- :wq ## Check playbook syntax ansible-playbook myplaybook.yml --syntax-check ## Execute the playbook ansible-playbook myplaybook.yml |
Play Variables:
You can directly declare variables in your Playbook using vars or can access variables from other area using Jinja 2 templating.
1 2 3 4 5 6 7 8 9 10 11 12 |
my_playbook.yml ----------------------- - hosts: system1 gather_facts: false vars: name1: myvar #Directly declared tasks: - name: Playbook Variables debug: msg: - "playbook variable using vars: {{ name1 }}" ----------------------- |
You can also include variables in your Playbook from a file using vars_files or include_vars directives.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
my_playbook.yml ----------------------- - hosts: system1 gather_facts: false vars_files: - myvar.yml tasks: - name: Playbook Variables using vars_files debug: msg: - "playbook variable using vars_files: {{ name1 }}" ----------------------- my_playbook.yml ----------------------- - hosts: system1 gather_facts: false tasks: - include_vars: file: ./myvar.yml name: included_variable - debug: var=included_variable ----------------------- |
Play Variables Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
##------------------ ## Play Variables ## ##------------------ ## Ansible Host file sudo vi /etc/ansible/hosts ------------------- system1 system2 system3 ------------------- :wq ## Variable file vi myvar.yml ----------------------- --- name1: myvar ----------------------- :wq ## Playbook Variables: vars vi my_playbook.yml ----------------------- - hosts: system1 gather_facts: false vars: name1: myvar #Directly declared tasks: - name: Playbook Variables debug: msg: - "playbook variable using vars: {{ name1 }}" ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml ## Playbook Variables: vars_files vi my_playbook.yml ----------------------- - hosts: system1 gather_facts: false vars_files: - myvar.yml tasks: - name: Playbook Variables using vars_files debug: msg: - "playbook variable using vars_files: {{ name1 }}" ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml ## Playbook Variables: include_vars vi my_playbook.yml ----------------------- - hosts: system1 gather_facts: false tasks: - include_vars: file: ./myvar.yml name: included_variable - debug: var=included_variable ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
##-------------------- ## System Variables ## ##-------------------- ## Ansible Host file sudo vi /etc/ansible/hosts ------------------- remote ansible_host=192.168.0.20 ansible_port=22 ansible_connection=ssh ansible_user=debjeet ------------------- :wq ## System Variables: Connection variables, facts and special variables vi my_playbook.yml ----------------------- - hosts: remote gather_facts: true tasks: - name: Ansible Fact variables debug: var=hostvars[inventory_hostname] ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
##------------------- ## Extra Variables ## ##------------------- ## Ansible Host file sudo vi /etc/ansible/hosts ------------------- system1 system2 system3 ------------------- :wq ## Extra Variables: key=value format vi my_playbook.yml ----------------------- - hosts: system1 gather_facts: false tasks: - name: Ansible extra vars debug: var=name1 ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml --extra-vars "name1=myvar1" |
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.