2
# -*- coding: utf-8 -*-
4
# (c) 2013, berenddeboer
5
# Written by berenddeboer <berend@pobox.com>
6
# Based on pkgng module written by bleader <bleader at ratonland.org>
8
# This module is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This software is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this software. If not, see <http://www.gnu.org/licenses/>.
25
short_description: Installing packages from FreeBSD's ports system
27
- Manage packages for FreeBSD using 'portinstall'.
32
- name of package to install/remove
36
- state of the package
37
choices: [ 'present', 'absent' ]
42
- use packages instead of ports whenever available
43
choices: [ 'yes', 'no' ]
51
- portinstall: name=foo state=present
53
# Install package security/cyrus-sasl2-saslauthd
54
- portinstall: name=security/cyrus-sasl2-saslauthd state=present
56
# Remove packages foo and bar
57
- portinstall: name=foo,bar state=absent
66
def query_package(module, name):
68
pkg_info_path = module.get_bin_path('pkg_info', False)
70
# Assume that if we have pkg_info, we haven't upgraded to pkgng
73
pkg_glob_path = module.get_bin_path('pkg_glob', True)
74
rc, out, err = module.run_command("%s -e `pkg_glob %s`" % (pkg_info_path, name))
77
pkg_info_path = module.get_bin_path('pkg', True)
78
pkg_info_path = pkg_info_path + " info"
79
rc, out, err = module.run_command("%s %s" % (pkg_info_path, name))
84
# databases/mysql55-client installs as mysql-client, so try solving
85
# that the ugly way. Pity FreeBSD doesn't have a fool proof way of checking
86
# some package is installed
87
name_without_digits = re.sub('[0-9]', '', name)
88
if name != name_without_digits:
90
rc, out, err = module.run_command("%s %s" % (pkg_info_path, name_without_digits))
92
rc, out, err = module.run_command("%s `%s %s`" % (pkg_info_path, pkg_glob_path, name_without_digits))
99
def matching_packages(module, name):
101
ports_glob_path = module.get_bin_path('ports_glob', True)
102
rc, out, err = module.run_command("%s %s | wc" % (ports_glob_path, name))
104
occurrences = int(parts[0])
106
name_without_digits = re.sub('[0-9]', '', name)
107
if name != name_without_digits:
108
rc, out, err = module.run_command("%s %s | wc" % (ports_glob_path, name_without_digits))
110
occurrences = int(parts[0])
114
def remove_packages(module, packages):
117
pkg_glob_path = module.get_bin_path('pkg_glob', True)
119
# If pkg_delete not found, we assume pkgng
120
pkg_delete_path = module.get_bin_path('pkg_delete', False)
121
if not pkg_delete_path:
122
pkg_delete_path = module.get_bin_path('pkg', True)
123
pkg_delete_path = pkg_delete_path + " delete -y"
125
# Using a for loop incase of error, we can report the package that failed
126
for package in packages:
127
# Query the package first, to see if we even need to remove
128
if not query_package(module, package):
131
rc, out, err = module.run_command("%s `%s %s`" % (pkg_delete_path, pkg_glob_path, package))
133
if query_package(module, package):
134
name_without_digits = re.sub('[0-9]', '', package)
135
rc, out, err = module.run_command("%s `%s %s`" % (pkg_delete_path, pkg_glob_path, name_without_digits))
136
if query_package(module, package):
137
module.fail_json(msg="failed to remove %s: %s" % (package, out))
143
module.exit_json(changed=True, msg="removed %s package(s)" % remove_c)
145
module.exit_json(changed=False, msg="package(s) already absent")
148
def install_packages(module, packages, use_packages):
152
# If portinstall not found, automagically install
153
portinstall_path = module.get_bin_path('portinstall', False)
154
if not portinstall_path:
155
pkg_path = module.get_bin_path('pkg', False)
157
module.run_command("pkg install -y portupgrade")
158
portinstall_path = module.get_bin_path('portinstall', True)
160
if use_packages == "yes":
161
portinstall_params="--use-packages"
163
portinstall_params=""
165
for package in packages:
166
if query_package(module, package):
169
# TODO: check how many match
170
matches = matching_packages(module, package)
172
rc, out, err = module.run_command("%s --batch %s %s" % (portinstall_path, portinstall_params, package))
173
if not query_package(module, package):
174
module.fail_json(msg="failed to install %s: %s" % (package, out))
176
module.fail_json(msg="no matches for package %s" % (package))
178
module.fail_json(msg="%s matches found for package name %s" % (matches, package))
183
module.exit_json(changed=True, msg="present %s package(s)" % (install_c))
185
module.exit_json(changed=False, msg="package(s) already present")
189
module = AnsibleModule(
190
argument_spec = dict(
191
state = dict(default="present", choices=["present","absent"]),
192
name = dict(aliases=["pkg"], required=True),
193
use_packages = dict(type='bool', default='yes')))
197
pkgs = p["name"].split(",")
199
if p["state"] == "present":
200
install_packages(module, pkgs, p["use_packages"])
202
elif p["state"] == "absent":
203
remove_packages(module, pkgs)
205
# this is magic, see lib/ansible/module_common.py
206
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>