summaryrefslogtreecommitdiffstats
path: root/ansible/ans_rpm_compare.py
blob: e30a7dd923072db93bde1b9960d2bfdc5444bb29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python -tt
# skvidal - gpl blah blah blah
# compare 2 or more hosts rpm pkg lists and show differences between them


import ansible
import ansible.runner
import sys



def pkgs_from_rpmqa(rpm_out):
    ret = set([])
    for i in rpm_out.split('\n'):
        if i.strip():
            ret.add(i)
    return ret
    

if len(sys.argv) < 3:
   print "%s base_host other_host_specification_or_group [..]" % sys.argv[0]
   sys.exit(1)



basehost=sys.argv[1]
hostpattern = ';'.join(sys.argv[1:])
conn = ansible.runner.Runner(pattern=hostpattern, timeout=40)
conn.module_name='command'
conn.module_args='rpm -qa'

res = conn.run()

if len(res['contacted']) == 1:
    print >> sys.stderr, "Pattern resolved only one host - nothing to show"
    sys.exit(1)


if basehost not in res['contacted']:
    # taking first host in the list and calling it 'basehost'
    print >> sys.stderr, "Could not find %s in returned list - just using the first one we have" % basehost
    basehost = res['contacted'].keys()[0]

base_pkgs = pkgs_from_rpmqa(res['contacted'][basehost]['stdout'])

for host in res['contacted']:
    if host == basehost:
        continue
    host_pkgs = pkgs_from_rpmqa(res['contacted'][host]['stdout'])

    basediff = base_pkgs.difference(host_pkgs)
    hostdiff = host_pkgs.difference(base_pkgs)
    print 'Packages on %s not on %s' % (basehost, host)
    print '\n'.join(basediff)
    print ''
    print 'Packages on %s not on %s' % (host, basehost)
    print '\n'.join(hostdiff)
    print ''


if len(res['dark']):
    print >> sys.stderr, "Failed to connect to host(s)"
    for h in res['dark']:
        print h