Ansible - Variables
Updated at 2017-03-21 23:46
You can pass variables in command line.
ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"
You can define variables in the playbook. And keep a separate (git ignored) file for some variables.
---
- hosts: all
remote_user: root
vars:
favcolor: blue
vars_files:
- /secret_vars.yml
tasks:
- name: this is just a placeholder
command: /bin/echo foo
---
# secret_vars.yml
password: magic
foo:
field1: one
field2: two
# dicts can be accessed with foo.field1
You can pass variables to include
s and role
s.
tasks:
- include: wordpress.yml wp_user=timmy
roles:
- { role: foo_app_instance, dir: '/opt/a', app_port: 5000 }
You can define a variable from task output with register
. If a task fails or is skipped, the variable still is registered with a failure or skipped status.
- hosts: web_servers
tasks:
- shell: /usr/bin/foo
register: foo_result
ignore_errors: True
- shell: /usr/bin/bar
when: foo_result.rc == 5
You can define variables with dynamic inventory. These can be group variables or host specific variables.
You can prompt the user for variables.
---
- hosts: all
remote_user: root
vars_prompt:
- name: "name"
prompt: "what is your name?"
- name: "secret"
prompt: "what is your secret?"
private: yes ; won't be shown in output
; there is also `encrypt` option
- name: "favcolor"
prompt: "what is your favorite color?"
default: "red"
setup
module allows assigning "facts" to variables. After running setup:
task, the variables are available in the scope of that host.
ansible all -m setup
...
"ansible_lsb": {
"codename": "xenial",
"description": "Ubuntu 16.04.1 LTS",
"id": "Ubuntu",
"major_release": "16",
"release": "16.04"
},
...