Table of Contents

About

This article is about variable inside a playbook, task, …

Type

logrotate__enabled: True
  • Block of text
logrotate__default_options: |
  create
  {{ logrotate__default_period }}
  rotate {{ logrotate__default_rotation }}
  tabooext + .dpkg-divert
  include /etc/logrotate.d

Management

Order of precedence

Order of precedence doc

from least to greatest (

command line option have the least importance !!

  • command line values (eg “-u user”)
  • role defaults
  • inventory file or script group vars
  • inventory group_vars/all
  • playbook group_vars/all
  • inventory group_vars/*
  • playbook group_vars/*
  • inventory file or script host vars
  • inventory host_vars/*
  • playbook host_vars/*
  • host facts / cached set_facts
  • play vars
  • play vars_prompt
  • play vars_files
  • role vars (defined in role/vars/main.yml)
  • block vars (only for tasks in block)
  • task vars (only for the task)
  • include_vars
  • set_facts / registered vars
  • role (and include_role) params
  • include params
  • extra vars (always win precedence)

The order/precedence is (from lowest to highest) on an alphabetical order:

The merge order for group is done:

  • on an alphabetical order (by default)
  • of by setting the ansible_group_priority

Example:

  • if both groups had the same priority, the result would normally have been testvar == b, but since the a_group has a higher priority, the result is testvar == a
a_group:
    testvar: a
    ansible_group_priority: 10
b_group:
    testvar: b

Init

Hard coded

  • In the playbook or a task as an atomic variable with the vars keyword.
- hosts: webservers
  vars:
    http_port: 80
  • In the playbook as an Array
vars:
    apache_group: 
      Debian: "root" 
      RedHat: "apache"

Accessed where ansible_os_family is a Ansible - Facts (Remote System Env)

group: "{{ apache_group[ansible_os_family] }}"
---
- hosts: all
  remote_user: root
  vars_files:
    - /vars/external_vars.yml

where vars/external_vars.yml format is just a YAML dictionary:

---
somevar: somevalue
password: magic
  • At the cli command line with the --extra-vars options
# Key value format
ansible-playbook release.yml --extra-vars "key1=value1 key2=value1 ..."
# Example
ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"
# Json format
ansible-playbook release.yml --extra-vars '{"version":"1.23.45","other_variable":"foo"}'
ansible-playbook release.yml --extra-vars "@some_file.json"
ansible-playbook arcade.yml --extra-vars "{\"name\":\"Conan O\'Brien\"}" # With escaping

Env variable

env variable

{{ lookup('env', 'MY_USER') | default('admin', true) }}

Built-in

All ansible_... variable

  • Facts are information derived from speaking with your remote systems. You can find a complete set under the ansible_facts variable.

include_vars

In a role where you have a vars directory with include_vars

- name: Include version-specific variables for CentOS/RHEL.
  include_vars: "RedHat-{{ ansible_distribution_version.split('.')[0] }}.yml"
  when: ansible_distribution == 'CentOS' or
        ansible_distribution == 'Red Hat Enterprise Linux' or
        ansible_distribution == 'RedHat'

set_facts

- name: Define java_packages.
  set_fact:
    java_packages: "{{ __java_packages | list }}"
  when: java_packages is not defined

from shell

You can register the output of a shell and use it as a variable.

See Ansible - Shell

Properties

Mandatory

{{ variable | mandatory }}

default value

  • numeric
{{ some_variable | default(5) }}
  • String
{{ some_variable | default('default_value') }}

defined/undefined

when: foo is defined
when: bar is undefined

Scope

Configuration

hash_merge

By default, Ansible overwrites variables including the ones defined for a group and/or host.

see the hash_merge setting.

Print

You can print the variable with the debug task

Example:

  • in a playbook
- debug: msg="{{ streaming_jar_path_result.files[0].path }}"
ansible -i inventory.yml -m debug -a var=ansible_host all
vps | SUCCESS => {
    "ansible_host": "server01.example.com"
}

Support

template error while templating string: unexpected char u

fatal: [primary_gateway_dev131]: FAILED! => {"msg": "template error while templating string: unexpected char u's' at 7. String: {{ 1022sp1_file_base }}"}

variable should not start with a number

Documentation / Reference

* https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html