What Are Magic Variables?

Ansible automatically defines magic variables (also called special variables) that you can use in any playbook. These provide information about hosts, groups, the play environment, and execution context.

Host Variables

inventory_hostname

The name of the current host as defined in inventory:

- name: Show current host
  ansible.builtin.debug:
    msg: "Running on {{ inventory_hostname }}"

inventory_hostname_short

The first part of inventory_hostname (before the first dot):

# If inventory_hostname = web1.example.com
- ansible.builtin.debug:
    msg: "Short name: {{ inventory_hostname_short }}"
    # Output: "Short name: web1"

ansible_host

The actual IP/hostname Ansible connects to:

- ansible.builtin.debug:
    msg: "Connecting to {{ ansible_host }}"

ansible_facts

Dictionary of all gathered facts about the current host:

- name: Show OS family
  ansible.builtin.debug:
    msg: "OS: {{ ansible_facts['os_family'] }}"

- name: Show memory
  ansible.builtin.debug:
    msg: "Total RAM: {{ ansible_facts['memtotal_mb'] }} MB"

- name: Show IP address
  ansible.builtin.debug:
    msg: "IP: {{ ansible_facts['default_ipv4']['address'] }}"

Common facts:

  • ansible_facts['os_family'] — Debian, RedHat, etc.
  • ansible_facts['distribution'] — Ubuntu, CentOS, etc.
  • ansible_facts['memtotal_mb'] (also ansible_memtotal_mb)
  • ansible_facts['processor_vcpus']
  • ansible_facts['default_ipv4']['address']

Group Variables

groups

Dictionary of all groups and their hosts:

- name: List all web servers
  ansible.builtin.debug:
    msg: "Web servers: {{ groups['webservers'] }}"

- name: List all hosts
  ansible.builtin.debug:
    msg: "All hosts: {{ groups['all'] }}"

group_names

List of groups the current host belongs to:

- name: Show my groups
  ansible.builtin.debug:
    msg: "{{ inventory_hostname }} is in groups: {{ group_names }}"

- name: Run only for webservers
  ansible.builtin.apt:
    name: nginx
    state: present
  when: "'webservers' in group_names"

hostvars

Access variables from any host:

- name: Get DB server IP
  ansible.builtin.debug:
    msg: "DB IP: {{ hostvars['db1']['ansible_host'] }}"

- name: Get another host's fact
  ansible.builtin.debug:
    msg: "DB memory: {{ hostvars['db1']['ansible_memtotal_mb'] }}"

Play and Execution Variables

playbook_dir

The directory where the playbook file lives:

- name: Show playbook location
  ansible.builtin.debug:
    msg: "Playbook is at: {{ playbook_dir }}"

- name: Copy file relative to playbook
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/files/config.yml"
    dest: /opt/app/config.yml

role_path

The directory of the current role:

# Inside a role's tasks/main.yml
- name: Copy template from role
  ansible.builtin.template:
    src: "{{ role_path }}/templates/app.conf.j2"
    dest: /etc/app/app.conf

ansible_play_hosts

List of hosts active in the current play:

- name: Show active hosts
  ansible.builtin.debug:
    msg: "Active hosts: {{ ansible_play_hosts }}"

- name: Count active hosts
  ansible.builtin.debug:
    msg: "Deploying to {{ ansible_play_hosts | length }} hosts"

ansible_play_batch

Hosts in the current batch (when using serial):

- name: Show current batch
  ansible.builtin.debug:
    msg: "Batch: {{ ansible_play_batch }}"

play_hosts (deprecated)

Use ansible_play_hosts instead.

Execution Context Variables

VariableDescription
ansible_versionAnsible version info dict
ansible_check_modetrue if running in check mode (--check)
ansible_diff_modetrue if running with --diff
ansible_verbosityVerbosity level (0-4)
ansible_run_tagsTags provided via --tags
ansible_skip_tagsTags provided via --skip-tags
- name: Skip destructive task in check mode
  ansible.builtin.command: /opt/app/migrate.sh
  when: not ansible_check_mode

- name: Extra detail in verbose mode
  ansible.builtin.debug:
    msg: "Detailed info: {{ hostvars[inventory_hostname] }}"
  when: ansible_verbosity >= 2

Task Result Variables

After register:

- name: Run command
  ansible.builtin.command: whoami
  register: result

- name: Use result
  ansible.builtin.debug:
    msg: |
      stdout: {{ result.stdout }}
      rc: {{ result.rc }}
      changed: {{ result.changed }}

Quick Reference Table

VariableTypeDescription
inventory_hostnamestringCurrent host name from inventory
inventory_hostname_shortstringShort hostname (before first dot)
ansible_hoststringConnection address
ansible_factsdictGathered facts
groupsdictAll groups → host lists
group_nameslistGroups current host belongs to
hostvarsdictVariables for all hosts
playbook_dirstringPlaybook directory path
role_pathstringCurrent role directory
ansible_play_hostslistActive hosts in play
ansible_play_batchlistCurrent serial batch
ansible_check_modeboolCheck mode active
ansible_versiondictAnsible version info

Explore 800+ Ansible tutorials with practical variable examples on AnsibleByExample.