# getting Asible.errors.AnsibleUndefinedVariable: 'broker' is undefined despite assigning in inventory file

**URL:** <https://forum.ansible.com/t/getting-asible-errors-ansibleundefinedvariable-broker-is-undefined-despite-assigning-in-inventory-file/38760>\
**Category:** Ansible Project\
**Created:** [April 22, 2024, 6:22pm UTC](https://forum.ansible.com/t/getting-asible-errors-ansibleundefinedvariable-broker-is-undefined-despite-assigning-in-inventory-file/38760 "2024-04-22T18:22:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [April 22, 2024, 6:22pm UTC](https://forum.ansible.com/t/getting-asible-errors-ansibleundefinedvariable-broker-is-undefined-despite-assigning-in-inventory-file/38760/1 "2024-04-22T18:22:29Z")

</div>

Hello Team,

I am putting broker: true and zookeeper: true in inventory file but i still getting below exception as its referred in j2 file

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: ‘broker’ is undefined. ‘broker’ is undefined

fatal: [en1qa1-zookpr02.qa]: FAILED! =\> {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘broker’ is undefined. ‘broker’ is undefined”}

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: ‘broker’ is undefined. ‘broker’ is undefined

fatal: [en1qa1-zookpr01.qa: FAILED! =\> {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘broker’ is undefined. ‘broker’ is undefined”}

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 23, 2024, 2:44am UTC](https://forum.ansible.com/t/getting-asible-errors-ansibleundefinedvariable-broker-is-undefined-despite-assigning-in-inventory-file/38760/2 "2024-04-23T02:44:54Z")

</div>

The messages are correct. You have only defined the “broker” variable on one set of hosts, and the two hosts indicated in the errors are not in that set.

You can fix this in a couple of ways. One way is to define all three variables in all three sets of hosts:

```auto
qa_eng_kafka_cluster:
   hosts:
     en1qa1-zookpr[01:03].qa:
       broker: false
       schema_registry: false
       zookeeper: true
     en1qa1-kafka[01:06].qa:
       broker: true
       schema_registry: false
       zookeeper: false
     en1qa1-kafka[04:06].qa:
       broker: false
       schema_registry: true
       zookeeper: false

```

The other way is to define all three variables in a higher group, setting them all to false, say, and then at the host level set only the ones you want to be true to true:

```auto
qa_kafka:
  vars:
    broker: false
    schema_registry: false
    zookeeper: false
  children:
    cc:
      children:
        qa_eng_kafka_cluster:
           hosts:
             en1qa1-zookpr[01:03].qa:
               zookeeper: true
             en1qa1-kafka[01:06].qa:
               broker: true
             en1qa1-kafka[04:06].qa:
               schema_registry: true

```

To verify the variables are being set and associated with hosts appropriately, you can run:

```auto
$ ansible-inventory -i kafka_inventory.yaml --list

```

Cheers,

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 23, 2024, 7:04pm UTC](https://forum.ansible.com/t/getting-asible-errors-ansibleundefinedvariable-broker-is-undefined-despite-assigning-in-inventory-file/38760/3 "2024-04-23T19:04:10Z")

</div>

There is yet another way to approach this that I like better than adding variables to hosts in your inventory. It looks like this:

```auto
qa_kafka:
  children:
    cc:
      children:
        qa_eng_kafka_cluster:
          children:
            qa_eng_zoo:
              hosts:
                en1qa1-zookpr[01:03].qa
            qa_eng_bro:
              hosts:
                en1qa1-kafka[01:06].qa
            qa_eng_sch:
              hosts:
                en1qa1-kafka[04:06].qa

```

This creates three additional host groups: qa\_eng\_zoo, qa\_eng\_bro, and qa\_end\_sch. In your templates and whatnot, you can simply check for group membership.

Cheers,
