Using Ansible to make an /proc like mount for your environment

All,

I made 2 scripts I wanted to show to you guys. Maybe someone finds them useful, and maybe someone has some suggestions on improving them (I am an engineer, not a developer, and this is the first time I am doing stuff with FUSE). The code is available on github.

After I saw how much data you can get “for free” (ie: no root required) with the setup module and how easy it was to use the Ansible module in python, I wanted to use these things to create a layout of my infrastructure independent of network location. What I came up with are 2 scripts, a ansible_fetcher.py that collects all the data provided by “ansible -m setup $host” as well as running optional custom commands and collecting their output. This is all saved in a json file. The second script, datamounter.py, can mount json files on your (linux) filesystem using fusepy. Optionally you can pass a --realtime parameter so that when a file gets opened (say, ansible_memfree), the data is updated using Ansible before the contents are shown.

Note that datamounter is not only useful in tandem with Ansible, I can imagine complex configurations being made available as a filesystem so you can easily grep through them or something…

In practice:

yoram@mac-air ~/DEV/Datamounter (master) $ ./ansible_fetcher.py --pattern my_hosts --retries 5 -f ./my_hosts.json
yoram@mac-air ~/DEV/Datamounter (master) $ ls -lh my_hosts.json
-rw-r–r-- 1 yoram staff 2.6M Feb 20 14:01 my_hosts.json
yoram@mac-air ~/DEV/Datamounter (master) $ ./datamounter.py --cache my_hosts.json ./Test
Loading data
done
yoram@mac-air ~/DEV/Datamounter (master*) $ mount
-------------------------- snip ---------------------------
DataFS on /Users/yoram/DEV/Datamounter/Test (osxfusefs, nodev, nosuid, read-only, synchronous, mounted by yoram)
yoram@mac-air ~/DEV/Datamounter (master*) $ ls Test
serverora220 serverora210 servermwf001 serverbeh003 serverweb008
serverora221 serverora211 servermwf002 serverbvb001 serverweb009
serverora222 serverora307 servermwf003 serverbvb002
-------------------------------------- etc ---------------------------------------
yoram@mac-air ~/DEV/Datamounter/Test (master*) $ cd Test
yoram@mac-air ~/DEV/Datamounter/Test (master*) $ find serverora220 | wc -l
216
yoram@mac-air ~/DEV/Datamounter/Test (master*) $ find serverora220 | head
serverora220
serverora220/ansible_all_ipv4_addresses
serverora220/ansible_all_ipv4_addresses/listitem_0
serverora220/ansible_all_ipv6_addresses
serverora220/ansible_architecture
serverora220/ansible_bios_date
serverora220/ansible_bios_version
serverora220/ansible_devices/sda
serverora220/ansible_devices/sda/holders
serverora220/ansible_devices/sda/host
serverora220/ansible_devices/sda/model
serverora220/ansible_devices/sda/partitions
serverora220/ansible_devices/sda/partitions/sda1
serverora220/ansible_devices/sda/partitions/sda1/sectors
serverora220/ansible_devices/sda/partitions/sda1/sectorsize
serverora220/ansible_devices/sda/partitions/sda1/size
serverora220/ansible_devices/sda/partitions/sda1/start
---------------------------- snip -------------------------------

Any input is more than welcome. I have to admit I am somewhat hesitant to post this because of my lack of experience, but as you see have made the plunge:)

Cheers,
Yoram

Note that fact caching with the jsonfile plugin already does the work
of the 1st script w/o the need to execute separately.

Yeah, thats true. But there are some additional options you can use with ansible_fetcher. For instance, if you want to mount stuff realtime, you need the json-value (to be translated to the filepath), but not really the content.
For instance, lets say I want a file named “sar” which always contains the output of “sar -Ap”. If I want to mount 100+ hosts, the resulting json file would be huge, but the actual data would not even be used (because when you access the “sar” file, the data gets fetched again anyway). In this case you can use --skeleton to clean up the datastructure before writing it out to the file.

Also I do some “sanitizing” to make some stuff easier to navigate when mounted.

But as stated, datamounter.py is in no way dependent on Ansible, I just find it an interesting usecase:)