diff options
author | Scott Dodson <sdodson@redhat.com> | 2016-07-27 14:20:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-27 14:20:44 -0400 |
commit | 945403f494197b63a41a4503672f15198a6c17a1 (patch) | |
tree | cb7560badfeb97a41bceca815b3558a715bab01b /library/rpm_q.py | |
parent | de664ef0826c816b5e9dd7bee835d05a1724a42e (diff) | |
parent | f834279d5010f5a8b1a1cd7c75adaa7b0dce7fed (diff) | |
download | openshift-945403f494197b63a41a4503672f15198a6c17a1.tar.gz openshift-945403f494197b63a41a4503672f15198a6c17a1.tar.bz2 openshift-945403f494197b63a41a4503672f15198a6c17a1.tar.xz openshift-945403f494197b63a41a4503672f15198a6c17a1.zip |
Merge pull request #963 from ibotty/rpm_q-module
add rpm_q module to query rpm database
Diffstat (limited to 'library/rpm_q.py')
-rw-r--r-- | library/rpm_q.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/library/rpm_q.py b/library/rpm_q.py new file mode 100644 index 000000000..ca3d0dd89 --- /dev/null +++ b/library/rpm_q.py @@ -0,0 +1,70 @@ +#!/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 * + +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( + 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() |