Ansible Variables
Ansible Variables
Balasubramanian M
Sakthimurugan A
Goal: Write playbooks that use variables,
sensitive variables (password and etc.) and facts
to simplify management of the playbook and facts
to reference information about the managed
Managing hosts
Variables Objectives:
• Create and reference variables
• Encrypt sensitive variables
• Using Facts and configuring Custom Facts
Why
variables? Reduces manual error in the cases of repeat
usage
Users to create
Packages to install
Example
Services to restart
such as: Files to remove
Archives to retrieve from the internet
user: joe
home: /home/joe
Relative path is
good enough
Example:
vars:
user: joe
tasks: # This line will read: Creates the user joe
- name: Creates the user {{ user }}
user: # This line will create the user named Joe
name: "{{ user }}"
Note:
• Host variable takes precedence than group variable
• Playbook variable takes precedence than these two
• Command-line variable takes precedence than these three
Ansible Variables - DevSecOps Quick Bytes 11
Defining Variables in Inventory file
Defining the “user” as group variable
Defining the “user” as host variable in inventory file for multiple groups of hosts
Note:
• Contents of that file would set variables to values using the same syntax as in a playbook
• <group> is the group / nested groups defined in inventory file
• <host-name> is the hosts listed in inventory file
Ansible Variables - DevSecOps Quick Bytes 13
Using group_vars and host_vars Directories
Note: Command line variables also known as extra variables in Ansible takes highest precedence of all
Using ”users” variable Alternate & Recommended way of using ”users” variable
Note: Variables in Ansible are defined as Python dictionary. At times, The dot notation can cause problems if the key
names are the same as names of Python methods or attributes, such as discard, copy, add, and so on. Using the
brackets notation can help avoid conflicts and errors.
Ansible Variables - DevSecOps Quick Bytes 16
Registered Variables
19
How it works?
• Using python-cryptography package
• PyCrypto command is used for cryptographic
operations
30
Ansible defined variables of other hosts that
might be of use in your play for the “current
control node”
• hostvars
{{ hostvars['test.example.com']['ansible_distribution'] }}
• group_names
Magic {% if 'webserver' in group_names %}
# action
Variables
{% endif %}
• groups
{% for host in groups['app_servers'] %}
# something that applies to all app servers.
{% endfor %}
Example:
Vars_prompt
in playbook
Exercise
Use Vars_prompt and use it in your play
host_vars/*
role defaults script group
inventory
group_vars/all group_vars/all group_vars/* script host vars
vars
role vars
block vars (only
role/vars/main.y
ml)
role (and
task vars (only set_facts /
include_role) include params include_vars extra vars
for the task) registered vars
params
Note: Within any section, redefining a var will overwrite the previous instance. If multiple groups have the same variable, the
last one loaded wins. If you define a variable twice in a play’s vars: section, the 2nd one wins.
Best Practices
Keep your sensitive variables in vault