This is a list of the different types of variables that are available in Ansible.
The list is arranged in reverse order of precedence. If the same variable name is set to different values by different methods from this list, the methods later in the list override values set by the earlier ones. A value set by the second method on the list overrides a value set by the first, the third overrides the second, and variables of the last type in this list ("extra variables" set on the command line) override values set all of the other methods.
Role defaults are variables defined as defaults within roles.
$cat roles/myrole/defaults/main.ymla_variable_name: myvalue
Inventory file group variables are group variables that are defined in an inventory file.
[atlanta] host1 host2 [atlanta:vars] ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com
Inventory group_vars/all variables are defined within an all group variables file found within the group_vars directory tree adjacent to the current inventory file.
. ├── group_vars │ └── all └── inventory[user@host ~]#cat group_vars/alla_variable_name: myvalue
Playbook group_vars/all variables are defined within an all group variables file found within the group_vars directory tree adjacent to the current playbook.
. ├── group_vars │ └── all └── myplaybook.yml[user@host ~]#cat group_vars/alla_variable_name: myvalue
Inventory group_vars/* variables are defined with a particular, group variables file other than all within the group_vars directory tree adjacent to the current inventory file.
. ├── group_vars │ └── some-group └── inventory[user@host ~]#cat group_vars/some-groupa_variable_name: myvalue
Playbook group_vars/* variables are defined with a particular group variables file other than all within the group_vars directory tree adjacent to the current inventory file.
. ├── group_vars │ └── some-group └── myplaybook.yml[user@host ~]#cat group_vars/some-groupa_variable_name: myvalue
Inventory file host variables are host variables defined in an inventory file.
[atlanta] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
Inventory host_vars/* variables are variables that are defined in a host-specific variables file under the host_vars directory that is adjacent to the current inventory file.
. ├── host_vars │ └── some-host └── inventory[user@host ~]#cat host_vars/some-hosta_variable_name: myvalue
Playbook host_vars/* variables are variables that are defined in a host-specific variables file under the host_vars directory that is adjacent to the current playbook file.
. ├── host_vars │ └── some-host └── myplaybook.yml[user@host ~]#cat host_vars/some-hosta_variable_name: myvalue
Host facts are variables associated with a host and are defined in a particular way (as facts).
- set_fact:
one_fact: foo
other_fact: bar
Play variables are defined in the vars block that appears at the top of a play.
---
- name: this is a play
hosts: groupname
vars:
foo: foovalue
bar: barvalue
Play vars_prompt variables are defined in the vars_prompt block that appears at the top of a play.
---
- hosts: all
remote_user: root
vars:
from: "camelot"
vars_prompt:
- name: "name"
prompt: "what is your name?"
- name: "quest"
prompt: "what is your quest?"
- name: "favcolor"
prompt: "what is your favorite color?"
Play vars_files variables are defined in a file that is loaded by virtue of having its file path listed in the vars_files block that appears at the top of a play.
$cat vars/example.ymlfoovar: fooval barvar: barval$cat myplaybook.yml--- - name: my playbook hosts: myhosts vars_files: - vars/example.yml
Role variables are defined in role/vars/main.yml.
$cat roles/myrole/vars/main.ymlfoovar: fooval barvar: barval
Block variables are defined under the vars heading within a block of Ansible statements that are logically grouped using the block module.
- block:
- debug:
var: var_for_block
vars:
var_for_block: "value for var_for_block"
Role parameters are defined under the vars heading that appears as an argument in conjunction with include_role.
---
- hosts: webservers
tasks:
- include_role:
name: foo_app_instance
vars:
dir: '/opt/a'
app_port: 5000
Include parameters are variables defined in conjunction with an include statement.
---
- name: This is an example play
hosts: agroup
vars:
foo: bar
- include: amazon.yml
vars:
application: FooServer
instance_type: t2.micro
instance_count: 1
- include: amazon.yml
vars:
application: BarServer
instance_type: t2.micro
instance_count: 1
Extra variables (extra vars) are defined at the command line using the --extra-vars (-e) option.
$ansible-playbook -e "version=1.23.45 another_variable=foobarino" myplaybook.yml