Working with Magic Variables
1. Using hostvars
| Variable | Description | Example Value |
|---|---|---|
| hostvars | Dictionary containing variables for all hosts in the inventory | {"web01": {"ansible_host": "192.168.1.10", "ansible_user": "admin"}, "db01": {"ansible_host": "192.168.1.11", "ansible_user": "dbadmin"}} |
| hostvars['hostname'] | Access variables for a specific host | {"ansible_host": "192.168.1.10", "ansible_user": "admin"} |
Example: Accessing host-specific variables
- name: Display host-specific variables
debug:
msg: "Host {{ inventory_hostname }} has IP {{ hostvars[inventory_hostname].ansible_host }}"
2. Using inventory_hostname
| Property | Description | Example |
|---|---|---|
| inventory_hostname | The name of the host as defined in the inventory | web01 |
| inventory_hostname_short | The short name of the host (before any underscores) | web |
Example: Using inventory_hostname in a task
- name: Create directory with host-specific name
file:
path: "/tmp/{{ inventory_hostname }}"
state: directory
Example: Using inventory_hostname_short in a task
- name: Set up environment based on host type
set_fact:
env_prefix: "{{ inventory_hostname_short }}_"
3. Using inventory_hostname_short
| Property | Description | Example |
|---|---|---|
| inventory_hostname_short | Short name of the inventory host, without any domain suffixes. | web01 |
Example: Using inventory_hostname_short in a task
- name: "Set hostname variable"
set_fact:
host_name: "{{ inventory_hostname_short }}"
4. Using group_names
| Property | Description | Example |
|---|---|---|
| group_names | List of all group names the host belongs to | ["web", "production"] |
Example: Using group_names in a Playbook
- name: "Display group names"
hosts: all
tasks:
- name: "Print group names"
debug:
msg: "Host belongs to groups: {{ group_names }}"
Note:
group_names is a read-only variable and cannot be modified.5. Using groups
| Variable | Description | Example Value |
|---|---|---|
| groups | All group names and their hosts | {"all": ["web1", "db1"], "web": ["web1"], "db": ["db1"]} |
| group_names | Current host's group names | ["web"] |
| group_all | All hosts in the inventory | ["web1", "db1"] |
Example: Accessing group members
- name: Display all hosts in the web group
debug:
msg: "Web servers: {{ groups['web'] }}"
Example: Using group names in conditionals
- name: Configure web server
command: systemctl start nginx
when: "'web' in group_names"
6. Using ansible_play_hosts
| Variable | Description | Usage |
|---|---|---|
| ansible_play_hosts | List of all hosts in the current play | Used to iterate over all hosts or check membership in a play |
Example: Looping over all hosts in a play
- name: Loop over all hosts in the play
debug:
msg: "Processing host: {{ item }}"
loop: "{{ ansible_play_hosts }}"
Example: Check if a host is part of the play
- name: Verify host is in play
debug:
msg: "Host {{ inventory_hostname }} is part of the play"
when: inventory_hostname in ansible_play_hosts
7. Using ansible_play_batch
| Variable | Description | Example Value |
|---|---|---|
| ansible_play_batch | Contains the list of hosts in the current play's batch | ["host1", "host2", "host3"] |
Example: Using ansible_play_batch in a Playbook
- name: Display hosts in current batch
hosts: all
tasks:
- name: Show current batch
debug:
msg: "Current batch: {{ ansible_play_batch }}"
8. Using playbook_dir
| Variable | Description | Example Value |
|---|---|---|
| playbook_dir | Directory containing the playbook file being executed | /home/user/ansible/playbooks/webapp |
Example: Reading playbook_dir in a task
- name: Show playbook directory
debug:
msg: "This playbook is located in {{ playbook_dir }}"
Note: The
playbook_dir variable is particularly useful when you need to reference files relative to the playbook's location, such as including templates or configuration files.
9. Using role_path
| Variable | Description | Example Value |
|---|---|---|
| role_path | Path to the current role's directory | /etc/ansible/roles/myapp |
Example: Accessing role_path in a task
- name: Display role path
debug:
msg: "Current role is located at {{ role_path }}"
Note: The
role_path variable is automatically set by Ansible when executing tasks within a role context. It provides the absolute path to the directory where the current role is stored.
10. Using ansible_version
| Variable | Description | Example Value |
|---|---|---|
| ansible_version | Version of Ansible currently running | 2.16.5 |
Example: Display Ansible Version
- name: Display Ansible version
debug:
msg: "Running Ansible version {{ ansible_version }}"
Note: The
ansible_version variable is particularly useful for conditional logic or logging within playbooks to ensure compatibility with specific Ansible features or bug fixes.