In Ansible, accessing inventory host variables within a playbook is straightforward and can be done using the hostvars dictionary. Here's how you can access inventory host variables and use them in your playbook:
Assume you have an inventory file (inventory.yml) structured like this:
all: hosts: webserver1: ansible_host: 192.168.1.101 ansible_user: ubuntu ansible_ssh_private_key_file: ~/.ssh/id_rsa my_custom_variable: value1 webserver2: ansible_host: 192.168.1.102 ansible_user: centos ansible_ssh_private_key_file: ~/.ssh/id_rsa my_custom_variable: value2
Below is an example Ansible playbook (playbook.yml) that demonstrates how to access host variables from the inventory:
--- - name: Example playbook to access host variables hosts: all gather_facts: false # Disable gathering facts for this example tasks: - name: Print host variables debug: msg: | Host: {{ inventory_hostname }} ansible_host: {{ ansible_host }} ansible_user: {{ ansible_user }} my_custom_variable: {{ hostvars[inventory_hostname]['my_custom_variable'] }} Inventory Variables: Each host in the inventory (webserver1 and webserver2) has defined variables like ansible_host, ansible_user, and my_custom_variable.
Playbook Configuration:
hosts: all: This playbook will run against all hosts defined in the inventory.gather_facts: false: Disables gathering facts to focus solely on demonstrating variable access.Tasks:
debug task prints various host variables using Jinja2 templating.{{ inventory_hostname }}: The current host being processed.{{ ansible_host }}, {{ ansible_user }}: Standard Ansible variables.{{ hostvars[inventory_hostname]['my_custom_variable'] }}: Accesses the my_custom_variable specific to each host using hostvars.Run the playbook using the ansible-playbook command:
ansible-playbook -i inventory.yml playbook.yml
When you run the playbook, you will see output similar to this:
PLAY [Example playbook to access host variables] ********************************* TASK [Print host variables] **************************************************** ok: [webserver1] => { "msg": "Host: webserver1\nansible_host: 192.168.1.101\nansible_user: ubuntu\nmy_custom_variable: value1" } ok: [webserver2] => { "msg": "Host: webserver2\nansible_host: 192.168.1.102\nansible_user: centos\nmy_custom_variable: value2" } PLAY RECAP ********************************************************************* webserver1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 webserver2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 hostvars[inventory_hostname]['variable_name'] to access host variables within your playbook tasks.inventory.yml) is correctly structured and variables are defined appropriately for each host.playbook.yml) and inventory variables (inventory.yml) according to your specific requirements and variable names.How to access host variables in Ansible playbook?
{{ variable_name }} syntax.- hosts: all tasks: - name: Print hostname debug: msg: "The hostname is {{ ansible_hostname }}" How to use custom host variables in Ansible?
# Inventory file [webservers] web1 ansible_ip=192.168.1.10 web2 ansible_ip=192.168.1.11 # Playbook - hosts: webservers tasks: - name: Show IP address debug: msg: "The IP address is {{ ansible_ip }}" How to access group variables in an Ansible playbook?
# Group vars file (group_vars/webservers.yml) message: "Hello from web servers" # Playbook - hosts: webservers tasks: - name: Print group variable debug: msg: "{{ message }}" How to define and access variables in Ansible inventory?
{{ variable_name }} in your tasks.# Inventory file [dbservers] db1 ansible_port=3306 # Playbook - hosts: dbservers tasks: - name: Check MySQL port debug: msg: "MySQL is running on port {{ ansible_port }}" How to override inventory variables in Ansible playbook?
- hosts: all vars: ansible_port: 8080 tasks: - name: Show overridden port debug: msg: "The port is {{ ansible_port }}" How to access all host variables in Ansible?
hostvars dictionary to access all variables for a specific host within your playbook.- hosts: all tasks: - name: Show variables for another host debug: msg: "{{ hostvars['web1'] }}" How to use Ansible facts to access host variables?
- hosts: all tasks: - name: Display OS information debug: msg: "The OS is {{ ansible_os_family }}" How to access dynamic inventory variables in Ansible?
- hosts: all tasks: - name: Show dynamic inventory variable debug: msg: "The instance ID is {{ ec2_instance_id }}" How to access inventory variables conditionally in Ansible?
- hosts: all tasks: - name: Print variable if defined debug: msg: "The variable is {{ custom_variable }}" when: custom_variable is defined How to access Ansible inventory variables in Jinja2 templates?
Description: Use Jinja2 syntax to access host variables within templates used in your playbook.
Code:
- hosts: all tasks: - name: Create a configuration file from template template: src: config.j2 dest: /etc/myapp/config.conf
# config.j2 host_name={{ ansible_hostname }} ip_address={{ ansible_ip }} identity-column ruby-on-rails-3 class-design stylus richtextbox edmx tedious libusb typescript1.8 board-games