Loading...

Watch: Ansible troubleshooting - VARIABLE IS NOT DEFINED! ansible_hostname

Join Luca Berton on Ansible Pilot as we explore troubleshooting for the VARIABLE IS NOT DEFINED! error, focusing on practical solutions.

Introduction

Today we’re going to talk about Ansible troubleshooting, specifically about VARIABLE IS NOT DEFINED! Message.

Most of the time the root cause is a misspelled variable or a variable really not defined. This use case is special about the ansible_hostname internal variable.

I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Playbook

The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the VARIABLE IS NOT DEFINED! and how to solve it!

error code

``yaml

---

  • name: hostname Playbook

hosts: all

gather_facts: false

tasks:

- name: print hostname

ansible.builtin.debug:

var: ansible_hostname

`

error execution

`bash

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/variablenotdefined_error.yml

PLAY [hostname Playbook] **

TASK [print hostname] *

ok: [demo.example.com] => {

"ansible_hostname": "VARIABLE IS NOT DEFINED!"

}

PLAY RECAP **

demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible-pilot $

`

fix code

`yaml

---

  • name: hostname Playbook

hosts: all

gather_facts: true

tasks:

- name: print hostname

ansible.builtin.debug:

var: ansible_hostname

`

fix execution

`bash

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/variablenotdefined_fix.yml

PLAY [hostname Playbook] **

TASK [Gathering Facts]

ok: [demo.example.com]

TASK [print hostname] *

ok: [demo.example.com] => {

"ansible_hostname": "Playbook"

}

PLAY RECAP **

demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible-pilot $

``

[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting)

Conclusion

Now you know better how to troubleshoot the Ansible VARIABLE IS N

Read the full tutorial: Ansible troubleshooting - VARIABLE IS NOT DEFINED! ansible_hostname