# Help generating device report in Ansible

**URL:** <https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017>\
**Category:** Ansible Project\
**Created:** [March 20, 2017, 7:10pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017 "2017-03-20T19:10:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![asilver](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@asilver](https://forum.ansible.com/u/asilver)\
**Post date:** [March 20, 2017, 7:10pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/1 "2017-03-20T19:10:13Z")

</div>

Hello,  
I’m fairly new to Ansible and trying to do something very simple. I want to collect a piece of information (version number) from a number a network devices and generate a simple report.

- name: Get Data from devices  
gather\_facts: False  
hosts: all

connection: local  
roles:

- Juniper.junos

tasks:

- name: Netconf test  
wait\_for: host={{ inventory\_hostname }} port=830 timeout=5

# Do a quick test to gather facts

- name: Getting Facts  
junos\_get\_facts: host={{ inventory\_hostname }} logfile=tmp.log user=me passwd=passwd123  
register: data

- name: Save version data to jinja2 template  
template: src=junos\_version\_report.j2 dest=./ver\_report.txt

Here is the j2 file:

Device Serial Number Version

* * *

{% for i in play\_hosts %}  
{{ i }} {{ data.facts.serialnumber }}

{% endfor %}

The problem is when the report is written to the file, the information in the “data” variable is only from the last device that set it… As you can see, the Serial number is reported as being identical for each device ( which it is not…)

Device Serial Number Version

* * *

dev1 77633B97ACF0  
dev2 77633B97ACF0  
dev3 77633B97ACF0

I’ve seen an example like this with the hostvars variable but how do I define my own variable to be an array like hostvars so I can then reference each object in the array. For example:

{{ data[i].facts.serialnumber }}

Or open to other ideas?

Thanks  
Al

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 20, 2017, 7:51pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/2 "2017-03-20T19:51:13Z")

</div>

Change the line to this.

{{ i }} {{ hostvars[i].data.facts.serialnumber }}

---

<div class="post-metadata">

**Author:** ![asilver](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@asilver](https://forum.ansible.com/u/asilver)\
**Post date:** [March 20, 2017, 8:17pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/3 "2017-03-20T20:17:08Z")

</div>

hostvars[i] works because that is predefined variable in ansible. However “data” is not. I tried data[i].facts.serialnumber but I get an error back thats says data is undefined.  
Thx

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 20, 2017, 8:27pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/4 "2017-03-20T20:27:57Z")

</div>

Did you try my suggestion? You syntax is wrong, thats why I wrote what I wrote.

"data" exist since you used "register: data" on the task before called "Netconf test".

---

<div class="post-metadata">

**Author:** ![asilver](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@asilver](https://forum.ansible.com/u/asilver)\
**Post date:** [March 20, 2017, 10:27pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/5 "2017-03-20T22:27:19Z")

</div>

That was it Kia… Thank you. I should have looked at your response more closely. I missed that you had “data” as a child of hostvars. So is that true in all cases any time you define a register variable? That variable will be a child within hostvars?

Thank you again.

{{ hostvars[i].data.facts.serialnumber }}

---

<div class="post-metadata">

**Author:** ![asilver](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@asilver](https://forum.ansible.com/u/asilver)\
**Post date:** [March 21, 2017, 6:09pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/6 "2017-03-21T18:09:16Z")

</div>

Any recommendations for problem related to this solution. If connectivity to any one devices fails, then the registered variable (data) is not accessible for any of the other devices that actually passed. This results on the following error when the jinja2 template part of the playbook is executed. I understand the data variable not being available for the device that failed but why isn’t available for the other 3 devices that collected the data with no problems??  
Thx

fatal: [device1]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘facts’”}  
fatal: [device2]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘facts’”}  
fatal: [device3]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘facts’”}

---

<div class="post-metadata">

**Author:** ![asilver](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@asilver](https://forum.ansible.com/u/asilver)\
**Post date:** [March 21, 2017, 6:59pm UTC](https://forum.ansible.com/t/help-generating-device-report-in-ansible/24017/7 "2017-03-21T18:59:53Z")

</div>

Ok, I got around this issue by editing the jinja template file to make sure the variable existed before referencing it… Then everything fell into place. The error message was throwing me off a bit as it was saying the variable did not exist even for the devices that passed. Anyhow, this is what I added to jinja template…

{%if hostvars[i].data.facts is defined %}  
{{ “%-15s”|format(i) }} {{ “%-15s”|format(hostvars[i].data.facts.serialnumber) }} {{ “%-s”|format(hostvars[i].data.facts.version) }}  
{% else %}  
{{ “%-15s”|format(i) }} {{ “%-15s”|format(‘–’) }} {{ “%-s”|format(‘–’) }}  
{% endif %}

Thanks  
Al
