summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2019-02-05 23:51:13 +0100
committerGuilhem Moulin <guilhem@fripost.org>2019-02-05 23:51:13 +0100
commitc19f6525465065496c485a5084a86707e4923580 (patch)
treeeca4439d3eb4fba5427dbae28f7a51e143af91e9
parentfc337924c7e66258319c6b6d538660240cfeda5e (diff)
Port custom modules to python3.
-rw-r--r--lib/modules/fetch_cmd.py4
-rw-r--r--lib/modules/mysql_user24
-rw-r--r--lib/modules/openldap10
-rw-r--r--lib/modules/postmap4
-rw-r--r--lib/modules/postmulti4
5 files changed, 13 insertions, 13 deletions
diff --git a/lib/modules/fetch_cmd.py b/lib/modules/fetch_cmd.py
index ac8757f..ca3e817 100644
--- a/lib/modules/fetch_cmd.py
+++ b/lib/modules/fetch_cmd.py
@@ -1,21 +1,21 @@
-#!/usr/bin/python
+#!/usr/bin/python3
# Fetch the output of a remote command
# Copyright (c) 2016 Guilhem Moulin <guilhem@fripost.org>
#
# 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/>.
# import module snippets
from ansible.module_utils.basic import *
@@ -31,26 +31,26 @@ def main():
)
params = module.params
cmd = params['cmd']
stdin = params['stdin']
dest = params['dest']
if cmd is None or dest is None:
return dict(failed=True, msg="cmd and dest are required")
changed = False
try:
if stdin is not None:
stdin = open(stdin, 'r')
with open(dest, 'w') as stdout:
subprocess.check_call(cmd.split(), stdin=stdin, stdout=stdout)
if stdin is not None:
stdin.close()
- except KeyError, e:
+ except KeyError as e:
module.fail_json(msg=str(e))
module.exit_json(changed=changed)
main()
diff --git a/lib/modules/mysql_user2 b/lib/modules/mysql_user2
index f2d9d40..20742fe 100644
--- a/lib/modules/mysql_user2
+++ b/lib/modules/mysql_user2
@@ -1,21 +1,21 @@
-#!/usr/bin/python
+#!/usr/bin/python3
# (c) 2012, Mark Theunissen <mark.theunissen@gmail.com>
# Sponsored by Four Kitchens http://fourkitchens.com.
#
# 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/>.
DOCUMENTATION = '''
@@ -455,40 +455,40 @@ def main():
mycnf_creds = load_mycnf()
if mycnf_creds is False:
login_user = "root"
login_password = ""
else:
login_user = mycnf_creds["user"]
login_password = mycnf_creds["passwd"]
elif login_password is None or login_user is None:
module.fail_json(msg="when supplying login arguments, both login_user and login_password must be provided")
cursor = None
try:
if check_implicit_admin:
try:
cursor = connect(module, 'root', '')
except:
pass
if not cursor:
cursor = connect(module, login_user, login_password)
- except Exception, e:
+ except Exception as e:
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials")
if state == "present":
if user_exists(cursor, user, host):
changed = user_mod(cursor, user, host, password, priv, append_privs, auth_plugin)
else:
if (password is None and auth_plugin is None) or (password is not None and auth_plugin is not None):
module.fail_json(msg="password xor auth_plugin is required when adding a user")
changed = user_add(cursor, user, host, password, priv, auth_plugin, soname)
elif state == "absent":
if user_exists(cursor, user, host):
changed = user_delete(cursor, user, host)
else:
changed = False
module.exit_json(changed=changed, user=user)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
diff --git a/lib/modules/openldap b/lib/modules/openldap
index 9b015b6..9afe1f1 100644
--- a/lib/modules/openldap
+++ b/lib/modules/openldap
@@ -1,21 +1,21 @@
-#!/usr/bin/python
+#!/usr/bin/python3
# Manage OpenLDAP databases
# Copyright (c) 2013 Guilhem Moulin <guilhem@fripost.org>
#
# 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/>.
import ldap, ldap.sasl
from ldap.filter import filter_format
from ldap.dn import dn2str,explode_dn,str2dn
@@ -286,41 +286,41 @@ def main():
delete = params['delete']
changed = False
try:
if delete is not None:
if name is None:
module.fail_json(msg="missing name")
l = ldap.initialize( 'ldapi://' )
l.sasl_interactive_bind_s('', ldap.sasl.external())
if delete == 'entry':
filterStr = '(objectClass=*)'
else:
filterStr = [ '(%s=*)' % x for x in delete.split(',') ]
if len(filterStr) > 1:
filterStr = '(|' + ''.join(filterStr) + ')'
else:
filterStr = filterStr[0]
try:
r = l.search_s( name, ldap.SCOPE_BASE, filterStr, attrsonly=1 )
- except ldap.LDAPError, ldap.NO_SUCH_OBJECT:
+ except (ldap.LDAPError, ldap.NO_SUCH_OBJECT):
r = None
if r:
changed = True
if module.check_mode:
module.exit_json(changed=changed)
if delete == 'entry':
l.delete_s(r[0][0])
else:
attrlist = list(set(r[0][1].keys()) & set(delete.split(',')))
l.modify_s(r[0][0], [ (ldap.MOD_DELETE, x, None) for x in attrlist ])
l.unbind_s()
else:
if form == 'slapd.conf':
if name is None:
module.fail_json(msg="missing name")
target = slapd_to_ldif(target, name)
if target is None and mod is None:
@@ -335,40 +335,40 @@ def main():
changed |= loadModule (module, l, '%s.la' % mod)
if target is None and suffix is None:
l.unbind_s()
module.exit_json(changed=changed)
if target is None or suffix is None:
module.fail_json(msg="missing target or suffix")
r = getDN_DB(module, l, 'olcSuffix', suffix)
if not r:
module.fail_json(msg="No database found for suffix %s" % suffix)
elif len(r) > 1:
module.fail_json(msg="Multiple results found! This is a bug. Please report.")
else:
d = 'olcOverlay=%s,%s' % (mod, r.pop()[0])
callback = lambda _,e: processEntry(module,l,d,e)
parser = LDIFCallback( module, open(target, 'r'), callback )
parser.parse()
changed = parser.changed
l.unbind_s()
- except subprocess.CalledProcessError, e:
+ except subprocess.CalledProcessError as e:
module.fail_json(rv=e.returncode, msg=e.output.rstrip())
- except ldap.LDAPError, e:
+ except ldap.LDAPError as e:
e = e.args[0]
if 'info' in e.keys():
msg = e['info']
elif 'desc' in e.keys():
msg = e['desc']
else:
msg = str(e)
module.fail_json(msg=msg)
- except KeyError, e:
+ except KeyError as e:
module.fail_json(msg=str(e))
module.exit_json(changed=changed)
# import module snippets
from ansible.module_utils.basic import *
main()
diff --git a/lib/modules/postmap b/lib/modules/postmap
index 8c9d54c..ce09018 100644
--- a/lib/modules/postmap
+++ b/lib/modules/postmap
@@ -1,21 +1,21 @@
-#!/usr/bin/python
+#!/usr/bin/python3
# Create or update postfix's alias and lookup tables
# Copyright (c) 2013 Guilhem Moulin <guilhem@fripost.org>
#
# 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/>.
try:
import selinux
HAVE_SELINUX=True
@@ -80,30 +80,30 @@ def main():
if os.path.isabs(src):
src = src
else:
module.fail_json(msg="absolute paths are required")
if not os.path.exists(src):
module.fail_json(src=src, msg="no such file")
try:
dst = "%s.%s" % (src, file_suffix(instance, db))
params['dest'] = dst
file_args = module.load_file_common_arguments(params)
changed = False
msg = None
if not os.path.exists(dst) or os.path.getmtime(dst) <= os.path.getmtime(src):
changed = True
if not module.check_mode:
msg = compile( cmd, instance, db, src)
- except subprocess.CalledProcessError, e:
+ except subprocess.CalledProcessError as e:
module.fail_json(rv=e.returncode, msg=e.output.rstrip())
changed = module.set_file_attributes_if_different(file_args, changed)
module.exit_json(changed=changed, msg=msg)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
diff --git a/lib/modules/postmulti b/lib/modules/postmulti
index d6ecb09..3c0a522 100644
--- a/lib/modules/postmulti
+++ b/lib/modules/postmulti
@@ -1,21 +1,21 @@
-#!/usr/bin/python
+#!/usr/bin/python3
# Create and manage postfix instances.
# Copyright (c) 2013 Guilhem Moulin <guilhem@fripost.org>
#
# 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/>.
# Look up postfix configuration variable
def postconf(k, instance=None):
@@ -77,27 +77,27 @@ def main():
cmd.extend([ '-G', group ])
cmd.extend([ '-I', 'postfix-%s' % instance ])
subprocess.check_output(cmd, stderr=subprocess.STDOUT).rstrip()
elif group != postconf('multi_instance_group', instance):
changed = True
# Assign a new group, or remove the existing group
if module.check_mode:
module.exit_json(changed=changed, msg="assign group")
cmd = [ os.path.join(os.sep, 'usr', 'sbin', 'postmulti') ]
cmd.extend([ '-e', 'assign', '-i', 'postfix-%s' % instance ])
if group:
cmd.extend([ '-G', group ])
else:
cmd.extend([ '-G', '-' ])
subprocess.check_output(cmd, stderr=subprocess.STDOUT).rstrip()
module.exit_json(changed=changed)
- except subprocess.CalledProcessError, e:
+ except subprocess.CalledProcessError as e:
module.fail_json(rv=e.returncode, msg=e.output.rstrip())
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()