Chapter 4: Managing Variables and Facts

Ansible variables and Vault cheat sheet

The chapter on one page: what to remember, the commands and patterns to have at your fingertips, and flashcards for quick revision.

The chapter in eight sentences

Cheat sheet

Variables

- name: Example play
  hosts: webservers
  vars:
    web_pkg: httpd
  vars_files:
    - vars/common.yml
    - vars/secret.yml          # may be Vault-encrypted
  tasks:
    - name: Install {{ web_pkg }}
      ansible.builtin.dnf:
        name: "{{ web_pkg }}"  # quoted: the value starts with {{
      register: install_result

    - ansible.builtin.debug:
        var: install_result['changed']

Project layout

Facts

- name: Facts demo
  hosts: all
  gather_facts: true            # the default; false skips setup
  tasks:
    - ansible.builtin.debug:
        msg: >
          {{ ansible_facts['fqdn'] }} runs
          {{ ansible_facts['distribution'] }} {{ ansible_facts['distribution_version'] }}
          at {{ ansible_facts['default_ipv4']['address'] }}

    - name: Refresh or limit facts
      ansible.builtin.setup:
        gather_subset:
          - '!hardware'
Vault taskCommand
Create an encrypted fileansible-vault create secret.yml
Read itansible-vault view secret.yml
Change itansible-vault edit secret.yml
Encrypt existing filesansible-vault encrypt a.yml b.yml
Remove encryptionansible-vault decrypt a.yml [--output=plain.yml]
Change the passwordansible-vault rekey secret.yml
Run with a promptansible-navigator run -m stdout --playbook-artifact-enable false site.yml --vault-id @prompt
Run with a password fileansible-navigator run -m stdout site.yml --vault-password-file=vault-pass
WantUse
Override for one run-e "name=value"
Every fact of a hostansible.builtin.debug: var=ansible_facts
A custom factansible_facts['ansible_local']['FILE']['SECTION']['KEY']
Another host's variableshostvars['HOST']['VAR']
This host's groupsgroup_names
All groups and hostsgroups
This host's inventory nameinventory_hostname
What characters can a variable name contain?
Letters, numbers and underscores, and it must **start with a letter**.
When must a variable reference be quoted?
When it is the **first thing in the value**: `name: "{{ user }}"`.
Where do group variables go, by recommended practice?
`group_vars/GROUP` (and `host_vars/HOST` for hosts), not in the inventory file.
Which beats which: host_vars or group_vars?
**host_vars**. Narrower scope wins.
Which beats which: inventory variables or play vars?
**Play vars**. The playbook beats the inventory.
What has the highest precedence of all?
Extra variables: `-e` / `--extra-vars` on the command line.
What does register store?
The full JSON result of the task, in a variable you name.
view vs edit in ansible-vault?
`view` only reads; `edit` rewrites the file (shows as a change in Git).
Why must playbook artifacts be disabled for --vault-id @prompt?
Otherwise ansible-navigator cannot prompt and **hangs**.
Which module gathers facts?
`ansible.builtin.setup`, run automatically as Gathering Facts unless `gather_facts: false`.
Where are custom facts stored on the host?
`/etc/ansible/facts.d/*.fact`, in INI or JSON (or an executable printing JSON).
Where do custom facts appear?
`ansible_facts['ansible_local']['FILE']['SECTION']['KEY']`
group_names vs groups?
`group_names`: this host’s groups. `groups`: every group and its hosts.