Variables can contain letters, numbers and underscore, but must begin with a letter
Can be stored as dictionaries which map keys to values. Dictionary variables can be refenced using bracket notation or dot notation, ex: dictionary_name [‘filed’] or dictionary.field
Variables stored as a list (array) may be accessed by putting the element number in brackets: Ex: {{array_name[0]}}
Variables may be defined or set in the following locations:
- Inventories (also in host_vars and group_vars directories)
- Playbooks (ie. vars, vars_files and vars_prompt)
- Roles (roled/role_name/vars/main.yml)
- The command line (-e or –extra-vars)
Referenced defined variables using the jinja2 templating system (ex: This is my {{ variable }}. YAML requires values starting with a variable to be quoted. Transform variable values using jinja2 filters (join, capabilities etc)
Ansible stores information about remote hosts in variables known as Ansible facts
Ansible provides special, reserved variables known as magic variables. Ex hostvars_groups, group_names and inventory_hostname
Custom facts (AKA local facts) can be added to the remote system by the user
- Defined in files end with .fact
- Local fact files are stored in /etc/ansible/facts.d
- Fact file directory can be changed using the fact_path keyword
- Local facts can be viewed by running the following:
- ansible hostname -m setup -a “filter=ansible_local”
Ansible Templates
- Templates are files that contain both static and dynamic values through the use of variables
- Ansible Processes templates using jinja2, templates are designed with the .j2 extension.
- Templates are often used for configuration file management.
- Templates have access to the same variables as the plays that call them.
- The template module processes a template and push it out to a remote server.