Skip to content

Example 10

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- name: Example playbook
  hosts: centos1
  gather_facts: false
  tasks:
    - name: Accessing the hostvars
      debug:
        msg: "{{ hostvars[inventory_hostname] }}"

Tip

Ansible offers a set of built-in variables such as hostvars and inventory_hostname.

Execute the following command

ansible-playbook playbook.yml

Bonus round

Modify the playbook to return the value of ansible_host variable.