Bookmark this page

Appendix F. Ansible Variable Types

Ansible Variable Types and Precedence

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.

  1. Role defaults are variables defined as defaults within roles.

    $ cat roles/myrole/defaults/main.yml
    a_variable_name: myvalue
  2. 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
  3. 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/all
    a_variable_name: myvalue
  4. 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/all
    a_variable_name: myvalue
  5. 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-group
    a_variable_name: myvalue
  6. 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-group
    a_variable_name: myvalue
  7. 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
  8. 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-host
    a_variable_name: myvalue
  9. 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-host
    a_variable_name: myvalue
  10. 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
  11. 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
  12. 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?"
  13. 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.yml
    foovar: fooval
    barvar: barval
    
    $ cat myplaybook.yml
    ---
    - name: my playbook
      hosts: myhosts
      vars_files:
        - vars/example.yml
  14. Role variables are defined in role/vars/main.yml.

    $ cat roles/myrole/vars/main.yml
    foovar: fooval
    barvar: barval
  15. 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"
  16. 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
  17. 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
    
  18. 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
Revision: do457-2.5-4693601