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

Hi Team,

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

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.

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.

\"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

Hi Team,

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

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.

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.

"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.

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

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.

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

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.