2014-12-31 12:59:40 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-12-31 13:48:59 +01:00
|
|
|
# Copyright 2014 Peter Oliver <ansible@mavit.org.uk>
|
|
|
|
#
|
|
|
|
# This program 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.
|
|
|
|
#
|
|
|
|
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-12-31 12:59:40 +01:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: pkg5
|
2015-05-15 21:12:26 +02:00
|
|
|
author: '"Peter Oliver (@mavit)" <ansible@mavit.org.uk>'
|
2014-12-31 12:59:40 +01:00
|
|
|
short_description: Manages packages with the Solaris 11 Image Packaging System
|
2015-02-13 14:20:12 +01:00
|
|
|
version_added: 1.9
|
2014-12-31 12:59:40 +01:00
|
|
|
description:
|
|
|
|
- IPS packages are the native packages in Solaris 11 and higher.
|
|
|
|
notes:
|
2015-01-25 16:42:52 +01:00
|
|
|
- The naming of IPS packages is explained at U(http://www.oracle.com/technetwork/articles/servers-storage-admin/ips-package-versioning-2232906.html).
|
2014-12-31 12:59:40 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- An FRMI of the package(s) to be installed/removed/updated.
|
2015-01-25 16:42:52 +01:00
|
|
|
- Multiple packages may be specified, separated by C(,).
|
2014-12-31 12:59:40 +01:00
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
2015-01-25 16:42:52 +01:00
|
|
|
- Whether to install (I(present), I(latest)), or remove (I(absent)) a
|
2014-12-31 12:59:40 +01:00
|
|
|
package.
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: [ present, latest, absent ]
|
2015-04-04 20:27:37 +02:00
|
|
|
accept_licenses:
|
|
|
|
description:
|
|
|
|
- Accept any licences.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
choices: [ true, false ]
|
|
|
|
aliases: [ accept_licences, accept ]
|
2014-12-31 12:59:40 +01:00
|
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Install Vim:
|
|
|
|
- pkg5: name=editor/vim
|
|
|
|
|
|
|
|
# Remove finger daemon:
|
|
|
|
- pkg5: name=service/network/finger state=absent
|
2015-01-26 22:11:38 +01:00
|
|
|
|
|
|
|
# Install several packages at once:
|
|
|
|
- pkg5:
|
|
|
|
name:
|
|
|
|
- /file/gnu-findutils
|
|
|
|
- /text/gnu-grep
|
2014-12-31 12:59:40 +01:00
|
|
|
'''
|
|
|
|
|
2014-12-31 14:27:21 +01:00
|
|
|
|
2014-12-31 12:59:40 +01:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(required=True, type='list'),
|
|
|
|
state=dict(
|
|
|
|
default='present',
|
|
|
|
choices=[
|
|
|
|
'present',
|
|
|
|
'installed',
|
|
|
|
'latest',
|
|
|
|
'absent',
|
|
|
|
'uninstalled',
|
|
|
|
'removed',
|
|
|
|
]
|
|
|
|
),
|
2015-04-04 20:27:37 +02:00
|
|
|
accept_licenses=dict(
|
|
|
|
choices=BOOLEANS,
|
2015-04-20 20:52:31 +02:00
|
|
|
default=False,
|
|
|
|
aliases=['accept_licences', 'accept'],
|
2015-04-04 20:27:37 +02:00
|
|
|
),
|
2014-12-31 12:59:40 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
params = module.params
|
2014-12-31 14:27:21 +01:00
|
|
|
packages = []
|
|
|
|
|
|
|
|
# pkg(5) FRMIs include a comma before the release number, but
|
|
|
|
# AnsibleModule will have split this into multiple items for us.
|
|
|
|
# Try to spot where this has happened and fix it.
|
|
|
|
for fragment in params['name']:
|
|
|
|
if (
|
|
|
|
re.search('^\d+(?:\.\d+)*', fragment)
|
|
|
|
and packages and re.search('@[^,]*$', packages[-1])
|
|
|
|
):
|
|
|
|
packages[-1] += ',' + fragment
|
|
|
|
else:
|
|
|
|
packages.append(fragment)
|
|
|
|
|
2014-12-31 12:59:40 +01:00
|
|
|
if params['state'] in ['present', 'installed']:
|
2015-04-04 20:27:37 +02:00
|
|
|
ensure(module, 'present', packages, params)
|
2014-12-31 12:59:40 +01:00
|
|
|
elif params['state'] in ['latest']:
|
2015-04-04 20:27:37 +02:00
|
|
|
ensure(module, 'latest', packages, params)
|
2014-12-31 12:59:40 +01:00
|
|
|
elif params['state'] in ['absent', 'uninstalled', 'removed']:
|
2015-04-04 20:27:37 +02:00
|
|
|
ensure(module, 'absent', packages, params)
|
2014-12-31 12:59:40 +01:00
|
|
|
|
|
|
|
|
2015-04-04 20:27:37 +02:00
|
|
|
def ensure(module, state, packages, params):
|
2014-12-31 12:59:40 +01:00
|
|
|
response = {
|
|
|
|
'results': [],
|
|
|
|
'msg': '',
|
|
|
|
}
|
|
|
|
behaviour = {
|
|
|
|
'present': {
|
|
|
|
'filter': lambda p: not is_installed(module, p),
|
|
|
|
'subcommand': 'install',
|
|
|
|
},
|
|
|
|
'latest': {
|
|
|
|
'filter': lambda p: not is_latest(module, p),
|
|
|
|
'subcommand': 'install',
|
|
|
|
},
|
|
|
|
'absent': {
|
|
|
|
'filter': lambda p: is_installed(module, p),
|
|
|
|
'subcommand': 'uninstall',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:36:15 +02:00
|
|
|
if params['accept_licenses']:
|
|
|
|
accept_licenses = ['--accept']
|
|
|
|
else:
|
|
|
|
accept_licenses = []
|
|
|
|
|
2014-12-31 12:59:40 +01:00
|
|
|
to_modify = filter(behaviour[state]['filter'], packages)
|
|
|
|
if to_modify:
|
|
|
|
rc, out, err = module.run_command(
|
2015-04-04 20:27:37 +02:00
|
|
|
[
|
|
|
|
'pkg', behaviour[state]['subcommand']
|
|
|
|
]
|
2015-05-08 23:36:15 +02:00
|
|
|
+ accept_licenses
|
2015-04-04 20:27:37 +02:00
|
|
|
+ [
|
|
|
|
'-q', '--'
|
|
|
|
] + to_modify
|
2014-12-31 12:59:40 +01:00
|
|
|
)
|
|
|
|
response['rc'] = rc
|
|
|
|
response['results'].append(out)
|
|
|
|
response['msg'] += err
|
|
|
|
response['changed'] = True
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(**response)
|
|
|
|
|
|
|
|
module.exit_json(**response)
|
|
|
|
|
|
|
|
|
|
|
|
def is_installed(module, package):
|
|
|
|
rc, out, err = module.run_command(['pkg', 'list', '--', package])
|
2015-05-08 23:36:15 +02:00
|
|
|
return not bool(int(rc))
|
2014-12-31 12:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_latest(module, package):
|
|
|
|
rc, out, err = module.run_command(['pkg', 'list', '-u', '--', package])
|
2015-05-08 23:36:15 +02:00
|
|
|
return bool(int(rc))
|
2014-12-31 12:59:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|