Doing a diff between two device's facts

544 views
Skip to first unread message

Jason Edelman

unread,
Jun 5, 2014, 5:56:23 PM6/5/14
to ansible...@googlegroups.com
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

Adam Morris

unread,
Jun 5, 2014, 7:08:28 PM6/5/14
to ansible...@googlegroups.com
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.  


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
 

Brian Coca

unread,
Jun 5, 2014, 7:17:40 PM6/5/14
to ansible...@googlegroups.com
I've used "ansible -m setup -t ./tmp hosts" then used diff on the output json files (with a little vim magic reformatting).​

Jason Edelman

unread,
Jun 5, 2014, 10:52:31 PM6/5/14
to ansible...@googlegroups.com
Okay, got it.  Have it working now.  Just need to decide how I want to do diffs and/or analyze the data!

Thanks!
Jason


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/SzvQN2zex-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/471be03a-841b-419a-a1d3-0c0a7bde972f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jason Edelman

unread,
Jun 5, 2014, 10:53:04 PM6/5/14
to ansible...@googlegroups.com
Thanks, Brian.  Have an alternative solution working now.  May try this one later too.

Thanks for the response.


On Thu, Jun 5, 2014 at 7:17 PM, Brian Coca <bria...@gmail.com> wrote:
I've used "ansible -m setup -t ./tmp hosts" then used diff on the output json files (with a little vim magic reformatting).​

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/SzvQN2zex-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Brent Langston

unread,
Jun 6, 2014, 12:22:01 PM6/6/14
to ansible...@googlegroups.com

On Thu, Jun 5, 2014 at 7:52 PM, Jason Edelman <jede...@gmail.com> wrote:
 Have an alternative solution working now.

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

--------
Brent
--------

Michael DeHaan

unread,
Jun 8, 2014, 11:33:29 AM6/8/14
to ansible...@googlegroups.com
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.






--
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 post to this group, send email to ansible...@googlegroups.com.

Jason Edelman

unread,
Jun 8, 2014, 11:53:45 AM6/8/14
to ansible...@googlegroups.com
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.

---
#-----------------------PLAY 1 ------------------------------------------#

- name: collecting ospf data
  hosts: routers
  connection: local
  gather_facts: no

# note: routers only has two hosts/devices in it.  Basically, the two I want to compare

  tasks:

  - name: get ospf facts
    ospf_facts: device={{inventory_hostname}} interface={{ ospf_interface }}
    register: ospf_data

# just a bunch of debug messages displaying some facts on each device during the run

  - name: interface ip addresses used for OSPF peering
    debug: msg="local router interface IP address- {{ ospf_data.ofacts.oif_ip }} on {{ ospf_data.ofacts.oif }}"

  - name: is ospf active on interface?
    debug: msg="ospf active on interface = {{ ospf_data.ofacts.ospf_active_on_intf }}"

  - name: process id check
    debug: msg="at least one ospf process configurd on router = {{ ospf_data.ofacts.processExists }}"

  - name: MTUs of interfaces
    debug: msg="MTU = {{ ospf_data.ofacts.oif_mtu }}"

  - name: ospf network type
    debug: msg="network type = {{ ospf_data.ofacts.network_type }}"

  - name: ospf timers on interface
    debug: msg="{{ ospf_data.ofacts.timers }}"

  - name: interface status
    debug: msg="interface status={{ ospf_data.ofacts.interface_status.status }} and line protocol = {{ ospf_data.ofacts.interface_status.line_proto}}"

  - name: display neighbors and state
    debug: msg="{{ospf_data.ofacts.oif_neighbors}}"
    
#-----------------------PLAY 2 ------------------------------------------#

# local is just 127.0.0.1, so it runs just once

- name: ospf config validation & automated re-configuration
  hosts: local
  connection: local
  gather_facts: no

#to simplify a bit, using "root" as what the "other" device will be compared to...10.1.1.110 and 10.1.1.1.120 are both devices from routers in the first play
  vars:
    root: "{{ hostvars['10.1.1.110'].inventory_hostname }}"
    other: "{{ hostvars['10.1.1.120'].inventory_hostname }}"

# creating new short name variables to simplify the calling of them in this play. Contents of the vars file is below too.
  vars_files:
    - vars/simple_vars.yml

  tasks:

  - debug: msg="{{ root }} mtu = {{ r1_mtu }} && {{ other }} mtu = {{ r2_mtu }}"
  
  - debug: msg="*******MTU mismatch*******"
    when: r1_mtu != r2_mtu

  - debug: msg="*******MTUs match*********"
    when: r1_mtu == r2_mtu

  - name: auto re-config of MTU by increasing lower MTU to be equal to higher MTU value
    mtu: dev1={{ r1_ip }} dev2={{ r2_ip }} mtu1={{ r1_mtu }} mtu2={{ r2_mtu }} int1={{ r1_oif }} int2={{ r2_oif }}
    when: r1_mtu != r2_mtu


VARS FILE:
---
r1_mtu: "{{ hostvars[root].ospf_data.ofacts.oif_mtu }}"
r2_mtu: "{{ hostvars[other].ospf_data.ofacts.oif_mtu }}"
r1_ip: "{{ hostvars[root].inventory_hostname }}"
r2_ip: "{{ hostvars[other].inventory_hostname }}"
r1_oif: "{{ hostvars[root].ospf_data.ofacts.oif }}"
r2_oif: "{{ hostvars[other].ospf_data.ofacts.oif }}"


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/SzvQN2zex-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Jun 8, 2014, 12:03:23 PM6/8/14
to ansible...@googlegroups.com
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...




Jason Edelman

unread,
Jun 8, 2014, 4:32:59 PM6/8/14
to ansible...@googlegroups.com
Great suggestion.  Templates will work great for storing the results.  Displaying them in real-time may still be beneficial.

Thanks.


Reply all
Reply to author
Forward
0 new messages