2013-06-07 03:57:32 +02:00
|
|
|
#!/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
|
2015-07-23 13:45:35 +02:00
|
|
|
author:
|
|
|
|
- "Patrick Callahan (@dirtyharrycallahan)"
|
|
|
|
- "Alexander Gubin (@alxgu)"
|
2014-06-25 14:59:30 +02:00
|
|
|
version_added: "1.2"
|
2014-11-10 17:42:43 +01:00
|
|
|
short_description: Manage packages on SUSE and openSUSE
|
2013-06-07 03:57:32 +02:00
|
|
|
description:
|
2014-11-10 17:42:43 +01:00
|
|
|
- Manage packages on SUSE and openSUSE using the zypper and rpm tools.
|
2013-06-07 03:57:32 +02:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2015-07-23 13:45:35 +02:00
|
|
|
- package name or package specifier with version C(name) or C(name-1.0). You can also pass a url or a local path to a rpm file.
|
2013-06-07 03:57:32 +02:00
|
|
|
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"
|
2015-04-01 20:44:05 +02:00
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- The type of package to be operated on.
|
|
|
|
required: false
|
|
|
|
choices: [ package, patch, pattern, product, srcpackage ]
|
|
|
|
default: "package"
|
2015-04-16 15:45:23 +02:00
|
|
|
version_added: "2.0"
|
2013-06-07 03:57:32 +02:00
|
|
|
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: []
|
2014-05-13 15:20:40 +02:00
|
|
|
disable_recommends:
|
2014-08-08 19:42:11 +02:00
|
|
|
version_added: "1.8"
|
2014-05-13 15:20:40 +02:00
|
|
|
description:
|
|
|
|
- Corresponds to the C(--no-recommends) option for I(zypper). Default behavior (C(yes)) modifies zypper's default behavior; C(no) does install recommended packages.
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-06-07 03:57:32 +02:00
|
|
|
|
|
|
|
notes: []
|
|
|
|
# informational: requirements for nodes
|
|
|
|
requirements: [ zypper, rpm ]
|
|
|
|
author: Patrick Callahan
|
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Install "nmap"
|
|
|
|
- zypper: name=nmap state=present
|
|
|
|
|
2014-05-13 15:20:40 +02:00
|
|
|
# Install apache2 with recommended packages
|
|
|
|
- zypper: name=apache2 state=present disable_recommends=no
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
# Remove the "nmap" package
|
|
|
|
- zypper: name=nmap state=absent
|
2015-07-23 13:45:35 +02:00
|
|
|
|
|
|
|
# Install the nginx rpm from a remote repo
|
|
|
|
- zypper: name=http://nginx.org/packages/sles/12/x86_64/RPMS/nginx-1.8.0-1.sles12.ngx.x86_64.rpm state=present
|
|
|
|
|
|
|
|
# Install local rpm file
|
|
|
|
- zypper: name=/tmp/fancy-software.rpm state=present
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2014-10-10 19:51:44 +02:00
|
|
|
# Function used for getting zypper version
|
|
|
|
def zypper_version(module):
|
|
|
|
"""Return (rc, message) tuple"""
|
|
|
|
cmd = ['/usr/bin/zypper', '-V']
|
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
if rc == 0:
|
|
|
|
return rc, stdout
|
|
|
|
else:
|
|
|
|
return rc, stderr
|
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
# Function used for getting versions of currently installed packages.
|
2014-12-04 11:28:18 +01:00
|
|
|
def get_current_version(m, packages):
|
2014-09-02 15:38:30 +02:00
|
|
|
cmd = ['/bin/rpm', '-q', '--qf', '%{NAME} %{VERSION}-%{RELEASE}\n']
|
2014-12-04 11:25:06 +01:00
|
|
|
cmd.extend(packages)
|
|
|
|
|
2014-12-04 11:28:18 +01:00
|
|
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
2014-09-02 15:38:30 +02:00
|
|
|
|
|
|
|
current_version = {}
|
|
|
|
rpmoutput_re = re.compile('^(\S+) (\S+)$')
|
2014-12-04 11:25:06 +01:00
|
|
|
|
|
|
|
for stdoutline in stdout.splitlines():
|
|
|
|
match = rpmoutput_re.match(stdoutline)
|
|
|
|
if match == None:
|
2014-09-02 15:38:30 +02:00
|
|
|
return None
|
2014-12-04 11:25:06 +01:00
|
|
|
package = match.group(1)
|
|
|
|
version = match.group(2)
|
|
|
|
current_version[package] = version
|
|
|
|
|
|
|
|
for package in packages:
|
|
|
|
if package not in current_version:
|
|
|
|
print package + ' was not returned by rpm \n'
|
2014-09-02 15:38:30 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
return current_version
|
2013-06-07 03:57:32 +02:00
|
|
|
|
2014-12-04 11:25:06 +01:00
|
|
|
|
2013-06-07 03:57:32 +02:00
|
|
|
# Function used to find out if a package is currently installed.
|
2014-09-02 15:38:30 +02:00
|
|
|
def get_package_state(m, packages):
|
2015-07-23 13:45:35 +02:00
|
|
|
for i in range(0, len(packages)):
|
|
|
|
# Check state of a local rpm-file
|
|
|
|
if ".rpm" in packages[i]:
|
|
|
|
# Check if rpm file is available
|
|
|
|
package = packages[i]
|
|
|
|
if not os.path.isfile(package) and not '://' in package:
|
|
|
|
stderr = "No Package file matching '%s' found on system" % package
|
|
|
|
m.fail_json(msg=stderr)
|
|
|
|
# Get packagename from rpm file
|
|
|
|
cmd = ['/bin/rpm', '--query', '--qf', '%{NAME}', '--package']
|
|
|
|
cmd.append(package)
|
|
|
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
|
|
|
packages[i] = stdout
|
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
cmd = ['/bin/rpm', '--query', '--qf', 'package %{NAME} is installed\n']
|
|
|
|
cmd.extend(packages)
|
2013-06-07 03:57:32 +02:00
|
|
|
|
|
|
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
installed_state = {}
|
|
|
|
rpmoutput_re = re.compile('^package (\S+) (.*)$')
|
2014-12-04 11:25:06 +01:00
|
|
|
for stdoutline in stdout.splitlines():
|
|
|
|
match = rpmoutput_re.match(stdoutline)
|
|
|
|
if match == None:
|
2015-11-11 11:57:11 +01:00
|
|
|
continue
|
2014-12-04 11:25:06 +01:00
|
|
|
package = match.group(1)
|
|
|
|
result = match.group(2)
|
2014-09-02 15:38:30 +02:00
|
|
|
if result == 'is installed':
|
2014-12-04 11:25:06 +01:00
|
|
|
installed_state[package] = True
|
2014-09-02 15:38:30 +02:00
|
|
|
else:
|
2014-12-04 11:25:06 +01:00
|
|
|
installed_state[package] = False
|
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
return installed_state
|
2013-06-07 03:57:32 +02:00
|
|
|
|
|
|
|
# Function used to make sure a package is present.
|
2015-04-01 20:44:05 +02:00
|
|
|
def package_present(m, name, installed_state, package_type, disable_gpg_check, disable_recommends, old_zypper):
|
2014-09-02 15:38:30 +02:00
|
|
|
packages = []
|
|
|
|
for package in name:
|
2015-11-11 11:57:11 +01:00
|
|
|
if package not in installed_state or installed_state[package] is False:
|
2014-09-02 15:38:30 +02:00
|
|
|
packages.append(package)
|
|
|
|
if len(packages) != 0:
|
2013-06-07 03:57:32 +02:00
|
|
|
cmd = ['/usr/bin/zypper', '--non-interactive']
|
|
|
|
# add global options before zypper command
|
2014-12-16 13:34:46 +01:00
|
|
|
if disable_gpg_check:
|
2014-10-10 19:51:44 +02:00
|
|
|
cmd.append('--no-gpg-checks')
|
2015-04-01 20:44:05 +02:00
|
|
|
cmd.extend(['install', '--auto-agree-with-licenses', '-t', package_type])
|
2014-05-13 15:20:40 +02:00
|
|
|
# add install parameter
|
2014-10-10 19:51:44 +02:00
|
|
|
if disable_recommends and not old_zypper:
|
2014-05-13 15:20:40 +02:00
|
|
|
cmd.append('--no-recommends')
|
2014-09-02 15:38:30 +02:00
|
|
|
cmd.extend(packages)
|
2013-06-07 03:57:32 +02:00
|
|
|
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.
|
2015-04-01 20:44:05 +02:00
|
|
|
def package_latest(m, name, installed_state, package_type, disable_gpg_check, disable_recommends, old_zypper):
|
2013-06-07 03:57:32 +02:00
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
# first of all, make sure all the packages are installed
|
2015-04-01 20:44:05 +02:00
|
|
|
(rc, stdout, stderr, changed) = package_present(m, name, installed_state, package_type, disable_gpg_check, disable_recommends, old_zypper)
|
2013-06-07 03:57:32 +02:00
|
|
|
|
2015-04-02 19:04:56 +02:00
|
|
|
# return if an error occured while installation
|
|
|
|
# otherwise error messages will be lost and user doesn`t see any error
|
|
|
|
if rc:
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
# if we've already made a change, we don't have to check whether a version changed
|
|
|
|
if not changed:
|
|
|
|
pre_upgrade_versions = get_current_version(m, name)
|
2013-06-07 03:57:32 +02:00
|
|
|
|
2014-12-16 13:34:46 +01:00
|
|
|
cmd = ['/usr/bin/zypper', '--non-interactive']
|
|
|
|
|
|
|
|
if disable_gpg_check:
|
|
|
|
cmd.append('--no-gpg-checks')
|
|
|
|
|
2014-10-10 19:51:44 +02:00
|
|
|
if old_zypper:
|
2015-04-01 20:44:05 +02:00
|
|
|
cmd.extend(['install', '--auto-agree-with-licenses', '-t', package_type])
|
2014-10-10 19:51:44 +02:00
|
|
|
else:
|
2015-04-01 20:44:05 +02:00
|
|
|
cmd.extend(['update', '--auto-agree-with-licenses', '-t', package_type])
|
2014-12-16 13:34:46 +01:00
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
cmd.extend(name)
|
|
|
|
rc, stdout, stderr = m.run_command(cmd, check_rc=False)
|
2013-06-07 03:57:32 +02:00
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
# if we've already made a change, we don't have to check whether a version changed
|
|
|
|
if not changed:
|
|
|
|
post_upgrade_versions = get_current_version(m, name)
|
|
|
|
if pre_upgrade_versions != post_upgrade_versions:
|
2013-06-07 03:57:32 +02:00
|
|
|
changed = True
|
|
|
|
|
2014-09-02 15:38:30 +02:00
|
|
|
return (rc, stdout, stderr, changed)
|
2013-06-07 03:57:32 +02:00
|
|
|
|
|
|
|
# Function used to make sure a package is not installed.
|
2015-04-01 20:44:05 +02:00
|
|
|
def package_absent(m, name, installed_state, package_type, old_zypper):
|
2014-09-02 15:38:30 +02:00
|
|
|
packages = []
|
|
|
|
for package in name:
|
2015-11-11 11:57:11 +01:00
|
|
|
if package not in installed_state or installed_state[package] is True:
|
2014-09-02 15:38:30 +02:00
|
|
|
packages.append(package)
|
|
|
|
if len(packages) != 0:
|
2015-04-01 20:44:05 +02:00
|
|
|
cmd = ['/usr/bin/zypper', '--non-interactive', 'remove', '-t', package_type]
|
2014-09-02 15:38:30 +02:00
|
|
|
cmd.extend(packages)
|
2013-06-07 03:57:32 +02:00
|
|
|
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(
|
2014-09-02 15:38:30 +02:00
|
|
|
name = dict(required=True, aliases=['pkg'], type='list'),
|
2013-06-07 03:57:32 +02:00
|
|
|
state = dict(required=False, default='present', choices=['absent', 'installed', 'latest', 'present', 'removed']),
|
2015-04-01 20:44:05 +02:00
|
|
|
type = dict(required=False, default='package', choices=['package', 'patch', 'pattern', 'product', 'srcpackage']),
|
2013-10-11 14:45:13 +02:00
|
|
|
disable_gpg_check = dict(required=False, default='no', type='bool'),
|
2014-09-03 19:03:59 +02:00
|
|
|
disable_recommends = dict(required=False, default='yes', type='bool'),
|
2013-06-07 03:57:32 +02:00
|
|
|
),
|
|
|
|
supports_check_mode = False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
params = module.params
|
|
|
|
|
|
|
|
name = params['name']
|
|
|
|
state = params['state']
|
2015-04-01 20:44:05 +02:00
|
|
|
type_ = params['type']
|
2013-06-07 03:57:32 +02:00
|
|
|
disable_gpg_check = params['disable_gpg_check']
|
2014-05-13 15:20:40 +02:00
|
|
|
disable_recommends = params['disable_recommends']
|
2013-06-07 03:57:32 +02:00
|
|
|
|
|
|
|
rc = 0
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
result = {}
|
|
|
|
result['name'] = name
|
|
|
|
result['state'] = state
|
|
|
|
|
2014-10-10 19:51:44 +02:00
|
|
|
rc, out = zypper_version(module)
|
|
|
|
match = re.match(r'zypper\s+(\d+)\.(\d+)\.(\d+)', out)
|
|
|
|
if not match or int(match.group(1)) > 0:
|
|
|
|
old_zypper = False
|
|
|
|
else:
|
|
|
|
old_zypper = True
|
|
|
|
|
2013-06-07 03:57:32 +02:00
|
|
|
# Get package state
|
|
|
|
installed_state = get_package_state(module, name)
|
|
|
|
|
|
|
|
# Perform requested action
|
|
|
|
if state in ['installed', 'present']:
|
2015-04-01 20:44:05 +02:00
|
|
|
(rc, stdout, stderr, changed) = package_present(module, name, installed_state, type_, disable_gpg_check, disable_recommends, old_zypper)
|
2013-06-07 03:57:32 +02:00
|
|
|
elif state in ['absent', 'removed']:
|
2015-04-01 20:44:05 +02:00
|
|
|
(rc, stdout, stderr, changed) = package_absent(module, name, installed_state, type_, old_zypper)
|
2013-06-07 03:57:32 +02:00
|
|
|
elif state == 'latest':
|
2015-04-01 20:44:05 +02:00
|
|
|
(rc, stdout, stderr, changed) = package_latest(module, name, installed_state, type_, disable_gpg_check, disable_recommends, old_zypper)
|
2013-06-07 03:57:32 +02:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
if stderr:
|
|
|
|
module.fail_json(msg=stderr)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=stdout)
|
|
|
|
|
|
|
|
result['changed'] = changed
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-06-07 03:57:32 +02:00
|
|
|
main()
|