Accessing inventory host variable in Ansible playbook

Accessing inventory host variable in Ansible playbook

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:

Inventory Setup

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 

Ansible Playbook Example

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'] }} 

Explanation:

  1. Inventory Variables: Each host in the inventory (webserver1 and webserver2) has defined variables like ansible_host, ansible_user, and my_custom_variable.

  2. 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.
  3. Tasks:

    • Print Host Variables: The 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.

Running the Playbook

Run the playbook using the ansible-playbook command:

ansible-playbook -i inventory.yml playbook.yml 

Output Example

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 

Summary

  • Use hostvars[inventory_hostname]['variable_name'] to access host variables within your playbook tasks.
  • Ensure your inventory (inventory.yml) is correctly structured and variables are defined appropriately for each host.
  • Adjust the playbook (playbook.yml) and inventory variables (inventory.yml) according to your specific requirements and variable names.

Examples

  1. How to access host variables in Ansible playbook?

    • Description: You can access host variables defined in your inventory file using the {{ variable_name }} syntax.
    • Code:
      - hosts: all tasks: - name: Print hostname debug: msg: "The hostname is {{ ansible_hostname }}" 
  2. How to use custom host variables in Ansible?

    • Description: Custom host variables can be defined in the inventory file or in a separate variable file and accessed in your playbook.
    • Code:
      # 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 }}" 
  3. How to access group variables in an Ansible playbook?

    • Description: Group variables can be defined in the inventory or in a group_vars directory and accessed using the group name.
    • Code:
      # Group vars file (group_vars/webservers.yml) message: "Hello from web servers" # Playbook - hosts: webservers tasks: - name: Print group variable debug: msg: "{{ message }}" 
  4. How to define and access variables in Ansible inventory?

    • Description: Variables can be defined directly in the inventory file or as separate files. Access them using {{ variable_name }} in your tasks.
    • Code:
      # Inventory file [dbservers] db1 ansible_port=3306 # Playbook - hosts: dbservers tasks: - name: Check MySQL port debug: msg: "MySQL is running on port {{ ansible_port }}" 
  5. How to override inventory variables in Ansible playbook?

    • Description: You can override inventory variables by defining them in your playbook or through extra variables at runtime.
    • Code:
      - hosts: all vars: ansible_port: 8080 tasks: - name: Show overridden port debug: msg: "The port is {{ ansible_port }}" 
  6. How to access all host variables in Ansible?

    • Description: Use the hostvars dictionary to access all variables for a specific host within your playbook.
    • Code:
      - hosts: all tasks: - name: Show variables for another host debug: msg: "{{ hostvars['web1'] }}" 
  7. How to use Ansible facts to access host variables?

    • Description: Ansible gathers facts about hosts which can be accessed directly in the playbook.
    • Code:
      - hosts: all tasks: - name: Display OS information debug: msg: "The OS is {{ ansible_os_family }}" 
  8. How to access dynamic inventory variables in Ansible?

    • Description: Dynamic inventory can provide host variables which can be accessed like static inventory.
    • Code:
      - hosts: all tasks: - name: Show dynamic inventory variable debug: msg: "The instance ID is {{ ec2_instance_id }}" 
  9. How to access inventory variables conditionally in Ansible?

    • Description: You can use conditionals to check the presence of inventory variables before accessing them.
    • Code:
      - hosts: all tasks: - name: Print variable if defined debug: msg: "The variable is {{ custom_variable }}" when: custom_variable is defined 
  10. 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 }} 

More Tags

identity-column ruby-on-rails-3 class-design stylus richtextbox edmx tedious libusb typescript1.8 board-games

More Programming Questions

More Statistics Calculators

More Fitness-Health Calculators

More Mortgage and Real Estate Calculators

More Everyday Utility Calculators