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

41 views
Skip to first unread message

Sac Isilia

unread,
May 17, 2023, 9:14:16 PM5/17/23
to Ansible Project
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

Dick Visser

unread,
May 18, 2023, 1:54:48 AM5/18/23
to ansible...@googlegroups.com
On Thu, 18 May 2023 at 03:14, Sac Isilia <udayprata...@gmail.com> wrote:
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. 




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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/52ff8092-399f-48ab-901a-228ce8c16c0cn%40googlegroups.com.
--
Sent from Gmail Mobile

Sac Isilia

unread,
May 18, 2023, 2:12:51 AM5/18/23
to Ansible Project
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

Sac Isilia

unread,
May 18, 2023, 2:43:48 AM5/18/23
to Ansible Project
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

Dick Visser

unread,
May 18, 2023, 6:14:01 AM5/18/23
to ansible...@googlegroups.com
On Thu, 18 May 2023 at 08:44, Sac Isilia <udayprata...@gmail.com> wrote:
>
> 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.

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.
Reply all
Reply to author
Forward
0 new messages