2018-03-14 20:44:21 +01:00
.. _playbooks_variables:
2018-10-30 16:37:34 +01:00
***** ***** *****
Using Variables
***** ***** *****
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
.. contents ::
:local:
2013-12-26 20:32:01 +01:00
2018-05-17 20:07:12 +02:00
While automation exists to make it easier to make things repeatable, all systems are not exactly alike; some may require configuration that is slightly different from others. In some instances, the observed behavior or state of one system might influence how you configure other systems. For example, you might need to find out the IP address of a system and use it as a configuration value on another system.
2013-10-03 04:06:56 +02:00
2018-05-17 20:07:12 +02:00
Ansible uses *variables* to help deal with differences between systems.
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
To understand variables you'll also want to read :ref: `playbooks_conditionals` and :ref: `playbooks_loops` .
2015-09-15 09:56:41 +02:00
Useful things like the **group_by** module
and the `` when `` conditional can also be used with variables, and to help manage differences between systems.
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
The `ansible-examples github repository <https://github.com/ansible/ansible-examples> `_ contains many examples of how variables are used in Ansible.
2015-10-15 15:55:17 +02:00
2013-10-05 00:41:14 +02:00
.. _valid_variable_names:
2018-10-30 16:37:34 +01:00
Creating valid variable names
=============================
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
Before you start using variables, it's important to know what are valid variable names.
2013-10-03 02:22:40 +02:00
Variable names should be letters, numbers, and underscores. Variables should always start with a letter.
2018-05-17 20:07:12 +02:00
`` foo_port `` is a great variable. `` foo5 `` is fine too.
2013-10-03 02:22:40 +02:00
2015-09-15 09:56:41 +02:00
`` foo-port `` , `` foo port `` , `` foo.port `` and `` 12 `` are not valid variable names.
2013-10-03 02:22:40 +02:00
2015-11-01 22:31:05 +01:00
YAML also supports dictionaries which map keys to values. For instance::
foo:
field1: one
field2: two
You can then reference a specific field in the dictionary using either bracket
notation or dot notation::
foo['field1']
foo.field1
These will both reference the same value ("one"). However, if you choose to
use dot notation be aware that some keys can cause problems because they
collide with attributes and methods of python dictionaries. You should use
bracket notation instead of dot notation if you use keys which start and end
with two underscores (Those are reserved for special meanings in python) or
are any of the known public attributes:
2015-10-31 22:26:05 +01:00
`` add `` , `` append `` , `` as_integer_ratio `` , `` bit_length `` , `` capitalize `` , `` center `` , `` clear `` , `` conjugate `` , `` copy `` , `` count `` , `` decode `` , `` denominator `` , `` difference `` , `` difference_update `` , `` discard `` , `` encode `` , `` endswith `` , `` expandtabs `` , `` extend `` , `` find `` , `` format `` , `` fromhex `` , `` fromkeys `` , `` get `` , `` has_key `` , `` hex `` , `` imag `` , `` index `` , `` insert `` , `` intersection `` , `` intersection_update `` , `` isalnum `` , `` isalpha `` , `` isdecimal `` , `` isdigit `` , `` isdisjoint `` , `` is_integer `` , `` islower `` , `` isnumeric `` , `` isspace `` , `` issubset `` , `` issuperset `` , `` istitle `` , `` isupper `` , `` items `` , `` iteritems `` , `` iterkeys `` , `` itervalues `` , `` join `` , `` keys `` , `` ljust `` , `` lower `` , `` lstrip `` , `` numerator `` , `` partition `` , `` pop `` , `` popitem `` , `` real `` , `` remove `` , `` replace `` , `` reverse `` , `` rfind `` , `` rindex `` , `` rjust `` , `` rpartition `` , `` rsplit `` , `` rstrip `` , `` setdefault `` , `` sort `` , `` split `` , `` splitlines `` , `` startswith `` , `` strip `` , `` swapcase `` , `` symmetric_difference `` , `` symmetric_difference_update `` , `` title `` , `` translate `` , `` union `` , `` update `` , `` upper `` , `` values `` , `` viewitems `` , `` viewkeys `` , `` viewvalues `` , `` zfill `` .
2013-10-03 02:22:40 +02:00
2013-10-05 00:41:14 +02:00
.. _variables_in_inventory:
2018-10-30 16:37:34 +01:00
Defining variables in inventory
===============================
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
Often you'll want to set variables for an individual host, or for a group of hosts in your inventory. For instance, machines in Boston
may all use 'boston.ntp.example.com' as an NTP server. The :ref: `intro_inventory` page has details on setting :ref: `host_variables` and :ref: `group_variables` in inventory.
2013-09-29 23:19:35 +02:00
2013-10-05 00:41:14 +02:00
.. _playbook_variables:
2018-10-30 16:37:34 +01:00
Defining variables in a playbook
================================
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
You can define variables directly in a playbook::
2013-09-30 00:44:46 +02:00
- hosts: webservers
vars:
http_port: 80
This can be nice as it's right there when you are reading the playbook.
2013-10-05 00:41:14 +02:00
.. _included_variables:
2018-10-30 16:37:34 +01:00
Defining variables in included files and roles
==============================================
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
As described in :ref: `playbooks_reuse_roles` , variables can also be included in the playbook via include files, which may or may
not be part of an Ansible Role. Usage of roles is preferred as it provides a nice organizational system.
2013-09-29 23:19:35 +02:00
2013-10-05 00:41:14 +02:00
.. _about_jinja2:
2018-10-30 16:37:34 +01:00
Using variables with Jinja2
===========================
2013-09-30 00:44:46 +02:00
2018-10-30 16:37:34 +01:00
Once you've defined variables, you can use them in your playbooks using the Jinja2 templating system. Here's a simple Jinja2 template::
2013-09-30 00:44:46 +02:00
My amp goes to {{ max_amp_value }}
2018-10-30 16:37:34 +01:00
This expression provides the most basic form of variable substitution.
2013-09-30 00:44:46 +02:00
2018-10-30 16:37:34 +01:00
You can use the same syntax in playbooks. For example::
2013-09-30 00:44:46 +02:00
2014-05-17 15:43:23 +02:00
template: src=foo.cfg.j2 dest={{ remote_install_path }}/foo.cfg
2013-09-30 00:44:46 +02:00
2018-10-30 16:37:34 +01:00
Here the variable defines the location of a file, which can vary from one system to another.
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
Inside a template you automatically have access to all variables that are in scope for a host. Actually
2013-10-03 02:22:40 +02:00
it's more than that -- you can also read variables about other hosts. We'll show how to do that in a bit.
2013-09-30 00:44:46 +02:00
.. note :: ansible allows Jinja2 loops and conditionals in templates, but in playbooks, we do not use them. Ansible
2014-03-08 22:53:16 +01:00
playbooks are pure machine-parseable YAML. This is a rather important feature as it means it is possible to code-generate
2013-10-03 02:22:40 +02:00
pieces of files, or to have other ecosystem tools read Ansible files. Not everyone will need this but it can unlock
possibilities.
2017-04-10 20:22:06 +02:00
.. seealso ::
2018-10-30 16:37:34 +01:00
:ref: `playbooks_templating`
2017-04-10 20:22:06 +02:00
More information about Jinja2 templating
2013-10-05 01:04:09 +02:00
.. _jinja2_filters:
2018-10-30 16:37:34 +01:00
Transforming variables with Jinja2 filters
==========================================
2013-10-05 01:04:09 +02:00
2018-10-30 16:37:34 +01:00
Jinja2 filters let you transform the value of a variable within a template expression. For example, the `` capitalize `` filter capitalizes any value passed to it; the `` to_yaml `` and `` to_json `` filters change the format of your variable values. Jinja2 includes many `built-in filters <http://jinja.pocoo.org/docs/templates/#builtin-filters> `_ and Ansible supplies :ref: `many more filters <playbooks_filters>` .
2013-10-05 01:04:09 +02:00
2013-10-05 00:41:14 +02:00
.. _yaml_gotchas:
2018-10-30 16:37:34 +01:00
Hey wait, a YAML gotcha
=======================
2013-10-03 02:22:40 +02:00
2015-09-15 09:56:41 +02:00
YAML syntax requires that if you start a value with `` {{ foo }} `` you quote the whole line, since it wants to be
2018-03-14 20:44:21 +01:00
sure you aren't trying to start a YAML dictionary. This is covered on the :ref: `yaml_syntax` documentation.
2013-10-03 02:22:40 +02:00
This won't work::
- hosts: app_servers
vars:
app_path: {{ base_path }}/22
Do it like this and you'll be fine::
- hosts: app_servers
vars:
app_path: "{{ base_path }}/22"
2013-09-30 00:44:46 +02:00
2013-10-05 00:41:14 +02:00
.. _vars_and_facts:
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
Variables discovered from systems: Facts
========================================
2013-09-29 23:19:35 +02:00
2013-10-03 02:22:40 +02:00
There are other places where variables can come from, but these are a type of variable that are discovered, not set by the user.
2018-09-06 17:26:58 +02:00
Facts are information derived from speaking with your remote systems. You can find a complete set under the `` ansible_facts `` variable,
most facts are also 'injected' as top level variables preserving the `` ansible_ `` prefix, but some are dropped due to conflicts.
2018-10-30 16:37:34 +01:00
This can be disabled via the :ref: `INJECT_FACTS_AS_VARS` setting.
2013-09-30 00:44:46 +02:00
2018-05-17 20:07:12 +02:00
An example of this might be the IP address of the remote host, or what the operating system is.
2013-09-30 00:44:46 +02:00
2018-09-06 17:26:58 +02:00
To see what information is available, try the following in a play::
- debug: var=ansible_facts
To see the 'raw' information as gathered::
2013-09-30 00:44:46 +02:00
ansible hostname -m setup
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
This will return a large amount of variable data, which may look like this on Ansible 2.7:
2017-01-07 20:38:52 +01:00
2018-04-11 20:46:27 +02:00
.. code-block :: json
2013-10-03 02:22:40 +02:00
2017-01-07 20:47:35 +01:00
{
2013-10-03 02:22:40 +02:00
"ansible_all_ipv4_addresses": [
"REDACTED IP ADDRESS"
2017-01-07 20:47:35 +01:00
],
2013-10-03 02:22:40 +02:00
"ansible_all_ipv6_addresses": [
"REDACTED IPV6 ADDRESS"
2017-01-07 20:38:52 +01:00
],
2018-10-30 16:37:34 +01:00
"ansible_apparmor": {
"status": "disabled"
},
2017-01-07 20:38:52 +01:00
"ansible_architecture": "x86_64",
2018-10-30 16:37:34 +01:00
"ansible_bios_date": "11/28/2013",
"ansible_bios_version": "4.1.5",
2013-10-03 02:22:40 +02:00
"ansible_cmdline": {
2018-10-30 16:37:34 +01:00
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-862.14.4.el7.x86_64",
"console": "ttyS0,115200",
"no_timer_check": true,
"nofb": true,
"nomodeset": true,
2017-01-07 20:38:52 +01:00
"ro": true,
2018-10-30 16:37:34 +01:00
"root": "LABEL=cloudimg-rootfs",
"vga": "normal"
2017-01-07 20:38:52 +01:00
},
2013-10-03 02:22:40 +02:00
"ansible_date_time": {
2018-10-30 16:37:34 +01:00
"date": "2018-10-25",
"day": "25",
"epoch": "1540469324",
"hour": "12",
"iso8601": "2018-10-25T12:08:44Z",
"iso8601_basic": "20181025T120844109754",
"iso8601_basic_short": "20181025T120844",
"iso8601_micro": "2018-10-25T12:08:44.109968Z",
"minute": "08",
2017-01-07 20:38:52 +01:00
"month": "10",
2018-10-30 16:37:34 +01:00
"second": "44",
"time": "12:08:44",
"tz": "UTC",
"tz_offset": "+0000",
"weekday": "Thursday",
"weekday_number": "4",
"weeknumber": "43",
"year": "2018"
2017-01-07 20:47:35 +01:00
},
2013-10-03 02:22:40 +02:00
"ansible_default_ipv4": {
2017-01-07 20:38:52 +01:00
"address": "REDACTED",
"alias": "eth0",
2018-10-30 16:37:34 +01:00
"broadcast": "REDACTED",
2017-01-07 20:38:52 +01:00
"gateway": "REDACTED",
"interface": "eth0",
"macaddress": "REDACTED",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "REDACTED",
2013-10-03 02:22:40 +02:00
"type": "ether"
2017-01-07 20:47:35 +01:00
},
2017-01-07 20:38:52 +01:00
"ansible_default_ipv6": {},
2018-10-30 16:37:34 +01:00
"ansible_device_links": {
"ids": {},
"labels": {
"xvda1": [
"cloudimg-rootfs"
],
"xvdd": [
"config-2"
]
},
"masters": {},
"uuids": {
"xvda1": [
"cac81d61-d0f8-4b47-84aa-b48798239164"
],
"xvdd": [
"2018-10-25-12-05-57-00"
]
}
},
2013-10-03 02:22:40 +02:00
"ansible_devices": {
2018-10-30 16:37:34 +01:00
"xvda": {
2017-01-07 20:38:52 +01:00
"holders": [],
2017-01-07 20:47:35 +01:00
"host": "",
2018-10-30 16:37:34 +01:00
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
2017-01-07 20:47:35 +01:00
"model": null,
2018-10-30 16:37:34 +01:00
"partitions": {
"xvda1": {
"holders": [],
"links": {
"ids": [],
"labels": [
"cloudimg-rootfs"
],
"masters": [],
"uuids": [
"cac81d61-d0f8-4b47-84aa-b48798239164"
]
},
"sectors": "83883999",
"sectorsize": 512,
"size": "40.00 GB",
"start": "2048",
"uuid": "cac81d61-d0f8-4b47-84aa-b48798239164"
}
},
"removable": "0",
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
2017-01-07 20:47:35 +01:00
"scheduler_mode": "deadline",
2018-10-30 16:37:34 +01:00
"sectors": "83886080",
2017-01-07 20:47:35 +01:00
"sectorsize": "512",
2018-10-30 16:37:34 +01:00
"size": "40.00 GB",
2017-01-07 20:47:35 +01:00
"support_discard": "0",
2018-10-30 16:37:34 +01:00
"vendor": null,
"virtual": 1
2017-01-07 20:47:35 +01:00
},
2018-10-30 16:37:34 +01:00
"xvdd": {
2017-01-07 20:47:35 +01:00
"holders": [],
2018-10-30 16:37:34 +01:00
"host": "",
"links": {
"ids": [],
"labels": [
"config-2"
],
"masters": [],
"uuids": [
"2018-10-25-12-05-57-00"
]
2017-01-07 20:47:35 +01:00
},
2018-10-30 16:37:34 +01:00
"model": null,
"partitions": {},
2017-01-07 20:47:35 +01:00
"removable": "0",
2018-10-30 16:37:34 +01:00
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
2017-01-07 20:47:35 +01:00
"scheduler_mode": "deadline",
2018-10-30 16:37:34 +01:00
"sectors": "131072",
2017-01-07 20:47:35 +01:00
"sectorsize": "512",
2018-10-30 16:37:34 +01:00
"size": "64.00 MB",
2017-01-07 20:47:35 +01:00
"support_discard": "0",
2018-10-30 16:37:34 +01:00
"vendor": null,
"virtual": 1
2017-01-07 20:47:35 +01:00
},
2018-10-30 16:37:34 +01:00
"xvde": {
2017-01-07 20:47:35 +01:00
"holders": [],
2018-10-30 16:37:34 +01:00
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {
"xvde1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"sectors": "167770112",
"sectorsize": 512,
"size": "80.00 GB",
"start": "2048",
"uuid": null
}
},
"removable": "0",
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
2017-01-07 20:47:35 +01:00
"scheduler_mode": "deadline",
2018-10-30 16:37:34 +01:00
"sectors": "167772160",
2017-01-07 20:47:35 +01:00
"sectorsize": "512",
2018-10-30 16:37:34 +01:00
"size": "80.00 GB",
2017-01-07 20:47:35 +01:00
"support_discard": "0",
2018-10-30 16:37:34 +01:00
"vendor": null,
"virtual": 1
2013-10-03 02:22:40 +02:00
}
2017-01-07 20:47:35 +01:00
},
2018-10-30 16:37:34 +01:00
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.5.1804",
"ansible_dns": {
"nameservers": [
"127.0.0.1"
]
},
2017-01-07 20:47:35 +01:00
"ansible_domain": "",
2018-10-30 16:37:34 +01:00
"ansible_effective_group_id": 1000,
"ansible_effective_user_id": 1000,
2013-10-03 02:22:40 +02:00
"ansible_env": {
2018-10-30 16:37:34 +01:00
"HOME": "/home/zuul",
"LANG": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "zuul",
"MAIL": "/var/mail/zuul",
"PATH": "/usr/local/bin:/usr/bin",
"PWD": "/home/zuul",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
2017-01-07 20:47:35 +01:00
"SHELL": "/bin/bash",
2018-10-30 16:37:34 +01:00
"SHLVL": "2",
"SSH_CLIENT": "23.253.245.60 55672 22",
"SSH_CONNECTION": "23.253.245.60 55672 104.130.127.149 22",
"USER": "zuul",
"XDG_RUNTIME_DIR": "/run/user/1000",
"XDG_SESSION_ID": "1",
"_": "/usr/bin/python2"
2017-01-07 20:47:35 +01:00
},
2013-10-03 02:22:40 +02:00
"ansible_eth0": {
2017-01-07 20:47:35 +01:00
"active": true,
"device": "eth0",
2013-10-03 02:22:40 +02:00
"ipv4": {
2017-01-07 20:47:35 +01:00
"address": "REDACTED",
2018-10-30 16:37:34 +01:00
"broadcast": "REDACTED",
2017-01-07 20:47:35 +01:00
"netmask": "255.255.255.0",
2013-10-03 02:22:40 +02:00
"network": "REDACTED"
2017-01-07 20:47:35 +01:00
},
2013-10-03 02:22:40 +02:00
"ipv6": [
{
2017-01-07 20:47:35 +01:00
"address": "REDACTED",
"prefix": "64",
2013-10-03 02:22:40 +02:00
"scope": "link"
}
2017-01-07 20:47:35 +01:00
],
"macaddress": "REDACTED",
2018-10-30 16:37:34 +01:00
"module": "xen_netfront",
2017-01-07 20:47:35 +01:00
"mtu": 1500,
2018-10-30 16:37:34 +01:00
"pciid": "vif-0",
"promisc": false,
2013-10-03 02:22:40 +02:00
"type": "ether"
2017-01-07 20:47:35 +01:00
},
2018-10-30 16:37:34 +01:00
"ansible_eth1": {
"active": true,
"device": "eth1",
"ipv4": {
"address": "REDACTED",
"broadcast": "REDACTED",
"netmask": "255.255.224.0",
"network": "REDACTED"
},
"ipv6": [
{
"address": "REDACTED",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "REDACTED",
"module": "xen_netfront",
"mtu": 1500,
"pciid": "vif-1",
"promisc": false,
"type": "ether"
},
"ansible_fips": false,
2017-01-07 20:47:35 +01:00
"ansible_form_factor": "Other",
2018-10-30 16:37:34 +01:00
"ansible_fqdn": "centos-7-rax-dfw-0003427354",
"ansible_hostname": "centos-7-rax-dfw-0003427354",
2013-10-03 02:22:40 +02:00
"ansible_interfaces": [
2017-01-07 20:47:35 +01:00
"lo",
2018-10-30 16:37:34 +01:00
"eth1",
2013-10-03 02:22:40 +02:00
"eth0"
2017-01-07 20:47:35 +01:00
],
2018-10-30 16:37:34 +01:00
"ansible_is_chroot": false,
"ansible_kernel": "3.10.0-862.14.4.el7.x86_64",
2013-10-03 02:22:40 +02:00
"ansible_lo": {
2017-01-07 20:47:35 +01:00
"active": true,
"device": "lo",
2013-10-03 02:22:40 +02:00
"ipv4": {
2017-01-07 20:47:35 +01:00
"address": "127.0.0.1",
2018-10-30 16:37:34 +01:00
"broadcast": "host",
2017-01-07 20:47:35 +01:00
"netmask": "255.0.0.0",
2013-10-03 02:22:40 +02:00
"network": "127.0.0.0"
2017-01-07 20:47:35 +01:00
},
2013-10-03 02:22:40 +02:00
"ipv6": [
{
2017-01-07 20:47:35 +01:00
"address": "::1",
"prefix": "128",
2013-10-03 02:22:40 +02:00
"scope": "host"
}
2017-01-07 20:47:35 +01:00
],
2018-10-30 16:37:34 +01:00
"mtu": 65536,
"promisc": false,
2013-10-03 02:22:40 +02:00
"type": "loopback"
2017-01-07 20:47:35 +01:00
},
2018-10-30 16:37:34 +01:00
"ansible_local": {},
2013-10-03 02:22:40 +02:00
"ansible_lsb": {
2018-10-30 16:37:34 +01:00
"codename": "Core",
"description": "CentOS Linux release 7.5.1804 (Core)",
"id": "CentOS",
"major_release": "7",
"release": "7.5.1804"
2017-01-07 20:47:35 +01:00
},
"ansible_machine": "x86_64",
2018-10-30 16:37:34 +01:00
"ansible_machine_id": "2db133253c984c82aef2fafcce6f2bed",
"ansible_memfree_mb": 7709,
"ansible_memory_mb": {
"nocache": {
"free": 7804,
"used": 173
},
"real": {
"free": 7709,
"total": 7977,
"used": 268
},
"swap": {
"cached": 0,
"free": 0,
"total": 0,
"used": 0
}
},
"ansible_memtotal_mb": 7977,
2013-10-03 02:22:40 +02:00
"ansible_mounts": [
{
2018-10-30 16:37:34 +01:00
"block_available": 7220998,
"block_size": 4096,
"block_total": 9817227,
"block_used": 2596229,
"device": "/dev/xvda1",
2017-01-07 20:47:35 +01:00
"fstype": "ext4",
2018-10-30 16:37:34 +01:00
"inode_available": 10052341,
"inode_total": 10419200,
"inode_used": 366859,
2017-01-07 20:47:35 +01:00
"mount": "/",
2018-10-30 16:37:34 +01:00
"options": "rw,seclabel,relatime,data=ordered",
"size_available": 29577207808,
"size_total": 40211361792,
"uuid": "cac81d61-d0f8-4b47-84aa-b48798239164"
},
{
"block_available": 0,
"block_size": 2048,
"block_total": 252,
"block_used": 252,
"device": "/dev/xvdd",
"fstype": "iso9660",
"inode_available": 0,
"inode_total": 0,
"inode_used": 0,
"mount": "/mnt/config",
"options": "ro,relatime,mode=0700",
"size_available": 0,
"size_total": 516096,
"uuid": "2018-10-25-12-05-57-00"
2013-10-03 02:22:40 +02:00
}
2017-01-07 20:47:35 +01:00
],
2018-10-30 16:37:34 +01:00
"ansible_nodename": "centos-7-rax-dfw-0003427354",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
2013-10-03 02:22:40 +02:00
"ansible_processor": [
2018-10-30 16:37:34 +01:00
"0",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"1",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"2",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"3",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"4",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"5",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"6",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz",
"7",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz"
2017-01-07 20:47:35 +01:00
],
2018-10-30 16:37:34 +01:00
"ansible_processor_cores": 8,
"ansible_processor_count": 8,
2017-01-07 20:47:35 +01:00
"ansible_processor_threads_per_core": 1,
2018-10-30 16:37:34 +01:00
"ansible_processor_vcpus": 8,
"ansible_product_name": "HVM domU",
2017-01-07 20:47:35 +01:00
"ansible_product_serial": "REDACTED",
"ansible_product_uuid": "REDACTED",
2018-10-30 16:37:34 +01:00
"ansible_product_version": "4.1.5",
"ansible_python": {
"executable": "/usr/bin/python2",
"has_sslcontext": true,
"type": "CPython",
"version": {
"major": 2,
"micro": 5,
"minor": 7,
"releaselevel": "final",
"serial": 0
},
"version_info": [
2,
7,
5,
"final",
0
]
},
"ansible_python_version": "2.7.5",
"ansible_real_group_id": 1000,
"ansible_real_user_id": 1000,
"ansible_selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 31,
"status": "enabled",
"type": "targeted"
},
"ansible_selinux_python_present": true,
"ansible_service_mgr": "systemd",
2017-01-07 20:47:35 +01:00
"ansible_ssh_host_key_ecdsa_public": "REDACTED KEY VALUE",
2018-10-30 16:37:34 +01:00
"ansible_ssh_host_key_ed25519_public": "REDACTED KEY VALUE",
2017-01-07 20:47:35 +01:00
"ansible_ssh_host_key_rsa_public": "REDACTED KEY VALUE",
2018-10-30 16:37:34 +01:00
"ansible_swapfree_mb": 0,
"ansible_swaptotal_mb": 0,
2017-01-07 20:47:35 +01:00
"ansible_system": "Linux",
2018-10-30 16:37:34 +01:00
"ansible_system_capabilities": [
""
],
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "Xen",
"ansible_uptime_seconds": 151,
"ansible_user_dir": "/home/zuul",
"ansible_user_gecos": "",
"ansible_user_gid": 1000,
"ansible_user_id": "zuul",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 1000,
2017-01-07 20:47:35 +01:00
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
2018-10-30 16:37:34 +01:00
"ansible_virtualization_type": "xen",
"gather_subset": [
"all"
],
"module_setup": true
2017-01-07 20:47:35 +01:00
}
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
In the above the model of the first disk may be referenced in a template or playbook as::
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
{{ ansible_facts['devices']['xvda']['model'] }}
2013-10-03 02:22:40 +02:00
Similarly, the hostname as the system reports it is::
2018-09-06 17:26:58 +02:00
{{ ansible_facts['nodename'] }}
2015-07-25 14:20:07 +02:00
2018-10-30 16:37:34 +01:00
Facts are frequently used in conditionals (see :ref: `playbooks_conditionals` ) and also in templates.
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
Facts can be also used to create dynamic groups of hosts that match particular criteria, see the :ref: `modules` documentation on **group_by** for details, as well as in generalized conditional statements as discussed in the :ref: `playbooks_conditionals` chapter.
2013-09-30 00:44:46 +02:00
2013-10-05 00:41:14 +02:00
.. _disabling_facts:
2018-10-30 16:37:34 +01:00
Disabling facts
---------------
2013-09-30 00:44:46 +02:00
If you know you don't need any fact data about your hosts, and know everything about your systems centrally, you
can turn off fact gathering. This has advantages in scaling Ansible in push mode with very large numbers of
systems, mainly, or if you are using Ansible on experimental platforms. In any play, just do this::
- hosts: whatever
gather_facts: no
2013-10-05 18:41:26 +02:00
.. _local_facts:
2013-10-05 00:41:14 +02:00
2018-10-30 16:37:34 +01:00
Local facts (facts.d)
---------------------
2013-09-30 00:44:46 +02:00
.. versionadded :: 1.3
As discussed in the playbooks chapter, Ansible facts are a way of getting data about remote systems for use in playbook variables.
2014-04-29 21:46:47 +02:00
2018-05-17 20:07:12 +02:00
Usually these are discovered automatically by the `` setup `` module in Ansible. Users can also write custom facts modules, as described in the API guide. However, what if you want to have a simple way to provide system or user provided data for use in Ansible variables, without writing a fact module?
2013-10-05 00:41:14 +02:00
2018-05-02 08:02:32 +02:00
"Facts.d" is one mechanism for users to control some aspect of how their systems are managed.
2013-10-05 00:41:14 +02:00
.. note :: Perhaps "local facts" is a bit of a misnomer, it means "locally supplied user values" as opposed to "centrally supplied user values", or what facts are -- "locally dynamically determined values".
2013-09-30 00:44:46 +02:00
2015-09-15 09:56:41 +02:00
If a remotely managed system has an `` /etc/ansible/facts.d `` directory, any files in this directory
ending in `` .fact `` , can be JSON, INI, or executable files returning JSON, and these can supply local facts in Ansible.
2018-03-22 08:02:45 +01:00
An alternate directory can be specified using the `` fact_path `` play keyword.
2013-09-30 00:44:46 +02:00
2018-05-02 08:02:32 +02:00
For example, assume `` /etc/ansible/facts.d/preferences.fact `` contains::
2013-09-30 00:44:46 +02:00
[general]
asdf=1
bar=2
2015-09-15 09:56:41 +02:00
This will produce a hash variable fact named `` general `` with `` asdf `` and `` bar `` as members.
2013-09-30 00:44:46 +02:00
To validate this, run the following::
ansible <hostname> -m setup -a "filter=ansible_local"
And you will see the following fact added::
"ansible_local": {
"preferences": {
"general": {
2014-11-12 23:55:58 +01:00
"asdf" : "1",
2013-09-30 00:44:46 +02:00
"bar" : "2"
}
}
}
2015-09-15 09:56:41 +02:00
And this data can be accessed in a `` template/playbook `` as::
2013-09-30 00:44:46 +02:00
2018-09-06 17:26:58 +02:00
{{ ansible_local['preferences']['general']['asdf'] }}
2013-09-30 00:44:46 +02:00
2018-05-02 08:02:32 +02:00
The local namespace prevents any user supplied fact from overriding system facts or variables defined elsewhere in the playbook.
2013-09-29 23:19:35 +02:00
2018-09-06 17:26:58 +02:00
.. note :: The key part in the key=value pairs will be converted into lowercase inside the ansible_local variable. Using the example above, if the ini file contained `` XYZ=3 `` in the `` [general] `` section, then you should expect to access it as: `` {{ ansible_local['preferences']['general']['xyz'] }} `` and not `` {{ ansible_local['preferences']['general']['XYZ'] }} `` . This is because Ansible uses Python's `ConfigParser`_ which passes all option names through the `optionxform`_ method and this method's default implementation converts option names to lower case.
2016-05-11 14:32:01 +02:00
.. _ConfigParser: https://docs.python.org/2/library/configparser.html
.. _optionxform: https://docs.python.org/2/library/configparser.html#ConfigParser.RawConfigParser.optionxform
2014-04-29 21:46:47 +02:00
If you have a playbook that is copying over a custom fact and then running it, making an explicit call to re-run the setup module
can allow that fact to be used during that particular play. Otherwise, it will be available in the next play that gathers fact information.
Here is an example of what that might look like::
- hosts: webservers
2014-11-12 23:55:58 +01:00
tasks:
2014-04-29 21:46:47 +02:00
- name: create directory for ansible custom facts
file: state=directory recurse=yes path=/etc/ansible/facts.d
2018-05-08 09:15:56 +02:00
- name: install custom ipmi fact
2014-04-29 21:46:47 +02:00
copy: src=ipmi.fact dest=/etc/ansible/facts.d
- name: re-read facts after adding custom fact
setup: filter=ansible_local
In this pattern however, you could also write a fact module as well, and may wish to consider this as an option.
2016-03-13 09:49:59 +01:00
.. _ansible_version:
Ansible version
2018-10-30 16:37:34 +01:00
---------------
2016-03-13 09:49:59 +01:00
2016-03-17 15:25:37 +01:00
.. versionadded :: 1.8
2016-03-13 09:49:59 +01:00
To adapt playbook behavior to specific version of ansible, a variable ansible_version is available, with the following
structure::
"ansible_version": {
"full": "2.0.0.2",
"major": 2,
"minor": 0,
"revision": 0,
"string": "2.0.0.2"
}
2014-08-11 21:14:58 +02:00
.. _fact_caching:
2018-10-30 16:37:34 +01:00
Caching Facts
-------------
2014-08-11 21:14:58 +02:00
.. versionadded :: 1.8
As shown elsewhere in the docs, it is possible for one server to reference variables about another, like so::
2018-09-06 17:26:58 +02:00
{{ hostvars['asdf.example.com']['ansible_facts']['os_family'] }}
2014-08-11 21:14:58 +02:00
With "Fact Caching" disabled, in order to do this, Ansible must have already talked to 'asdf.example.com' in the
current play, or another play up higher in the playbook. This is the default configuration of ansible.
To avoid this, Ansible 1.8 allows the ability to save facts between playbook runs, but this feature must be manually
enabled. Why might this be useful?
2018-05-02 08:02:32 +02:00
With a very large infrastructure with thousands of hosts, fact caching could be configured to run nightly. Configuration of a small set of servers could run ad-hoc or periodically throughout the day. With fact caching enabled, it would
2014-08-22 12:39:38 +02:00
not be necessary to "hit" all servers to reference variables and information about them.
2014-08-11 21:14:58 +02:00
2018-05-02 08:02:32 +02:00
With fact caching enabled, it is possible for machine in one group to reference variables about machines in the other group, despite the fact that they have not been communicated with in the current execution of /usr/bin/ansible-playbook.
2014-08-11 21:14:58 +02:00
2015-09-15 09:56:41 +02:00
To benefit from cached facts, you will want to change the `` gathering `` setting to `` smart `` or `` explicit `` or set `` gather_facts `` to `` False `` in most plays.
2015-07-10 11:33:31 +02:00
Currently, Ansible ships with two persistent cache plugins: redis and jsonfile.
2015-09-15 09:56:41 +02:00
To configure fact caching using redis, enable it in `` ansible.cfg `` as follows::
2014-08-11 21:14:58 +02:00
[defaults]
2014-12-25 23:33:56 +01:00
gathering = smart
2014-08-11 21:14:58 +02:00
fact_caching = redis
2014-11-26 14:41:39 +01:00
fact_caching_timeout = 86400
# seconds
2014-08-11 21:14:58 +02:00
To get redis up and running, perform the equivalent OS commands::
yum install redis
service redis start
pip install redis
Note that the Python redis library should be installed from pip, the version packaged in EPEL is too old for use by Ansible.
In current embodiments, this feature is in beta-level state and the Redis plugin does not support port or password configuration, this is expected to change in the near future.
2015-09-15 09:56:41 +02:00
To configure fact caching using jsonfile, enable it in `` ansible.cfg `` as follows::
2015-07-10 11:33:31 +02:00
[defaults]
gathering = smart
fact_caching = jsonfile
2015-07-29 21:43:52 +02:00
fact_caching_connection = /path/to/cachedir
2015-07-10 11:33:31 +02:00
fact_caching_timeout = 86400
# seconds
2015-09-15 09:56:41 +02:00
`` fact_caching_connection `` is a local filesystem path to a writeable
2015-07-10 11:33:31 +02:00
directory (ansible will attempt to create the directory if one does not exist).
2015-07-29 21:43:52 +02:00
2015-09-29 17:59:55 +02:00
`` fact_caching_timeout `` is the number of seconds to cache the recorded facts.
2013-10-05 00:41:14 +02:00
.. _registered_variables:
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
Registering variables
=====================
2013-09-29 23:19:35 +02:00
2018-11-29 21:00:36 +01:00
Another major use of variables is running a command and registering the result of that command as a variable. When you execute a task and save the return value in a variable for use in later tasks, you create a registered variable. There are more examples of this in the
2018-10-30 16:37:34 +01:00
:ref: `playbooks_conditionals` chapter.
2013-09-29 23:19:35 +02:00
2018-11-29 21:00:36 +01:00
For example::
2013-10-03 02:22:40 +02:00
- hosts: web_servers
tasks:
- shell: /usr/bin/foo
register: foo_result
ignore_errors: True
- shell: /usr/bin/bar
when: foo_result.rc == 5
2018-11-29 21:00:36 +01:00
Results will vary from module to module. Each module's documentation includes a `` RETURN `` section describing that module's return values. To see the values for a particular task, run your playbook with `` -v `` .
Registered variables are similar to facts, with a few key differences. Like facts, registered variables are host-level variables. However, registered variables are only stored in memory. (Ansible facts are backed by whatever cache plugin you have configured.) Registered variables are only valid on the host for the rest of the current playbook run. Finally, registered variables and facts have different :ref: `precedence levels <ansible_variable_precedence>` .
2013-10-03 02:22:40 +02:00
2018-11-29 21:00:36 +01:00
When you register a variable in a task with a loop, the registered variable contains a value for each item in the loop. The data structure placed in the variable during the loop will contain a `` results `` attribute, that is a list of all responses from the module. For a more in-depth example of how this works, see the :ref: `playbooks_loops` section on using register with a loop.
2016-06-09 19:13:51 +02:00
2015-09-15 15:48:30 +02:00
.. note :: If a task fails or is skipped, the variable still is registered with a failure or skipped status, the only way to avoid registering a variable is using tags.
2013-10-05 00:41:14 +02:00
.. _accessing_complex_variable_data:
2018-10-30 16:37:34 +01:00
Accessing complex variable data
===============================
2013-09-29 23:19:35 +02:00
2018-05-02 08:02:32 +02:00
We already described facts a little higher up in the documentation.
2013-10-03 02:22:40 +02:00
2013-09-29 23:19:35 +02:00
Some provided facts, like networking information, are made available as nested data structures. To access
2015-09-15 09:56:41 +02:00
them a simple `` {{ foo }} `` is not sufficient, but it is still easy to do. Here's how we get an IP address::
2013-09-29 23:19:35 +02:00
2018-09-06 17:26:58 +02:00
{{ ansible_facts["eth0"]["ipv4"]["address"] }}
2013-09-29 23:19:35 +02:00
2013-10-03 02:22:40 +02:00
OR alternatively::
2018-09-06 17:26:58 +02:00
{{ ansible_facts.eth0.ipv4.address }}
2013-10-03 02:22:40 +02:00
2013-09-29 23:19:35 +02:00
Similarly, this is how we access the first element of an array::
{{ foo[0] }}
2013-10-05 00:41:14 +02:00
.. _magic_variables_and_hostvars:
2018-10-30 16:37:34 +01:00
Accessing information about other hosts with magic variables
============================================================
Whether or not you define any variables, you can access information about your hosts with the :ref: `special_variables` Ansible provides, including "magic" variables, facts, and connection variables. Magic variable names are reserved - do not set variables with these names. The variable `` environment `` is also reserved.
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
The most commonly used magic variables are `` hostvars `` , `` groups `` , `` group_names `` , and `` inventory_hostname `` .
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
`` hostvars `` lets you access variables for another host, including facts that have been gathered about that host. You can access host variables at any point in a playbook. Even if you haven't connected to that host yet in any play in the playbook or set of playbooks, you can still get the variables, but you will not be able to see the facts.
2013-09-29 23:19:35 +02:00
If your database server wants to use the value of a 'fact' from another node, or an inventory variable
assigned to another node, it's easy to do so within a template or even an action line::
2018-09-06 17:26:58 +02:00
{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
`` groups `` is a list of all the groups (and hosts) in the inventory. This can be used to enumerate all hosts within a group. For example:
2017-01-07 20:38:52 +01:00
.. code-block :: jinja
2013-09-29 23:19:35 +02:00
{% for host in groups['app_servers'] %}
# something that applies to all app servers.
{% endfor %}
2018-10-30 16:37:34 +01:00
A frequently used idiom is walking a group to find all IP addresses in that group.
2017-01-07 20:38:52 +01:00
.. code-block :: jinja
2013-09-29 23:19:35 +02:00
{% for host in groups['app_servers'] %}
2018-09-06 17:26:58 +02:00
{{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
2013-09-29 23:19:35 +02:00
{% endfor %}
2018-10-30 16:37:34 +01:00
You can use this idiom to point a frontend proxy server to all of the app servers, to set up the correct firewall rules between servers, etc.
2014-10-28 11:18:32 +01:00
You need to make sure that the facts of those hosts have been populated before though, for example by running a play against them if the facts have not been cached recently (fact caching was added in Ansible 1.8).
2013-09-29 23:19:35 +02:00
2018-10-30 16:37:34 +01:00
`` group_names `` is a list (array) of all the groups the current host is in. This can be used in templates using Jinja2 syntax to make template source files that vary based on the group membership (or role) of the host:
.. code-block :: jinja
{% if 'webserver' in group_names %}
# some part of a configuration file that only applies to webservers
{% endif %}
`` inventory_hostname `` is the name of the hostname as configured in Ansible's inventory host file. This can
be useful when you've disabled fact-gathering, or you don't want to rely on the discovered hostname `` ansible_hostname `` . If you have a long FQDN, you can use `` inventory_hostname_short `` , which contains the part up to the first
2013-09-29 23:19:35 +02:00
period, without the rest of the domain.
2018-10-30 16:37:34 +01:00
Other useful magic variables refer to the current play or playbook, including:
2016-09-28 16:32:19 +02:00
.. versionadded :: 2.2
2016-12-22 18:26:28 +01:00
2016-09-23 17:23:25 +02:00
`` ansible_play_hosts `` is the full list of all hosts still active in the current play.
2016-11-02 18:02:44 +01:00
2016-09-28 16:32:19 +02:00
.. versionadded :: 2.2
2016-12-22 18:26:28 +01:00
2016-09-28 16:32:19 +02:00
`` ansible_play_batch `` is available as a list of hostnames that are in scope for the current 'batch' of the play. The batch size is defined by `` serial `` , when not set it is equivalent to the whole play (making it the same as `` ansible_play_hosts `` ).
2016-11-23 22:30:46 +01:00
.. versionadded :: 2.3
2016-12-22 18:26:28 +01:00
2016-11-23 22:30:46 +01:00
`` ansible_playbook_python `` is the path to the python executable used to invoke the Ansible command line tool.
2016-09-28 16:32:19 +02:00
2016-09-23 17:23:25 +02:00
These vars may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer.
2014-02-06 06:01:39 +01:00
2015-09-15 09:56:41 +02:00
Also available, `` inventory_dir `` is the pathname of the directory holding Ansible's inventory host file, `` inventory_file `` is the pathname and the filename pointing to the Ansible's inventory host file.
2013-09-29 23:19:35 +02:00
2016-06-07 15:13:06 +02:00
`` playbook_dir `` contains the playbook base directory.
2016-01-20 19:47:09 +01:00
We then have `` role_path `` which will return the current role's pathname (since 1.8). This will only work inside a role.
And finally, `` ansible_check_mode `` (added in version 2.1), a boolean magic variable which will be set to `` True `` if you run Ansible with `` --check `` .
2015-06-24 17:50:14 +02:00
2014-08-22 12:39:38 +02:00
.. _variable_file_separation_details:
2013-10-05 00:41:14 +02:00
2018-10-30 16:37:34 +01:00
Defining variables in files
===========================
2013-09-29 23:19:35 +02:00
It's a great idea to keep your playbooks under source control, but
you may wish to make the playbook source public while keeping certain
important variables private. Similarly, sometimes you may just
want to keep certain information in different files, away from
the main playbook.
You can do this by using an external variables file, or files, just like this::
---
2014-02-28 20:18:44 +01:00
2013-09-29 23:19:35 +02:00
- hosts: all
remote_user: root
vars:
favcolor: blue
vars_files:
- /vars/external_vars.yml
2014-02-28 20:18:44 +01:00
2013-09-29 23:19:35 +02:00
tasks:
2014-02-28 20:18:44 +01:00
2013-09-29 23:19:35 +02:00
- name: this is just a placeholder
command: /bin/echo foo
This removes the risk of sharing sensitive data with others when
sharing your playbook source with them.
The contents of each variables file is a simple YAML dictionary, like this::
---
# in the above example, this would be vars/external_vars.yml
somevar: somevalue
password: magic
.. note ::
It's also possible to keep per-host and per-group variables in very
2014-12-16 21:25:41 +01:00
similar files, this is covered in :ref: `splitting_out_vars` .
2013-09-29 23:19:35 +02:00
2013-10-05 00:41:14 +02:00
.. _passing_variables_on_the_command_line:
2018-10-30 16:37:34 +01:00
Passing variables on the command line
=====================================
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
In addition to `` vars_prompt `` and `` vars_files `` , it is possible to set variables at the
command line using the `` --extra-vars `` (or `` -e `` ) argument. Variables can be defined using
2018-05-17 20:07:12 +02:00
a single quoted string (containing one or more variables) using one of the formats below
2017-10-16 16:50:38 +02:00
key=value format::
2013-09-29 23:19:35 +02:00
ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"
2017-10-16 16:50:38 +02:00
.. note :: Values passed in using the `` key=value `` syntax are interpreted as strings.
Use the JSON format if you need to pass in anything that shouldn't be a string (Booleans, integers, floats, lists etc).
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
.. versionadded :: 1.2
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
JSON string format::
2014-02-28 20:18:44 +01:00
2017-10-16 16:50:38 +02:00
ansible-playbook release.yml --extra-vars '{"version":"1.23.45","other_variable":"foo"}'
ansible-playbook arcade.yml --extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}'
2014-02-28 20:18:44 +01:00
2017-10-16 16:50:38 +02:00
.. versionadded :: 1.3
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
YAML string format::
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
ansible-playbook release.yml --extra-vars '
version: "1.23.45"
other_variable: foo'
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
ansible-playbook arcade.yml --extra-vars '
pacman: mrs
ghosts:
- inky
- pinky
- clyde
- sue'
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
.. versionadded :: 1.3
2013-09-29 23:19:35 +02:00
2017-10-16 16:50:38 +02:00
vars from a JSON or YAML file::
ansible-playbook release.yml --extra-vars "@some_file.json"
This is useful for, among other things, setting the hosts group or the user for the playbook.
Escaping quotes and other special characters:
2016-02-15 15:06:21 +01:00
2017-10-16 16:50:38 +02:00
.. versionadded :: 1.2
2013-09-29 23:19:35 +02:00
2018-05-17 20:07:12 +02:00
Ensure you're escaping quotes appropriately for both your markup (e.g. JSON), and for
2017-10-16 16:50:38 +02:00
the shell you're operating in.::
ansible-playbook arcade.yml --extra-vars "{\"name\":\"Conan O\'Brien\"}"
ansible-playbook arcade.yml --extra-vars '{"name":"Conan O'\\\''Brien"}'
ansible-playbook script.yml --extra-vars "{\"dialog\":\"He said \\\"I just can\'t get enough of those single and double-quotes"\!"\\\"\"}"
.. versionadded :: 1.3
2013-09-29 23:19:35 +02:00
2018-05-17 20:07:12 +02:00
In these cases, it's probably best to use a JSON or YAML file containing the variable
2017-10-16 16:50:38 +02:00
definitions.
2013-09-29 23:19:35 +02:00
2017-09-23 05:19:50 +02:00
.. _ansible_variable_precedence:
2013-10-05 00:41:14 +02:00
2018-10-30 16:37:34 +01:00
Variable precedence: Where should I put a variable?
===================================================
2013-10-03 02:22:40 +02:00
A lot of folks may ask about how variables override another. Ultimately it's Ansible's philosophy that it's better
2018-03-22 08:02:45 +01:00
you know where to put a variable, and then you have to think about it a lot less.
2013-10-03 02:22:40 +02:00
2015-09-02 22:38:23 +02:00
Avoid defining the variable "x" in 47 places and then ask the question "which x gets used".
2013-10-03 02:22:40 +02:00
Why? Because that's not Ansible's Zen philosophy of doing things.
There is only one Empire State Building. One Mona Lisa, etc. Figure out where to define a variable, and don't make
it complicated.
However, let's go ahead and get precedence out of the way! It exists. It's a real thing, and you might have
a use for it.
2015-09-15 17:45:55 +02:00
If multiple variables of the same name are defined in different places, they get overwritten in a certain order.
2013-10-03 02:22:40 +02:00
2018-03-22 08:02:45 +01:00
Here is the order of precedence from least to greatest (the last listed variables winning prioritization):
2015-09-02 22:38:23 +02:00
2018-08-31 15:56:58 +02:00
* command line values (eg "-u user")
2015-09-02 22:38:23 +02:00
* role defaults [1]_
2017-05-23 23:16:49 +02:00
* inventory file or script group vars [2]_
2017-10-30 17:56:07 +01:00
* inventory group_vars/all [3]_
* playbook group_vars/all [3]_
* inventory group_vars/* [3]_
* playbook group_vars/* [3]_
2017-05-23 23:16:49 +02:00
* inventory file or script host vars [2]_
2017-10-30 17:56:07 +01:00
* inventory host_vars/* [3]_
* playbook host_vars/* [3]_
2018-07-16 17:54:16 +02:00
* host facts / cached set_facts [4]_
2015-09-02 22:38:23 +02:00
* play vars
* play vars_prompt
* play vars_files
2017-03-03 00:07:00 +01:00
* role vars (defined in role/vars/main.yml)
2015-09-02 22:38:23 +02:00
* block vars (only for tasks in block)
* task vars (only for the task)
2017-03-03 00:07:00 +01:00
* include_vars
* set_facts / registered vars
2018-04-05 20:09:12 +02:00
* role (and include_role) params
* include params
2016-02-19 16:32:34 +01:00
* extra vars (always win precedence)
2015-09-02 22:38:23 +02:00
2015-10-22 15:12:43 +02:00
Basically, anything that goes into "role defaults" (the defaults folder inside the role) is the most malleable and easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in namespace. The idea here to follow is that the more explicit you get in scope, the more precedence it takes with command line `` -e `` extra vars always winning. Host and/or inventory variables can win over role defaults, but not explicit includes like the vars directory or an `` include_vars `` task.
2015-09-02 22:38:23 +02:00
.. rubric :: Footnotes
2015-12-05 03:06:58 +01:00
.. [1] Tasks in each role will see their own role's defaults. Tasks defined outside of a role will see the last role's defaults.
.. [2] Variables defined in inventory file or provided by dynamic inventory.
2017-10-30 17:56:07 +01:00
.. [3] Includes vars added by 'vars plugins' as well as host_vars and group_vars which are added by the default vars plugin shipped with Ansible.
.. [4] When created with set_facts's cacheable option, variables will have the high precedence in the play,
2017-11-01 16:42:17 +01:00
but will be the same as a host facts precedence when they come from the cache.
2015-09-02 22:38:23 +02:00
2015-12-13 07:24:27 +01:00
.. note :: Within any section, redefining a var will overwrite the previous instance.
2015-09-02 22:38:23 +02:00
If multiple groups have the same variable, the last one loaded wins.
2018-05-17 20:07:12 +02:00
If you define a variable twice in a play's `` vars: `` section, the second one wins.
.. note :: The previous describes the default config `` hash_behaviour=replace `` , switch to `` merge `` to only partially overwrite.
2018-08-22 23:05:42 +02:00
.. note :: Group loading follows parent/child relationships. Groups of the same 'parent/child' level are then merged following alphabetical order.
2018-06-06 02:20:21 +02:00
This last one can be superceeded by the user via `` ansible_group_priority `` , which defaults to `` 1 `` for all groups.
2018-08-31 15:20:26 +02:00
This variable, `` ansible_group_priority `` , can only be set in the inventory source and not in group_vars/ as the variable is used in the loading of group_vars/.
2013-10-03 02:22:40 +02:00
2018-09-04 16:33:01 +02:00
Another important thing to consider (for all versions) is that connection variables override config, command line and play/role/task specific options and keywords. For example, if your inventory specifies `` ansible_ssh_user: ramon `` and you run::
2013-10-03 02:22:40 +02:00
2017-02-23 21:37:29 +01:00
ansible -u lola myhost
2018-08-31 15:56:58 +02:00
This will still connect as `` ramon `` because the value from the variable takes priority (in this case, the variable came from the inventory, but the same would be true no matter where the variable was defined).
For plays/tasks this is also true for `` remote_user `` . Assuming the same inventory config, the following play::
2017-02-23 21:37:29 +01:00
- hosts: myhost
tasks:
2018-09-04 16:33:01 +02:00
- command: I'll connect as ramon still
2017-02-23 21:37:29 +01:00
remote_user: lola
2015-09-02 22:38:23 +02:00
2018-09-04 16:33:01 +02:00
will have the value of `` remote_user `` overwritten by `` ansible_ssh_user `` in the inventory.
2018-08-31 15:56:58 +02:00
2017-03-01 04:03:20 +01:00
This is done so host-specific settings can override the general settings. These variables are normally defined per host or group in inventory,
2018-08-31 15:56:58 +02:00
but they behave like other variables.
If you want to override the remote user globally (even over inventory) you can use extra vars. For instance, if you run::
ansible... -e "ansible_user=maria" -u lola
2015-09-02 22:38:23 +02:00
2018-09-04 16:33:01 +02:00
the `` lola `` value is still ignored, but `` ansible_user=maria `` takes precedence over all other places where `` ansible_user `` (or `` ansible_ssh_user `` , or `` remote_user `` ) might be set.
2017-02-23 21:37:29 +01:00
You can also override as a normal variable in a play::
2015-09-02 22:38:23 +02:00
2017-02-23 21:37:29 +01:00
- hosts: all
vars:
ansible_user: lola
tasks:
2018-09-04 16:33:01 +02:00
- command: I'll connect as lola!
2015-09-02 22:38:23 +02:00
.. _variable_scopes:
2018-10-30 16:37:34 +01:00
Scoping variables
-----------------
2015-09-02 22:38:23 +02:00
2018-10-30 16:37:34 +01:00
You can decide where to set a variable based on the scope you want that value to have. Ansible has three main scopes:
2015-09-02 22:38:23 +02:00
* Global: this is set by config, environment variables and the command line
2017-04-24 18:14:47 +02:00
* Play: each play and contained structures, vars entries (vars; vars_files; vars_prompt), role defaults and vars.
* Host: variables directly associated to a host, like inventory, include_vars, facts or registered task outputs
2015-09-02 22:38:23 +02:00
.. _variable_examples:
2018-10-30 16:37:34 +01:00
Examples of where to set a variable
-----------------------------------
2014-04-01 16:48:14 +02:00
2017-11-22 05:14:27 +01:00
Let's show some examples and where you would choose to put what based on the kind of control you might want over values.
2013-10-03 02:22:40 +02:00
2017-11-22 05:14:27 +01:00
First off, group variables are powerful.
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
Site-wide defaults should be defined as a `` group_vars/all `` setting. Group variables are generally placed alongside
your inventory file. They can also be returned by a dynamic inventory script (see :ref: `intro_dynamic_inventory` ) or defined
2018-03-14 20:44:21 +01:00
in things like :ref: `ansible_tower` from the UI or API::
2013-10-03 02:22:40 +02:00
---
# file: /etc/ansible/group_vars/all
# this is the site wide default
ntp_server: default-time.example.com
2015-09-15 09:56:41 +02:00
Regional information might be defined in a `` group_vars/region `` variable. If this group is a child of the `` all `` group (which it is, because all groups are), it will override the group that is higher up and more general::
2013-10-03 02:22:40 +02:00
---
# file: /etc/ansible/group_vars/boston
2015-09-02 22:38:23 +02:00
ntp_server: boston-time.example.com
2013-10-03 02:22:40 +02:00
If for some crazy reason we wanted to tell just a specific host to use a specific NTP server, it would then override the group variable!::
---
# file: /etc/ansible/host_vars/xyz.boston.example.com
2014-01-03 19:18:47 +01:00
ntp_server: override.example.com
2013-10-03 02:22:40 +02:00
So that covers inventory and what you would normally set there. It's a great place for things that deal with geography or behavior. Since groups are frequently the entity that maps roles onto hosts, it is sometimes a shortcut to set variables on the group instead of defining them on a role. You could go either way.
Remember: Child groups override parent groups, and hosts always override their groups.
Next up: learning about role variable precedence.
We'll pretty much assume you are using roles at this point. You should be using roles for sure. Roles are great. You are using
2018-05-17 20:07:12 +02:00
roles aren't you? Hint hint.
2013-10-03 02:22:40 +02:00
2017-11-22 05:14:27 +01:00
If you are writing a redistributable role with reasonable defaults, put those in the `` roles/x/defaults/main.yml `` file. This means
2018-05-17 20:07:12 +02:00
the role will bring along a default value but ANYTHING in Ansible will override it.
2018-10-30 16:37:34 +01:00
See :ref: `playbooks_reuse_roles` for more info about this::
2013-10-03 02:22:40 +02:00
2013-11-04 14:16:33 +01:00
---
2013-10-03 02:22:40 +02:00
# file: roles/x/defaults/main.yml
2014-03-31 01:45:55 +02:00
# if not overridden in inventory or as a parameter, this is the value that will be used
2013-10-03 02:22:40 +02:00
http_port: 80
2015-09-15 09:56:41 +02:00
If you are writing a role and want to ensure the value in the role is absolutely used in that role, and is not going to be overridden
by inventory, you should put it in `` roles/x/vars/main.yml `` like so, and inventory values cannot override it. `` -e `` however, still will::
2013-10-03 02:22:40 +02:00
2013-11-04 14:16:33 +01:00
---
2013-10-03 02:22:40 +02:00
# file: roles/x/vars/main.yml
# this will absolutely be used in this role
http_port: 80
2017-11-22 05:14:27 +01:00
This is one way to plug in constants about the role that are always true. If you are not sharing your role with others,
2013-10-03 02:22:40 +02:00
app specific behaviors like ports is fine to put in here. But if you are sharing roles with others, putting variables in here might
be bad. Nobody will be able to override them with inventory, but they still can by passing a parameter to the role.
Parameterized roles are useful.
If you are using a role and want to override a default, pass it as a parameter to the role like so::
roles:
2018-04-05 20:41:26 +02:00
- role: apache
vars:
http_port: 8080
2013-10-03 02:22:40 +02:00
2013-10-06 03:19:19 +02:00
This makes it clear to the playbook reader that you've made a conscious choice to override some default in the role, or pass in some
2013-10-03 02:22:40 +02:00
configuration that the role can't assume by itself. It also allows you to pass something site-specific that isn't really part of the
role you are sharing with others.
2017-11-22 05:14:27 +01:00
This can often be used for things that might apply to some hosts multiple times. For example::
2013-10-03 02:22:40 +02:00
roles:
2018-04-05 20:41:26 +02:00
- role: app_user
vars:
myname: Ian
- role: app_user
vars:
myname: Terry
- role: app_user
vars:
myname: Graham
- role: app_user
vars:
myname: John
2013-10-03 02:22:40 +02:00
2017-11-22 05:14:27 +01:00
In this example, the same role was invoked multiple times. It's quite likely there was
2018-05-17 20:07:12 +02:00
no default for `` name `` supplied at all. Ansible can warn you when variables aren't defined -- it's the default behavior in fact.
2013-10-03 02:22:40 +02:00
2017-11-22 05:14:27 +01:00
There are a few other things that go on with roles.
2013-10-03 02:22:40 +02:00
2015-09-15 09:56:41 +02:00
Generally speaking, variables set in one role are available to others. This means if you have a `` roles/common/vars/main.yml `` you
2013-10-03 02:22:40 +02:00
can set variables in there and make use of them in other roles and elsewhere in your playbook::
roles:
2018-04-05 20:41:26 +02:00
- role: common_settings
- role: something
vars:
foo: 12
- role: something_else
2013-10-03 02:22:40 +02:00
2017-09-07 05:36:01 +02:00
.. note :: There are some protections in place to avoid the need to namespace variables.
2014-07-18 20:29:25 +02:00
In the above, variables defined in common_settings are most definitely available to 'something' and 'something_else' tasks, but if
2013-10-03 02:22:40 +02:00
"something's" guaranteed to have foo set at 12, even if somewhere deep in common settings it set foo to 20.
So, that's precedence, explained in a more direct way. Don't worry about precedence, just think about if your role is defining a
variable that is a default, or a "live" variable you definitely want to use. Inventory lies in precedence right in the middle, and
2015-09-15 09:56:41 +02:00
if you want to forcibly override something, use `` -e `` .
2013-10-03 02:22:40 +02:00
2018-10-30 16:37:34 +01:00
If you found that a little hard to understand, take a look at the `ansible-examples <https://github.com/ansible/ansible-examples> `_ repo on GitHub for a bit more about how all of these things can work together.
2014-02-17 12:05:53 +01:00
2018-10-30 16:37:34 +01:00
Using advanced variable syntax
==============================
2016-02-04 18:10:30 +01:00
2018-10-30 16:37:34 +01:00
For information about advanced YAML syntax used to declare variables and have more control over the data placed in YAML files used by Ansible, see :ref: `playbooks_advanced_syntax` .
2016-02-04 18:10:30 +01:00
2013-10-05 18:31:16 +02:00
.. seealso ::
2018-10-30 16:37:34 +01:00
:ref: `about_playbooks`
2013-10-05 18:31:16 +02:00
An introduction to playbooks
2018-10-30 16:37:34 +01:00
:ref: `playbooks_conditionals`
2013-10-05 18:31:16 +02:00
Conditional statements in playbooks
2018-10-30 16:37:34 +01:00
:ref: `playbooks_filters`
2015-02-21 13:01:22 +01:00
Jinja2 filters and their uses
2018-10-30 16:37:34 +01:00
:ref: `playbooks_loops`
2013-10-05 18:31:16 +02:00
Looping in playbooks
2018-10-30 16:37:34 +01:00
:ref: `playbooks_reuse_roles`
2013-10-05 18:31:16 +02:00
Playbook organization by roles
2018-10-30 16:37:34 +01:00
:ref: `playbooks_best_practices`
2013-10-05 18:31:16 +02:00
Best practices in playbooks
2018-08-27 21:57:32 +02:00
:ref: `special_variables`
List of special variables
`User Mailing List <http://groups.google.com/group/ansible-devel> `_
2013-10-05 18:31:16 +02:00
Have a question? Stop by the google group!
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel