# Using Ansible Facts to get devices information

**URL:** <https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170>\
**Category:** Ansible Project\
**Created:** [December 29, 2012, 12:28am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170 "2012-12-29T00:28:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [December 29, 2012, 12:28am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/1 "2012-12-29T00:28:40Z")

</div>

Ansible facts, i.e. when i run ansible all -m setup I get all the basic information like the interfaces, IP etc, But i am really interested in also getting disk/partition information and devices attached, Like the information with fdisk and hdparm/smartctl will give.

Is there a way i can get those from facts ? If not what would be the right vehicle ? Modify setup.py to get the details needed ?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 29, 2012, 12:45am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/2 "2012-12-29T00:45:22Z")

</div>

Modifying setup will work, but take into account that this is relied on by default for all plays. I had this at one point (for freebsd), but didn’t need it any more and setup has changed a lot since then.

You can also create your own module that adds custom facts.

Brian Coca

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 29, 2012, 1:03am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/3 "2012-12-29T01:03:41Z")

</div>

I just remembered, check the setup module docs, it uses a couple of popular system info gathering utilities if they are installed on the target, they might already get the mount info.

Brian Coca

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [December 29, 2012, 9:40am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/4 "2012-12-29T09:40:12Z")

</div>

It looks like facter/ohai needs ruby installed on remote machines, I wanted something pure python and with least dependencies, So tried to create a module.

newbie alert ! How to get this module (attached) reviewed /get help ?

I tried to port the disk-info python script to a Ansible module (from little i understand- started reading about Ansible yesterday). I have attached the module here. I would appreciate if i can get some comments on how to improve it , so it follows all the ansible conventions.

The output looks like this:

{‘Devices’: {‘sda’: {‘vendor’: ‘ATA’, ‘sectors’: ‘976773168’, ‘sector size(bytes)’: ‘512’, ‘disk type’: ‘Spinning disk’, ‘host’: ‘SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family SATA AHCI Controller (rev 05)’, ‘sda2’: {‘holders’: {0: ‘VolGroup00-LogVol01’, 1: ‘VolGroup00-LogVol00’}, ‘sectors’: ‘975747072’, ‘start sector’: ‘1026048’, ‘size’: ‘465.27GiB’}, ‘removable’: ‘No’, ‘sda1’: {‘sectors’: ‘1024000’, ‘start sector’: ‘2048’, ‘size’: ‘500.00MiB’}, ‘support discard’: ‘No’, ‘model’: ‘ST500DM002-1BC14’, ‘size’: ‘465.76GiB’}, ‘sdb’: {‘vendor’: ‘ATA’, ‘sectors’: ‘395093371’, ‘sector size(bytes)’: ‘512’, ‘disk type’: ‘SSD’, ‘host’: ‘SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family SATA AHCI Controller (rev 05)’, ‘removable’: ‘No’, ‘support discard’: ‘Yes’, ‘model’: ‘SAMSUNG SSD 830’, ‘size’: ‘188.40GiB’}, ‘sdc’: {‘vendor’: ‘ATA’, ‘sectors’: ‘395093371’, ‘sector size(bytes)’: ‘512’, ‘disk type’: ‘SSD’, ‘host’: ‘SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family SATA AHCI Controller (rev 05)’, ‘removable’: ‘No’, ‘support discard’: ‘Yes’, ‘model’: ‘SAMSUNG SSD 830’, ‘size’: ‘188.40GiB’}}}

(to get the above result in main of get\_devices module, i commented out ansible related stuff and ran as a standalone python script which just printed output of get\_devices(module))

Regards,–  
Kavin K

