summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2016-12-08 19:39:01 +0100
committerGuilhem Moulin <guilhem@fripost.org>2016-12-08 19:39:01 +0100
commitca71056ec50e7b51ca0eaebb7a716207ce1a00e6 (patch)
tree3d782f3ad97db1095765db8c5fe137bb1eb34069
parent43f39850ffd9e658b4d783106ea32d9f5430e633 (diff)
Make Ansible modules compatible with Ansible 2.2.0.0.
-rw-r--r--lib/action_plugins/openldap.py5
-rw-r--r--lib/modules/openldap14
2 files changed, 10 insertions, 9 deletions
diff --git a/lib/action_plugins/openldap.py b/lib/action_plugins/openldap.py
index ad77abc..86ca41f 100644
--- a/lib/action_plugins/openldap.py
+++ b/lib/action_plugins/openldap.py
@@ -1,69 +1,70 @@
# Manage OpenLDAP databases
# Copyright (c) 2014 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/>.
from ansible.plugins.action import ActionBase
-from ansible.utils.unicode import to_bytes, to_unicode
+from ansible.utils.unicode import to_bytes
+from ansible.module_utils._text import to_text
class ActionModule(ActionBase):
TRANSFERS_FILES = True
def run(self, tmp=None, task_vars=None):
if task_vars is None:
task_vars = dict()
if self._play_context.check_mode:
return dict(skipped=True, msg='check mode not supported for this module')
result = super(ActionModule, self).run(tmp, task_vars)
target = self._task.args.get('target', None)
local = self._task.args.get('local', 'no')
if local not in [ 'no', 'file', 'template' ]:
return dict(failed=True, msg="local must be in ['no','file','template']")
if local != 'no' and target is None:
return dict(failed=True, msg="target is required in local mode")
if local == 'no':
# run the module remotely
return self._execute_module(module_args=self._task.args, task_vars=task_vars)
if self._task._role is not None:
target = self._loader.path_dwim_relative(self._task._role._role_path, local+'s', target)
else:
target = self._loader.path_dwim_relative(self._loader.get_basedir(), local+'s', target)
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
new_module_args = self._task.args.copy()
new_module_args['target'] = self._connection._shell.join_path(self._make_tmp_path(remote_user), 'target.ldif')
new_module_args['local'] = 'no'
if local == 'template':
# template the source data locally
try:
with open(target, 'r') as f:
- template_data = to_unicode(f.read())
+ template_data = to_text(f.read())
target = self._templar.template(template_data, preserve_trailing_newlines=True, escape_backslashes=False, convert_data=False)
except Exception as e:
result['failed'] = True
result['msg'] = type(e).__name__ + ": " + str(e)
return result
# transfer the file and run the module remotely
self._transfer_data(new_module_args['target'], target)
result.update(self._execute_module(module_args=new_module_args, task_vars=task_vars))
return result
diff --git a/lib/modules/openldap b/lib/modules/openldap
index 5178033..9b015b6 100644
--- a/lib/modules/openldap
+++ b/lib/modules/openldap
@@ -122,72 +122,72 @@ def flexibleSearch(module, l, dn, entry):
f = f[0]
else:
f = '(&(' + ')('.join(f) + '))'
r = l.search_s( base, scope, filterstr=f )
if len(r) > 1:
module.fail_json(msg="Multiple results found! This is a bug. Please report.")
elif r:
return r.pop()
# Add or modify (only the attributes that differ from those in the
# directory) the entry for that DN.
# l must be an LDAPObject, and should provide an open connection to the
# directory with disclose/search/write access.
def processEntry(module, l, dn, entry):
changed = False
for x in indexedAttributes.intersection(entry.keys()):
# remove useless extra spaces in ACLs etc
- entry[x] = map( partial(multispaces.sub, ' '), entry[x] )
+ entry[x] = list(map( partial(multispaces.sub, ' '), entry[x] ))
r = flexibleSearch( module, l, dn, entry )
if r is None:
changed = True
if module.check_mode:
module.exit_json(changed=changed, msg="add DN %s" % dn)
if 'olcAccess' in entry.keys():
# replace "username=...,cn=peercred,cn=external,cn=auth"
# by a DN with proper gidNumber and uidNumber
- entry['olcAccess'] = map ( partial(sasl_ext_re.sub, acl_sasl_ext)
- , entry['olcAccess'] )
+ entry['olcAccess'] = list(map ( partial(sasl_ext_re.sub, acl_sasl_ext)
+ , entry['olcAccess'] ))
l.add_s( dn, addModlist(entry) )
else:
d,e = r
fst = str2dn(dn).pop(0)[0][0]
diff = []
for a,v in e.iteritems():
if a not in entry.keys():
if a != fst:
# delete all values except for the first attribute,
# which is implicit
diff.append(( ldap.MOD_DELETE, a, None ))
elif a in indexedAttributes:
if a == 'olcAccess':
# replace "username=...,cn=peercred,cn=external,cn=auth"
# by a DN with proper gidNumber and uidNumber
- entry[a] = map ( partial(sasl_ext_re.sub, acl_sasl_ext)
- , entry[a] )
+ entry[a] = list(map ( partial(sasl_ext_re.sub, acl_sasl_ext)
+ , entry[a] ))
# add explicit indices in the entry from the LDIF
- entry[a] = map( (lambda x: '{%d}%s' % x)
- , zip(range(len(entry[a])),entry[a]) )
+ entry[a] = list(map( (lambda x: '{%d}%s' % x)
+ , zip(range(len(entry[a])),entry[a])))
if v != entry[a]:
diff.append(( ldap.MOD_REPLACE, a, entry[a] ))
elif v != entry[a]:
# for non-indexed attribute, we update values in the
# symmetric difference only
s1 = set(v)
s2 = set(entry[a])
if s1.isdisjoint(s2):
# replace the former values with the new ones
diff.append(( ldap.MOD_REPLACE, a, entry[a] ))
else:
x = list(s1.difference(s2))
if x:
diff.append(( ldap.MOD_DELETE, a, x ))
y = list(s2.difference(s1))
if y:
diff.append(( ldap.MOD_ADD, a, y ))
# add attributes that weren't in e
for a in set(entry).difference(e.keys()):