summaryrefslogtreecommitdiffstats
path: root/roles/lib_utils/library/rpm_q.py
blob: 3dec50fc21464572aa42562e707ee1caaa563947 (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
68
69
70
71
72
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2015, Tobias Florek <tob@butter.sh>
# Licensed under the terms of the MIT License
"""
An ansible module to query the RPM database. For use, when yum/dnf are not
available.
"""

# pylint: disable=redefined-builtin,wildcard-import,unused-wildcard-import
from ansible.module_utils.basic import *  # noqa: F403

DOCUMENTATION = """
---
module: rpm_q
short_description: Query the RPM database
author: Tobias Florek
options:
  name:
    description:
    - The name of the package to query
    required: true
  state:
    description:
    - Whether the package is supposed to be installed or not
    choices: [present, absent]
    default: present
"""

EXAMPLES = """
- rpm_q: name=ansible state=present
- rpm_q: name=ansible state=absent
"""

RPM_BINARY = '/bin/rpm'


def main():
    """
    Checks rpm -q for the named package and returns the installed packages
    or None if not installed.
    """
    module = AnsibleModule(  # noqa: F405
        argument_spec=dict(
            name=dict(required=True),
            state=dict(default='present', choices=['present', 'absent'])
        ),
        supports_check_mode=True
    )

    name = module.params['name']
    state = module.params['state']

    # pylint: disable=invalid-name
    rc, out, err = module.run_command([RPM_BINARY, '-q', name])

    installed = out.rstrip('\n').split('\n')

    if rc != 0:
        if state == 'present':
            module.fail_json(msg="%s is not installed" % name, stdout=out, stderr=err, rc=rc)
        else:
            module.exit_json(changed=False)
    elif state == 'present':
        module.exit_json(changed=False, installed_versions=installed)
    else:
        module.fail_json(msg="%s is installed", installed_versions=installed)


if __name__ == '__main__':
    main()