-
Notifications
You must be signed in to change notification settings - Fork 1
/
apt_versions
executable file
·76 lines (63 loc) · 1.94 KB
/
apt_versions
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
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class NotInstalled(Exception):
def __init__(self, arg):
self.args = arg
def main():
module = AnsibleModule(
argument_spec = dict(
name = dict(default='__all__'),
verbose = dict(default=True, choices=BOOLEANS),
)
)
changed = True
name = module.params['name']
try:
ansible_facts = {'apt_versions': get_facts(name)}
except KeyError:
module.fail_json(msg='Unknown package %s' % (name))
except NotInstalled:
module.fail_json(msg='Package %s is not installed' % (name))
except Exception, e:
module.fail_json(msg='Unknown error %s' % (e))
else:
module.exit_json(changed=changed, ansible_facts=ansible_facts, msg='Populated ansible_facts.apt_versions')
def get_facts(name):
c = apt.Cache()
apt_versions = {}
if name == '__all__':
packages = c
else:
packages = [c[name]]
count = 0
for p in packages:
if not p.is_installed:
if name == '__all__':
continue
else:
raise NotInstalled(name);
count += 1
version = p.installed.version
pieces = version.split('.')
# version = semantic_version.Version(p.installed.version)
if len(pieces) > 1:
print version
major = version.split('.')[0]
minor = version.split('.')[1]
simple = '%s.%s' % (major,minor)
else:
major = minor = simple = version
apt_versions[p.name] = {
'version': version,
'major': major,
'minor': minor,
'simple': simple
}
# add general installed package count
apt_versions['__meta__'] = { 'count': count }
return apt_versions
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
import apt
main()