Skip to content

Example 11

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].ansible_user | default('ute') }}"

Tip

Ansible offers a set of built-in filters which can be used inside the template clauses. One of the most commonly used is default.

Execute the following command

ansible-playbook playbook.yml

Bonus round

Change the ansible_user for host centos1 to root and run the command again.