# AnsibleUndefinedVariable: 'dict object' has no attribute 'xvdb'

**URL:** <https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [May 18, 2023, 1:14am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004 "2023-05-18T01:14:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amentee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amentee/32/9190_2.png) [@amentee](https://forum.ansible.com/u/amentee)\
**Post date:** [May 18, 2023, 1:14am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004/1 "2023-05-18T01:14:15Z")

</div>

Hi Team,

Below is the jinja template that I wrote that works fine on existing RHEL 7 machine .

```auto
UUID={{ hostvars['localhost']['ansible_facts']['device_links']['uuids']['xvda2'][0] }} / {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
UUID_home={{ hostvars['localhost']['ansible_facts']['device_links']['uuids']['xvdb'][0] }} /home {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
UUID_var={{ hostvars['localhost']['ansible_facts']['device_links']['uuids']['xvdc'][0] }} /var {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
UUID_swap={{ hostvars['localhost']['ansible_facts']['device_links']['uuids']['xvdd'][0] }} swap swap defaults 0 0

```

It works fine on existing test RHEL 7 machine but it fails when I am trying to run this template in our Jenkins job with below error.

```auto
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AnsibleUndefinedVariable: 'dict object' has no attribute 'xvdb'

```

I tried running ansible localhost -m setup and the above paths actually exist and xvdb entry is also there. My main doubt is the playbook parsed xvda2 line but fails at xvdb. Why? Below is the relevant output of ansible setup command.

```auto
\"ansible_device_links\": {",
" \"ids\": {}, ",
" \"labels\": {}, ",
" \"masters\": {}, ",
\"uuids\": {",
\"xvda2\": [",
\"87be522b-2b75-4150-879d-4ee109559f7e\"",
], ",
\"xvdb\": [",
\"da750c25-cc98-4c2a-a5fe-24bbf42c210e\"",
" ], ",
" \"xvdc\": [",
" \"c2d5f1dc-5840-40bf-9a54-bb8fb215fbec\"",
" ], ",
" \"xvdd\": [",
" \"3b78b0ff-cc18-49ff-80b4-f21ca0a1234e\"",
" ]",
" }",
" }, ",

```

Please help me to resolve this

Regards  
Sachin Kumar

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [May 18, 2023, 5:54am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004/2 "2023-05-18T05:54:21Z")

</div>

> Hi Team,
> 
> Below is the jinja template that I wrote that works fine on existing RHEL 7 machine .
> 
> ```auto
> UUID={{ hostvars['localhost']['ansible_facts']['device_links']['uuids']['xvda2'][0] }} / {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
> 
> ```

> UUID\_home={{ hostvars[‘localhost’][‘ansible\_facts’][‘device\_links’][‘uuids’][‘xvdb’][0] }}

> /home {{ ansible\_facts[‘mounts’][0].fstype }} defaults 0 0  
> UUID\_var={{ hostvars[‘localhost’][‘ansible\_facts’][‘device\_links’][‘uuids’][‘xvdc’][0] }} /var {{ ansible\_facts[‘mounts’][0].fstype }} defaults 0 0  
> UUID\_swap={{ hostvars[‘localhost’][‘ansible\_facts’][‘device\_links’][‘uuids’][‘xvdd’][0] }} swap swap defaults 0 0
> 
> ```auto
> 
> It works fine on existing test RHEL 7 machine but it fails when I am trying to run this template in our Jenkins job with below error.
> 
> ```
> 
> An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘xvdb’
> 
> ```auto
> 
> I tried running ansible localhost -m setup and the above paths actually exist and xvdb entry is also there. My main doubt is the playbook parsed xvda2 line but fails at xvdb. Why? Below is the relevant output of ansible setup command.
> 
> ```

> "ansible\_device\_links": {",

This is different from what you have in the template:

hostvars[‘localhost’][‘ansible\_facts’][‘device\_links’]

Also are you sure this “Jenkins” is targeting the right host? That could be the problem.

---

<div class="post-metadata">

**Author:** ![amentee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amentee/32/9190_2.png) [@amentee](https://forum.ansible.com/u/amentee)\
**Post date:** [May 18, 2023, 6:12am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004/3 "2023-05-18T06:12:51Z")

</div>

Hi Team,

The standard way to access is - ansible\_facts[‘device\_links’][‘uuids’][‘xvdb’][0] . It works on existing rhel 7 machine but it fails there. Also if the syntax is wrong it should have failed at xvda2 line , the very first line of jinja2 template. WHy it failed at second line ? That’s strange . What should be the correct way to extract uuid that works in jinja2 template.

Regards  
Sachin Kumar

---

<div class="post-metadata">

**Author:** ![amentee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amentee/32/9190_2.png) [@amentee](https://forum.ansible.com/u/amentee)\
**Post date:** [May 18, 2023, 6:43am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004/4 "2023-05-18T06:43:48Z")

</div>

Hi Team,

I have changed to standard jinja2 template , even then the error is same. It fails to find xvdb but it finds xvda2 . The syntax to extract UUID is same and both values are present in the ansible localhost -m setup command output . This playbook is running on localhost only.

```auto
UUID={{ ansible_facts['device_links']['uuids']['xvda2'][0] }} / {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
UUID_home={{ ansible_facts['device_links']['uuids']['xvdb'][0] }} /home {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
UUID_var={{ ansible_facts['device_links']['uuids']['xvdc'][0] }} /var {{ ansible_facts['mounts'][0].fstype }} defaults 0 0
UUID_swap={{ ansible_facts['device_links']['uuids']['xvdd'][0] }} swap swap defaults 0 0

```

Regards  
Sachin Kumar

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [May 18, 2023, 10:13am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute-xvdb/38004/5 "2023-05-18T10:13:24Z")

</div>

There is nothing wrong with the template.  
The problem is that the playbook is not run against the host that you  
think it is run.  
And because they have a similar partitioning setup, a few common  
devices exist, but some other do not.  
So, it's not really an ansible problem, but rather a problem with how  
you invoke ansible, in your environment.  
That consists of "existing test RHEL 7 machine", a "jenkins" system,  
and various incarnations of "localhost".  
Try to get that clear a bit.
