defaults/main.ymlWe'll talk about roles in part 3, but roles can set default values for variables, and these are available to the rest of the playbook.
host_vars and group_vars stored in the inventory directorypip install ansible-inventory-grapher) along with graphviz to help visualize inventory hierarchies:ansible-inventory-grapher -q target | \
dot -Tpng | display png:-
Run
ansible-playbook playbooks/simple/add-inventory-graph.yml

hosts_file in the ansible configuration file to a directoryhost_vars and group_varshost_varsHost variables should be used only for things that will only be true for a single host. An example of this might be caching of a UUID of a host, or setting kerberos keytabs
This means that SSL certificates and keys, kerberos keytabs, server uuids etc. might be candidates, but most other inventory variables will be properties of groups.
[group:vars] or [host:vars] mechanism) — the inventory files should be used for group contents and hierarchy definitions (using [group:children]).group_vars instead, or host_vars at a push.In general playbooks shouldn't need to define vars, but the capability exists.
vars_prompt is useful if you need to provide a variable at run time — e.g. a password for a service and don't want to source it from a vaulted file.
vars_prompt example- hosts: certificate_authority
vars_prompt:
- name: ca_password
prompt: "Please enter your CA password"
tasks:
- name: sign certificate
command: openssl ca -in req.pem \
-out newcert.pem -passin env:CA_PASSWD
environment:
CA_PASSWD: "{{ ca_password }}"
registered variablesregistered variables used to store the results of a task in a playbook. - name: get stat data for file
stat:
path: /path/to/file
register: stat_file
- name: fail if path doesn't exist
fail:
msg: "File does not exist"
when: not stat_file.stat.exists
Information about a host sourced at runtime, e.g. IP address or OS version.
You don't need to run the setup module directly to gather facts — it is always run in playbook mode, unless gather_facts is set to False
If you ran the previous lab, you should be able to see the facts for target at http://192.168.33.11:8000/
set_fact moduleset_fact module is used to derive new facts from existing facts to produce more useful ones. - name: set timezone fact
set_fact:
args:
timezone: "{{ ansible_date_time.tz }}"
set_fact examplesIf os_version is the fact obtained by joining ansible_distribution with ansible_distribution_major_version then:
vars directory of a role for a file called e.g. CentOS7.yml - name: include variables based on OS version
include_vars: "{{ os_version }}.yml"
tasks directory of a role for a file called e.g. CentOS7.yml - name: run tasks based on OS version
include: "{{ os_version }}.yml"
include_vars and vars_filesUse include_vars to include a variables file as a task in a playbook run. You can use no_log to ensure vars aren't logged.
You can also use vars_files in a playbook to include one or more variables files. vars_files can't be used in a role.
vars/main.yml-e-e @filename.yml — can be useful for overriding defaults during an outage.The order of variables presented has been in increasing order. There are more variable types than presented here — others aren't widely used or highly recommended
See more: Ansible Variable precedence
ansible provides a tool called ansible-vault for encrypting secret variables. while other tools are available, the vault is usefully integrated.
ansible-vault create secrets.ymlansible-vault edit secrets.ymlansible-vault view secrets.ymlansible-vault encrypt secrets.ymlansible-vault encrypt secrets.ymlansible-vault rekey secrets.ymlsee more: ansible vault
ansible-playbook playbook.yml --ask-vault-pass
you can also set the password in a file (e.g. ~/.ansible/vault_pass) and use:
ansible-playbook playbook.yml --vault-password-file ~/.ansible/vault_pass
or set the ANSIBLE_VAULT_PASSWORD_FILE environment variable.
run_me is set.httpd_directory