> **(attachments)**
>
> [get\_devices](https://forum.ansible.com/uploads/short-url/oOOR71Duf7mRdKrYhEqOdkBiljL) (9.11 KB)

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [December 29, 2012, 10:04am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/5 "2012-12-29T10:04:08Z")

</div>

I guess cannot attach file: uploaded it here:

[https://www.filepicker.io/api/file/egn0MijfQ1ISBD8wxALQ](https://www.filepicker.io/api/file/egn0MijfQ1ISBD8wxALQ)

---

<div class="post-metadata">

**Author:** ![James\_Martin](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@James\_Martin](https://forum.ansible.com/u/James_Martin)\
**Post date:** [December 29, 2012, 2:13pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/6 "2012-12-29T14:13:20Z")

</div>

> It looks like facter/ohai needs ruby installed on remote machines, I  
> wanted something pure python and with least dependencies, So tried to  
> create a module.
> 
> newbie alert ! How to get this module (attached) reviewed /get help ?

The way I understand it is that you put your module data in a dict named  
ansible\_facts and return it , like this:

&nbsp;&nbsp;&nbsp;&nbsp;ansible\_facts['riak\_primary\_node'] = host\_list[0]  
&nbsp;&nbsp;&nbsp;&nbsp;ansible\_facts['riak\_last\_node'] = host\_list[-1]  
&nbsp;&nbsp;&nbsp;&nbsp;ansible\_facts['riak\_node\_count'] = len(host\_list)  
&nbsp;&nbsp;&nbsp;&nbsp;changed = True

&nbsp;&nbsp;&nbsp;&nbsp;result = {"changed": changed,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_facts": ansible\_facts }  
&nbsp;&nbsp;&nbsp;&nbsp;module.exit\_json(\*\*result)

Then from your module you can directly access your custom facts like:

$riak\_primary\_node  
$riak\_node\_count

etc.

This is all documented [http://ansible.cc/docs/moduledev.htm](http://ansible.cc/docs/moduledev.htm) under "Module  
Provided 'Facts'.

- James

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [December 29, 2012, 5:48pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/7 "2012-12-29T17:48:31Z")

</div>

This is super minor, but I figured I'd share... there's no need to  
return a 'changed' attribute in a facts module.

A changed = True/False only is really useful if you have a 'notify'  
hanging off of the task and the module makes changes to the underlying  
system.

It implies that the module has actually /changed/ the underlying  
system, so you wouldn't want to return it unless that were actually  
the case.

--Michael

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 29, 2012, 6:52pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/8 "2012-12-29T18:52:46Z")

</div>

2 small changes, added a path to lspci and made it non critical if it  
isn't isntalled.

&nbsp;&nbsp;&nbsp;&nbsp;pcidata = None  
&nbsp;&nbsp;&nbsp;&nbsp;try:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = Popen(["/usr/sbin/lspci"], stdout=PIPE)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = p.wait()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if err:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Error running lspci"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcidata = p.stdout.read()  
&nbsp;&nbsp;&nbsp;&nbsp;except OSError:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass # no lspci

works nicely for me now

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [December 29, 2012, 8:21pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/9 "2012-12-29T20:21:12Z")

</div>

Thanks for all the feedback, On my Centos 6.3 machine lspci is located in /sbin/lspci  
So i changed it to:

pcidata = None  
try:  
try:  
p = Popen([“/sbin/lspci”], stdout=PIPE)  
except:  
try:  
p = Popen([“/usr/sbin/lspci”], stdout=PIPE)  
except:  
p = Popen([“lspci”], stdout=PIPE)  
err = p.wait()  
if err:  
print “Error running lspci”  
else:  
pcidata = p.stdout.read()  
except OSError, e:  
module.fail\_json( msg=“lspci failed %s” % e) # no lspci

[https://www.filepicker.io/api/file/nsajpMMqSu2AIjPGC3lA](https://www.filepicker.io/api/file/nsajpMMqSu2AIjPGC3lA)

This is the modified code based on the feedback received, Let me know if i can make it better .

Out put:

s2 | success \>\> {  
“ansible\_facts”: {  
“device\_details”: {  
“sda”: {  
“disk type”: “Spinning disk”,  
“host”: “SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family SATA AHCI Controller (rev 05)”,  
“model”: “ST500DM002-1BC14”,  
“removable”: “No”,  
“sda1”: {  
“sectors”: “1024000”,  
“size”: “500.00MiB”,  
“start sector”: “2048”  
},  
“sda2”: {  
“holders”: [  
“VolGroup00-LogVol01”,  
“VolGroup00-LogVol00”  
],  
“sectors”: “975747072”,  
“size”: “465.27GiB”,  
“start sector”: “1026048”  
},  
“sector size(bytes)”: “512”,  
“sectors”: “976773168”,  
“size”: “465.76GiB”,  
“support discard”: “No”,  
“vendor”: “ATA”  
},  
“sdb”: {  
“disk type”: “SSD”,  
“host”: “SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family SATA AHCI Controller (rev 05)”,  
“model”: “SAMSUNG SSD 830”,  
“removable”: “No”,  
“sector size(bytes)”: “512”,  
“sectors”: “395093371”,  
“size”: “188.40GiB”,  
“support discard”: “Yes”,  
“vendor”: “ATA”  
},  
“sdc”: {  
“disk type”: “SSD”,  
“host”: “SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family SATA AHCI Controller (rev 05)”,  
“model”: “SAMSUNG SSD 830”,  
“removable”: “No”,  
“sector size(bytes)”: “512”,  
“sectors”: “395093371”,  
“size”: “188.40GiB”,  
“support discard”: “Yes”,  
“vendor”: “ATA”  
}  
},  
“devices”: [  
“sda”,  
“sdb”,  
“sdc”  
]  
},  
“changed”: false  
}

Regards,–  
Kavin Kankeshwar

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [December 29, 2012, 9:19pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/10 "2012-12-29T21:19:21Z")

</div>

Small request.

For those sharing larger pieces of files on this mailing list, I'd  
recommend using something like github's gist versus a file download  
site like filepicker.

It's much easier to tell what you are looking at before you download  
something potentially unsafe.

Pastebin is somewhat less tolerable as content tends to expire.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 29, 2012, 10:38pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/11 "2012-12-29T22:38:10Z")

</div>

A small patch that puts partitions in their own key and also adds  
mount information

--- Downloads/get\_devices 2012-12-29 17:36:50.162819361 -0500  
+++ get\_devices 2012-12-29 17:37:07.014825267 -0500  
@@ -278,6 +278,18 @@  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a\_devices= {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device\_names=  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dd={}

---

<div class="post-metadata">

**Author:** ![Kavin](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kavin](https://forum.ansible.com/u/Kavin)\
**Post date:** [December 30, 2012, 9:00am UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/12 "2012-12-30T09:00:49Z")

</div>

Thanks for all the help, Sorry about sending files to the group…

[https://github.com/kavink/ansible/blob/devel/library/get\_devices](https://github.com/kavink/ansible/blob/devel/library/get_devices)

Now I also get the scheduler-mode for the devices.

Regards,–  
Kavin Kankeshwar

---

<div class="post-metadata">

**Author:** ![Stephen\_Fromm](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Stephen\_Fromm](https://forum.ansible.com/u/Stephen_Fromm)\
**Post date:** [December 31, 2012, 4:57pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/13 "2012-12-31T16:57:17Z")

</div>

You may find get\_bin\_path() in module\_common.py useful when searching for the full path to lcpsi:

[https://github.com/ansible/ansible/blob/devel/lib/ansible/module\_common.py#L560](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_common.py#L560)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 31, 2012, 5:06pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/14 "2012-12-31T17:06:06Z")

</div>

i had a minor version of this hacked into setup, should I attempt to  
put this version in (under linux facts)? I would also add a freeBSD  
version

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [December 31, 2012, 5:24pm UTC](https://forum.ansible.com/t/using-ansible-facts-to-get-devices-information/12170/15 "2012-12-31T17:24:40Z")

</div>

> i had a minor version of this hacked into setup, should I attempt to  
> put this version in (under linux facts)? I would also add a freeBSD  
> version

Sounds good!

--Michael
