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'](alsoansible_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
| Variable | Description |
|---|---|
ansible_version | Ansible version info dict |
ansible_check_mode | true if running in check mode (--check) |
ansible_diff_mode | true if running with --diff |
ansible_verbosity | Verbosity level (0-4) |
ansible_run_tags | Tags provided via --tags |
ansible_skip_tags | Tags 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
| Variable | Type | Description |
|---|---|---|
inventory_hostname | string | Current host name from inventory |
inventory_hostname_short | string | Short hostname (before first dot) |
ansible_host | string | Connection address |
ansible_facts | dict | Gathered facts |
groups | dict | All groups → host lists |
group_names | list | Groups current host belongs to |
hostvars | dict | Variables for all hosts |
playbook_dir | string | Playbook directory path |
role_path | string | Current role directory |
ansible_play_hosts | list | Active hosts in play |
ansible_play_batch | list | Current serial batch |
ansible_check_mode | bool | Check mode active |
ansible_version | dict | Ansible version info |
Related Articles
- Ansible Tutorial for Beginners — Complete getting started guide
- How to Install Ansible — Installation on all platforms
- Ansible Training — Courses and certifications
- Ansible Dry Run — Test playbooks safely
- Ansible Facts — Gather system info
- Ansible Vault — Encrypt secrets
- Ansible Error Handling — Handle failures
Explore 800+ Ansible tutorials with practical variable examples on AnsibleByExample.