This article is about variable inside a playbook, task, …
logrotate__enabled: True
logrotate__default_options: |
create
{{ logrotate__default_period }}
rotate {{ logrotate__default_rotation }}
tabooext + .dpkg-divert
include /etc/logrotate.d
from least to greatest (
command line option have the least importance !!
The order/precedence is (from lowest to highest) on an alphabetical order:
The merge order for group is done:
Example:
a_group:
testvar: a
ansible_group_priority: 10
b_group:
testvar: b
- hosts: webservers
vars:
http_port: 80
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
# 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
{{ lookup('env', 'MY_USER') | default('admin', true) }}
All ansible_... variable
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'
- name: Define java_packages.
set_fact:
java_packages: "{{ __java_packages | list }}"
when: java_packages is not defined
You can register the output of a shell and use it as a variable.
See Ansible - Shell
{{ variable | mandatory }}
{{ some_variable | default(5) }}
{{ some_variable | default('default_value') }}
when: foo is defined
when: bar is undefined
By default, Ansible overwrites variables including the ones defined for a group and/or host.
see the hash_merge setting.
You can print the variable with the debug task
Example:
- 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"
}
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
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html