Ansible: variables - referenced Jeff Geerling’s book

Frances (Jing) Du
3 min readJun 23, 2023

--

The book is Ansible for DevOps — 2nd Edition by Jeff Geerling.

Playbook Variables

Variables can be passed in via the command line, when calling ansible-playbook, with the ‘— — extra-vars’ option.

ansible-playbook example.yml --extra-vars "foo=bar"

Variables may be included inline with the rest of a playbook, in a ‘vars’ section:

---
- hosts: example

vars:
foo: bar
tasks:
# Prints "Variable 'foo' is set to bar".
- debug:
msg: "Variable 'foo' is set to {{ foo }}"

Variables may also be included in a separate file, using the ‘vars_files’ section:

---
# Main playbook file.
- hosts: example

vars_files:
- vars.yml

tasks:
- debug:
msg: "Variable 'foo' is set to {{ foo }}"
---
# Variable file 'vars.yml' in the same folder as the playbook.
foo: bar

Variable files can also be imported conditionally. Say, for instance, you have one set of variables for your RHEL servers (where the Apache service is named httpd), and another for your Debian servers (where the Apache service is named apache2). In this case, you can conditionally include variables files using ‘include_vars’:

---
- hosts: example

pre_tasks:
- include_vars: "{{ item }}"
with_first_found:
- "apache_{{ ansible_os_family }}.yml"
- "apache_default.yml"
tasks:
- name: Ensure Apache is running.
service:
name: "{{ apache_service_name }}"
state: running

with_first_found: allows you to run a task against the first file found from several search paths.

Then, add two files in the same folder as your example playbook, apache_RedHat.yml, and apache_default.yml. Define the variable apache_service_name: httpd in the CenOS (RedHat) file, and apache_service_name: apache2 in the default files.

As long as you don’t disable gather_facts (or if you run a setup task at some point to gather facts manually), Ansible store the OS of the server in the variable ansible_os_family, and will include the vars file with the resulting name. If ansible can’t find a file with that name, it will use the variables loaded from apache_default.yml. So, on a Debian or Ubuntu server, Ansible would correctly use apache2 as the service name, even though there is no apache_Debian.yml or apache_Ubuntu.yml file available.

Inventory variables

Variables may also be added via Ansible inventory files, either inline with a host definition, or after a group:

# Host-specific variables (defined inline).

[washington]

app1.example.com proxy_state=present

app2.example.com proxy_state=absent

# Variables defined for the entire group.

[washington: vars]

cdn_host=washington.static.example.com

api_version=3.0.1

However, Ansible’s documentation recommends not storing variables within the inventory. Instead, you can use group_vars and host_vars YAML variable files within a specific path, and Ansible will assign them to individual hosts and groups defined in your inventory.

For example, to apply a set of variables to the host app1.example.com, create a blank file named app1.example.com at the location /etc/ansible/host_vars/app1.example.com, and add variables as you would in an included vars_files YAML file:

---
foo: bar
baz: qux

To apply a set of variables to the entire washington group, create a similar file in the location /etc/ansible/groups_vars/washington (substitute washington for whatever group name’s variables you’re defining).

You can also put these files (named the same way) in host_vars or group_vars directories in your playbook’s directory. Ansible will use the variables defined in the inventory /etc/ansible/[host|group]_vars directory first (if the appropriate files exist), then it will use variables defined in the playbook directories.

https://docs.ansible.com/ansible/latest/tips_tricks/sample_setup.html

Host and Group Variables

--

--

No responses yet