Skip to content

Commit

Permalink
Preserve special ANSIBLE_ envs with their value
Browse files Browse the repository at this point in the history
ANSIBLE_ like environment variables have higher precedence over their
equivalents inside ansible.cfg.  Thus, if the envs are set on CLI, the
roles_path, library and filter_plugins inside ansible.cfg are completely
ignored.

No longer set these inside ansible.cfg, and pass them as env vars
to the provisioner and ansible-lint.  This prevents one from providing
these on the command line, since molecule.yml provides a mechanism for
setting env vars.

Fixes: #845
  • Loading branch information
retr0h committed Jun 12, 2017
1 parent 89083a2 commit 38e8acb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
5 changes: 4 additions & 1 deletion molecule/lint/ansible_lint.py
Expand Up @@ -89,7 +89,10 @@ def default_options(self):

@property
def default_env(self):
return self._config.merge_dicts(os.environ.copy(), self._config.env)
env = self._config.merge_dicts(os.environ.copy(), self._config.env)
env = self._config.merge_dicts(env, self._config.provisioner.env)

return env

@property
def default_ignore_paths(self):
Expand Down
49 changes: 38 additions & 11 deletions molecule/provisioner/ansible.py
Expand Up @@ -216,13 +216,6 @@ def default_config_options(self):
False,
'nocows':
1,
'roles_path':
'../../../../:$ANSIBLE_ROLES_PATH',
'library':
'{}:$ANSIBLE_LIBRARY'.format(self._get_libraries_directory()),
'filter_plugins':
'{}:$ANSIBLE_FILTER_PLUGINS'.format(
self._get_filter_plugin_directory()),
},
'ssh_connection': {
'scp_if_ssh': True,
Expand All @@ -240,8 +233,16 @@ def default_options(self):
@property
def default_env(self):
env = self._config.merge_dicts(os.environ.copy(), self._config.env)
env = self._config.merge_dicts(
env, {'ANSIBLE_CONFIG': self._config.provisioner.config_file})
env = self._config.merge_dicts(env, {
'ANSIBLE_CONFIG':
self._config.provisioner.config_file,
'ANSIBLE_ROLES_PATH':
'../../../../',
'ANSIBLE_LIBRARY':
self._get_libraries_directory(),
'ANSIBLE_FILTER_PLUGINS':
self._get_filter_plugin_directory(),
})
env = self._config.merge_dicts(env, self._config.env)

return env
Expand All @@ -264,8 +265,34 @@ def options(self):

@property
def env(self):
return self._config.merge_dicts(
self.default_env, self._config.config['provisioner']['env'])
default_env = self.default_env
env = self._config.config['provisioner']['env'].copy()

roles_path = default_env['ANSIBLE_ROLES_PATH']
library_path = default_env['ANSIBLE_LIBRARY']
filter_plugins_path = default_env['ANSIBLE_FILTER_PLUGINS']

try:
roles_path = '{}:{}'.format(roles_path, env['ANSIBLE_ROLES_PATH'])
except KeyError:
pass

try:
library_path = '{}:{}'.format(library_path, env['ANSIBLE_LIBRARY'])
except KeyError:
pass

try:
filter_plugins_path = '{}:{}'.format(filter_plugins_path,
env['ANSIBLE_FILTER_PLUGINS'])
except KeyError:
pass

env['ANSIBLE_ROLES_PATH'] = roles_path
env['ANSIBLE_LIBRARY'] = library_path
env['ANSIBLE_FILTER_PLUGINS'] = filter_plugins_path

return self._config.merge_dicts(default_env, env)

@property
def host_vars(self):
Expand Down
4 changes: 4 additions & 0 deletions test/unit/lint/test_ansible_lint.py
Expand Up @@ -102,6 +102,10 @@ def test_default_env_property(ansible_lint_instance):

def test_env_property(ansible_lint_instance):
assert 'bar' == ansible_lint_instance.env['foo']
assert 'ANSIBLE_CONFIG' in ansible_lint_instance.env
assert 'ANSIBLE_ROLES_PATH' in ansible_lint_instance.env
assert 'ANSIBLE_LIBRARY' in ansible_lint_instance.env
assert 'ANSIBLE_FILTER_PLUGINS' in ansible_lint_instance.env


def test_default_ignore_paths_property(ansible_lint_instance):
Expand Down
56 changes: 25 additions & 31 deletions test/unit/provisioner/test_ansible.py
Expand Up @@ -45,7 +45,10 @@ def molecule_provisioner_section_data():
'foo': 'bar'
},
'env': {
'foo': 'bar'
'foo': 'bar',
'ANSIBLE_ROLES_PATH': 'foo/bar',
'ANSIBLE_LIBRARY': 'foo/bar',
'ANSIBLE_FILTER_PLUGINS': 'foo/bar',
},
'host_vars': {
'instance-1': [{
Expand Down Expand Up @@ -84,24 +87,13 @@ def test_ns_private_member(ansible_instance):


def test_default_config_options_property(ansible_instance):
libraries_directory = ansible_instance._get_libraries_directory()
filter_plugins_directory = ansible_instance._get_filter_plugin_directory()
x = {
'defaults': {
'ansible_managed':
'Ansible managed: Do NOT edit this file manually!',
'retry_files_enabled':
False,
'host_key_checking':
False,
'nocows':
1,
'roles_path':
'../../../../:$ANSIBLE_ROLES_PATH',
'library':
'{}:$ANSIBLE_LIBRARY'.format(libraries_directory),
'filter_plugins':
'{}:$ANSIBLE_FILTER_PLUGINS'.format(filter_plugins_directory),
'retry_files_enabled': False,
'host_key_checking': False,
'nocows': 1,
},
'ssh_connection': {
'scp_if_ssh': True,
Expand All @@ -123,33 +115,25 @@ def test_default_env_property(ansible_instance):
assert 'MOLECULE_INVENTORY_FILE' in ansible_instance.default_env
assert 'MOLECULE_SCENARIO_DIRECTORY' in ansible_instance.default_env
assert 'MOLECULE_INSTANCE_CONFIG' in ansible_instance.default_env
assert 'ANSIBLE_CONFIG' in ansible_instance.env
assert 'ANSIBLE_ROLES_PATH' in ansible_instance.env
assert 'ANSIBLE_LIBRARY' in ansible_instance.env
assert 'ANSIBLE_FILTER_PLUGINS' in ansible_instance.env


def test_name_property(ansible_instance):
assert 'ansible' == ansible_instance.name


def test_config_options_property(ansible_instance):
libraries_directory = ansible_instance._get_libraries_directory()
filter_plugins_directory = ansible_instance._get_filter_plugin_directory()
x = {
'defaults': {
'ansible_managed':
'Ansible managed: Do NOT edit this file manually!',
'retry_files_enabled':
False,
'host_key_checking':
False,
'nocows':
1,
'roles_path':
'../../../../:$ANSIBLE_ROLES_PATH',
'library':
'{}:$ANSIBLE_LIBRARY'.format(libraries_directory),
'filter_plugins':
'{}:$ANSIBLE_FILTER_PLUGINS'.format(filter_plugins_directory),
'foo':
'bar'
'retry_files_enabled': False,
'host_key_checking': False,
'nocows': 1,
'foo': 'bar'
},
'ssh_connection': {
'scp_if_ssh': True,
Expand Down Expand Up @@ -178,6 +162,16 @@ def test_env_property(ansible_instance):
assert 'bar' == ansible_instance.env['foo']


def test_env_appends_env_property(ansible_instance):
assert '../../../../:foo/bar' == ansible_instance.env['ANSIBLE_ROLES_PATH']

x = '{}:foo/bar'.format(ansible_instance._get_libraries_directory())
x == ansible_instance.env['ANSIBLE_LIBRARY']

x = '{}:foo/bar'.format(ansible_instance._get_filter_plugin_directory())
x == ansible_instance.env['ANSIBLE_FILTER_PLUGINS']


def test_host_vars_property(ansible_instance):
x = {'instance-1': [{'foo': 'bar'}], 'localhost': [{'foo': 'baz'}]}

Expand Down

0 comments on commit 38e8acb

Please sign in to comment.