zypper 6.64 KB
Newer Older
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
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-

# (c) 2013, Patrick Callahan <pmc@patrickcallahan.com>
# based on
#     openbsd_pkg
#         (c) 2013
#         Patrik Lundin <patrik.lundin.swe@gmail.com>
#
#     yum
#         (c) 2012, Red Hat, Inc
#         Written by Seth Vidal <skvidal at fedoraproject.org>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

import re

DOCUMENTATION = '''
---
module: zypper
author: Patrick Callahan
35
version_added: "1.2"
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
short_description: Manage packages on SuSE and openSuSE
description:
    - Manage packages on SuSE and openSuSE using the zypper and rpm tools.
options:
    name:
        description:
        - package name or package specifier wth version C(name) or C(name-1.0).
        required: true
        aliases: [ 'pkg' ]
    state:
        description:
          - C(present) will make sure the package is installed.
            C(latest)  will make sure the latest version of the package is installed.
            C(absent)  will make sure the specified package is not installed.
        required: false
        choices: [ present, latest, absent ]
        default: "present"
    disable_gpg_check:
        description:
          - Whether to disable to GPG signature checking of the package
            signature being installed. Has an effect only if state is
            I(present) or I(latest).
        required: false
        default: "no"
        choices: [ "yes", "no" ]
        aliases: []

notes: []
# informational: requirements for nodes
requirements: [ zypper, rpm ]
author: Patrick Callahan
'''

69 70 71 72 73 74 75 76
EXAMPLES = '''
# Install "nmap"
- zypper: name=nmap state=present

# Remove the "nmap" package
- zypper: name=nmap state=absent
'''

77 78 79
# Function used for getting the name of a currently installed package.
def get_current_name(m, name):
    cmd = '/bin/rpm -q --qf \'%{NAME}-%{VERSION}\''
80
    (rc, stdout, stderr) = m.run_command("%s %s" % (cmd, name))
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    if rc != 0:
        return (rc, stdout, stderr)

    syntax = "%s"

    for line in stdout.splitlines():
        if syntax % name in line:
            current_name = line.split()[0]

    return current_name

# Function used to find out if a package is currently installed.
def get_package_state(m, name):
    cmd = ['/bin/rpm', '--query', '--info', name]

    rc, stdout, stderr = m.run_command(cmd, check_rc=False)

    if rc == 0:
        return True
    else:
        return False

# Function used to make sure a package is present.
def package_present(m, name, installed_state, disable_gpg_check):
    if installed_state is False:
        cmd = ['/usr/bin/zypper', '--non-interactive']
        # add global options before zypper command
        if disable_gpg_check:
            cmd.append('--no-gpg-check')

        cmd.extend(['install', '--auto-agree-with-licenses'])
        cmd.append(name)
        rc, stdout, stderr = m.run_command(cmd, check_rc=False)

        if rc == 0:
            changed=True
        else:
            changed=False
    else:
        rc = 0
        stdout = ''
        stderr = ''
        changed=False

    return (rc, stdout, stderr, changed)

# Function used to make sure a package is the latest available version.
def package_latest(m, name, installed_state, disable_gpg_check):

    if installed_state is True:
        cmd = ['/usr/bin/zypper', '--non-interactive', 'update', '--auto-agree-with-licenses', name]
        pre_upgrade_name = ''
        post_upgrade_name = ''

        # Compare the installed package before and after to know if we changed anything.
        pre_upgrade_name = get_current_name(m, name)

        rc, stdout, stderr = m.run_command(cmd, check_rc=False)

        post_upgrade_name = get_current_name(m, name)

        if pre_upgrade_name == post_upgrade_name:
            changed = False
        else:
            changed = True

        return (rc, stdout, stderr, changed)

    else:
        # If package was not installed at all just make it present.
        return package_present(m, name, installed_state, disable_gpg_check)

# Function used to make sure a package is not installed.
def package_absent(m, name, installed_state):
    if installed_state is True:
        cmd = ['/usr/bin/zypper', '--non-interactive', 'remove', name]
        rc, stdout, stderr = m.run_command(cmd)

        if rc == 0:
            changed=True
        else:
            changed=False
    else:
        rc = 0
        stdout = ''
        stderr = ''
        changed=False

    return (rc, stdout, stderr, changed)

# ===========================================
# Main control flow

def main():
    module = AnsibleModule(
        argument_spec = dict(
            name = dict(required=True, aliases=['pkg']),
            state = dict(required=False, default='present', choices=['absent', 'installed', 'latest', 'present', 'removed']),
            disable_gpg_check = dict(required=False, default='no', choices=BOOLEANS, type='bool'),
        ),
        supports_check_mode = False
    )


    params = module.params

    name  = params['name']
    state = params['state']
    disable_gpg_check = params['disable_gpg_check']

    rc = 0
    stdout = ''
    stderr = ''
    result = {}
    result['name'] = name
    result['state'] = state

    # Decide if the name contains a version number.
    match = re.search("-[0-9]", name)
    if match:
        specific_version = True
    else:
        specific_version = False

    # Get package state
    installed_state = get_package_state(module, name)

    # Perform requested action
    if state in ['installed', 'present']:
        (rc, stdout, stderr, changed) = package_present(module, name, installed_state, disable_gpg_check)
    elif state in ['absent', 'removed']:
        (rc, stdout, stderr, changed) = package_absent(module, name, installed_state)
    elif state == 'latest':
        (rc, stdout, stderr, changed) = package_latest(module, name, installed_state, disable_gpg_check)

    if rc != 0:
        if stderr:
            module.fail_json(msg=stderr)
        else:
            module.fail_json(msg=stdout)

    result['changed'] = changed

    module.exit_json(**result)

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()