Ansible - Var
Var
Define Var in Playbook
Define Var in File
file:
|
|
usage
Var Trap
sometimes YAML and ansible playbook cannot work together. when it report syntex error, you could try add quote.
Facts
Facts are the information of remote hosts collected by ‘setup’ module.
we can directly use the vars in ansible all -m setup -u root
like {{some_vars}}
the vars we collected will be in a json format, we could use .
to access different level of json like {{ansible_ens3.ipv4.address}}
Register Vars
we can register var in the playbook and use it later.
Pass Vars Through CMD
we could use
|
|
to pass vars to playbook.yml
we could use vars in playbook just like other type of vars
Var Scope
- global
- vars in the ansible.cfg
- env vars
- from cli
- play
var
keywords in playbookinclude_vars
keyworddefault/main.yml
orvars/main.yml
in role
- host
- vars defined in the host inventory list
- remote host system var
- registered vars
Var Precedence
ekreutz/ansible_variable_precedence.md
from the least to the most important
- 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
- 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)
- role (and include_role) params
- include params
- include_vars
- set_facts / registered vars
- extra vars (always win precedence)