Playbook Variable

> Infrastructure as code > Ansible

1 - About

Variable inside a Ansible - Playbook

Advertising

3 - 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

4 - Management

4.1 - 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
Advertising

4.2 - Init

4.2.1 - Hard coded

  • In the playbook as an atomic variable
- 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:

external_vars.yml
---
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
Advertising

4.2.2 - Env variable

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

4.2.3 - 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.

4.2.4 - 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'

4.2.5 - set_facts

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

4.3 - Properties

4.3.1 - Mandatory

{{ variable | mandatory }}

4.3.2 - default value

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

4.4 - Scope

4.5 - Configuration

4.5.1 - hash_merge

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

see the hash_merge setting.

4.6 - Print

You can print the variable with the debug task

Example:

playbook.yml
- debug: msg="{{ streaming_jar_path_result.files[0].path }}"

5 - Support

5.1 - 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

6 - Documentation / Reference