Fix custom module support in Ansible 2.4

Before Ansible 2.4 the location of modules was a ':'-separated
string, but in Ansible 2.4 it is list of paths (and a list MUST
contain more than one item).

This patch adds correct handling for both Ansible versions.

Change-Id: Iab571f9e03e0f27a7720bcea801592d0ab3d3209
This commit is contained in:
Ilya Shakhat 2017-11-02 11:17:48 +01:00
parent acdd04e4e0
commit 276405ef1c
2 changed files with 35 additions and 3 deletions

View File

@ -121,6 +121,18 @@ def add_module_paths(paths):
MODULE_PATHS.update(dirs)
def make_module_path_option():
if PRE_24_ANSIBLE:
# it was a string of colon-separated directories
module_path = os.pathsep.join(get_module_paths())
else:
# now it is a list of strings (MUST have > 1 element)
module_path = list(get_module_paths())
if len(module_path) == 1:
module_path += [module_path[0]]
return module_path
Options = collections.namedtuple(
'Options',
['connection', 'module_path', 'forks',
@ -146,7 +158,7 @@ class AnsibleRunner(object):
self.passwords = dict(conn_pass=password, become_pass=become_password)
self.options = Options(
connection='smart',
module_path=os.pathsep.join(get_module_paths()),
module_path=make_module_path_option(),
forks=forks, remote_user=remote_user,
private_key_file=private_key_file,
ssh_common_args=ssh_common_args, ssh_extra_args=None,

View File

@ -186,8 +186,7 @@ class AnsibleRunnerTestCase(test.TestCase):
def test___init__options(self, config, options_args, passwords,
mock_options):
runner = executor.AnsibleRunner(**config)
module_path = executor.resolve_relative_path(
'os_faults/ansible/modules')
module_path = executor.make_module_path_option()
mock_options.assert_called_once_with(module_path=module_path,
**options_args)
self.assertEqual(passwords, runner.passwords)
@ -403,3 +402,24 @@ class AnsibleRunnerTestCase(test.TestCase):
mock.call('Execution completed with 1 result(s):'),
mock.call(result),
))
@mock.patch('os_faults.executor.get_module_paths')
@mock.patch('os_faults.executor.PRE_24_ANSIBLE', False)
def test_make_module_path_option_ansible_24(self, mock_mp):
mock_mp.return_value = ['/path/one', 'path/two']
self.assertEqual(['/path/one', 'path/two'],
executor.make_module_path_option())
@mock.patch('os_faults.executor.get_module_paths')
@mock.patch('os_faults.executor.PRE_24_ANSIBLE', False)
def test_make_module_path_option_ansible_24_one_item(self, mock_mp):
mock_mp.return_value = ['/path/one']
self.assertEqual(['/path/one', '/path/one'],
executor.make_module_path_option())
@mock.patch('os_faults.executor.get_module_paths')
@mock.patch('os_faults.executor.PRE_24_ANSIBLE', True)
def test_make_module_path_option_ansible_pre24(self, mock_mp):
mock_mp.return_value = ['/path/one', 'path/two']
self.assertEqual('/path/one:path/two',
executor.make_module_path_option())