Playbook Variable

Card Puncher Data Processing

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





Discover More
Card Puncher Data Processing
Ansible - Connection

Connection parameters to hosts are given through variable. ... The playbook defines ramon as connection user. At the command line, we set the connection user to lola but the connection...
Card Puncher Data Processing
Ansible - Dict (Map)

value type of a variable The map variable in ansible is based on the python dict. See example:
Card Puncher Data Processing
Ansible - Facts (Remote System Env)

Facts are variable that contains environment information from the remote systems (ie current host (inventory_hostname)) ansible_facts contains any facts gathered or cached for the inventory_hostname...
Card Puncher Data Processing
Ansible - Flow (Conditional)

This page is conditional operators in Ansible. Variable initialization See Loop variable value can be made conditional. Example: variables by type of operating system with include_vars...
Card Puncher Data Processing
Ansible - Function

function in Ansible are tasks that includes other playbook and can pass variable. Include_tasks is done at runtime while import_tasks is done at start time If you have a conditional (such as...
Card Puncher Data Processing
Ansible - Group (Host Properties)

A host can have one or more group (tag). A group may have also have a group. See Groups don’t really survive outside of inventory and host matching because variables are defined to a specific host...
Card Puncher Data Processing
Ansible - Inventory

inventory is a file that define the following entity: the hosts the group of host the child relationship between group and variables (connection variable,...). The preferred practice in Ansible...
Card Puncher Data Processing
Ansible - List Variable

List variable or from two_sources Flatten a list (same thing the flatten lookup does):...
Card Puncher Data Processing
Ansible - Playbook

Playbook is the language of Ansible. Ansible modules are the function Playbooks are declarative instruction written in Yaml that run module functions A playbook is a list of play. Playbooks are:...
Card Puncher Data Processing
Ansible - Role

Roles is the reusable unit of Ansible. You may think of it as a function. It: helps make playbook content self-contained works well with things like git submodules for sharing content with others....



Share this page:
Follow us:
Task Runner