# Doing a diff between two device's facts

**URL:** <https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417>\
**Category:** Ansible Project\
**Created:** [June 5, 2014, 9:56pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417 "2014-06-05T21:56:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Edelman](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason\_Edelman](https://forum.ansible.com/u/Jason_Edelman)\
**Post date:** [June 5, 2014, 9:56pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/1 "2014-06-05T21:56:23Z")

</div>

Hi,

I have a single playbook using one module to gather facts about ONE device and this works all good. Upon execution, I register a variable and display several debug messages. All good. I added a second host to the hosts file and I’m now getting good debug info on both devices.

My next task is to compare, or do a diff, on particular fact data from each device. This diff would happen by sending both variables (facts) to another custom module to analyze. I was going to do a single host per playbook and pass the registered variable to a 3rd playbook, but variables are only valid within the pb. As an alternative, I suppose I can write the contents of the registered variable to a file in the first pb and the read from that in the second playbook, then do the needed comparisons, but that just doesn’t seem right.

While this scenario is for custom facts, the same solution should hold true for the off the shelf facts too.

What would be the best approach for this? Any feedback would be greatly appreciated.

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [June 5, 2014, 11:08pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/2 "2014-06-05T23:08:28Z")

</div>

If you have a single play to gather facts on all hosts you can then have a second play within the same playbook that compares the facts for different hosts… This would work for runs with small numbers of hosts but might get a lot harder for larger quantities of hosts.

[http://docs.ansible.com/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

You could take the first host and assume that it is definitive and then walk through all of the other hosts comparing the facts that you care about and saying which ones differ. This would work for two hosts, and might work for more but if the odd one out was the first host then you would get a list of all other hosts as differing.

I hope that this helps,

Adam

---

<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:** [June 5, 2014, 11:17pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/3 "2014-06-05T23:17:37Z")

</div>

I’ve used “ansible -m setup -t ./tmp hosts” then used diff on the output json files (with a little vim magic reformatting).​

---

<div class="post-metadata">

**Author:** ![Jason\_Edelman](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason\_Edelman](https://forum.ansible.com/u/Jason_Edelman)\
**Post date:** [June 6, 2014, 2:52am UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/4 "2014-06-06T02:52:27Z")

</div>

Okay, got it. Have it working now. Just need to decide how I want to do diffs and/or analyze the data!

Thanks!  
Jason

---

<div class="post-metadata">

**Author:** ![Jason\_Edelman](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason\_Edelman](https://forum.ansible.com/u/Jason_Edelman)\
**Post date:** [June 6, 2014, 2:52am UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/5 "2014-06-06T02:52:58Z")

</div>

Thanks, Brian. Have an alternative solution working now. May try this one later too.

Thanks for the response.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@Brent\_Langston](https://forum.ansible.com/u/Brent_Langston)\
**Post date:** [June 6, 2014, 4:21pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/6 "2014-06-06T16:21:37Z")

</div>

secrets don't make friends! mind closing the loop by sharing what you came  
up with?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 8, 2014, 3:33pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/7 "2014-06-08T15:33:24Z")

</div>

Hmm, if you are trying to build a “how are these various servers different” kind of solution in Ansible, I’ll say that doesn’t really exist yet, so what you are going to create if attempting to use ansible as a programming language (it’s not) may feel a little hackish.

---

<div class="post-metadata">

**Author:** ![Jason\_Edelman](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason\_Edelman](https://forum.ansible.com/u/Jason_Edelman)\
**Post date:** [June 8, 2014, 3:53pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/8 "2014-06-08T15:53:37Z")

</div>

Brent, sorry! Here is what I have working right now.  
Michael et al, feel free to comment on this “hack” and let me know what else could be done to simplify.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 8, 2014, 4:03pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/9 "2014-06-08T16:03:17Z")

</div>

If you’re trying to make ansible make reports, rather than doing the debug calls, _maybe_ you would want to make a template for the report instead.

Just a thought…

---

<div class="post-metadata">

**Author:** ![Jason\_Edelman](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason\_Edelman](https://forum.ansible.com/u/Jason_Edelman)\
**Post date:** [June 8, 2014, 8:32pm UTC](https://forum.ansible.com/t/doing-a-diff-between-two-devices-facts/15417/10 "2014-06-08T20:32:56Z")

</div>

Great suggestion. Templates will work great for storing the results. Displaying them in real-time may still be beneficial.

Thanks.
