Skip to content

Commit

Permalink
Make ansible.cfg settings configurable (#643)
Browse files Browse the repository at this point in the history
* Make ansible.cfg defaults section configurable

* Disable retry_files by default

* Add docs for ansiblecfg_defaults

* Make ansible.cfg ssh_connection section configurable

* Add docs for ansiblecfg_ssh_connection
  • Loading branch information
alvaroaleman authored and retr0h committed Jan 12, 2017
1 parent 8b528c7 commit 8a59c00
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doc/source/configuration.rst
Expand Up @@ -71,11 +71,19 @@ variables. There are only a few currently in use.
- -o ControlPersist=60s
raw_env_vars:
ANSIBLE_ACTION_PLUGINS: ../plugins
ansiblecfg_defaults:
retry_files_enabled: False
ansiblecfg_ssh_connection:
pipelining: True
The `raw_env_vars` section allows you to pass arbitrary environment variables
to ``ansible-playbook``. This can be useful, for example, if you want to do a
role level override of a value normally found in ``ansible.cfg``.

The `ansiblecfg_defaults` and `ansiblecfg_ssh_connection`` sections both take
arbitrary key value pairs. Those key value pairs will then be written to the
`defaults` and `ssh_connection` section of ``ansible.cfg``, respectively.

Host/Group Vars
^^^^^^^^^^^^^^^

Expand Down
5 changes: 4 additions & 1 deletion molecule/ansible_playbook.py
Expand Up @@ -116,7 +116,10 @@ def parse_arg(self, name, value):
self._playbook = value
return

if name == 'host_vars' or name == 'group_vars':
if name in [
'host_vars', 'group_vars', 'ansiblecfg_defaults',
'ansiblecfg_ssh_connection'
]:
return

# verbose is weird, must be -vvvv not verbose=vvvv
Expand Down
4 changes: 4 additions & 0 deletions molecule/config.py
Expand Up @@ -111,6 +111,10 @@ def _get_defaults(self):
'become': True,
'become_user': False,
'config_file': 'ansible.cfg',
'ansiblecfg_defaults': {
'retry_files_enabled': False,
},
'ansiblecfg_ssh_connection': {},
'diff': True,
'host_key_checking': False,
'inventory_file': 'ansible_inventory',
Expand Down
2 changes: 2 additions & 0 deletions molecule/cookiecutter/molecule/cookiecutter.json
Expand Up @@ -3,6 +3,8 @@
"ansiblecfg_molecule_dir": "OVERRIDEN",
"ansiblecfg_ansible_library_path": "OVERRIDEN",
"ansiblecfg_ansible_managed": "Ansible managed: Do NOT edit this file manually!",
"ansiblecfg_defaults": "OVERRIDDEN",
"ansiblecfg_ssh_connection": "OVERRIDDEN",
"rakefile_state_file": "OVERRIDEN",
"rakefile_serverspec_dir": "OVERRIDEN"
}
Expand Up @@ -5,3 +5,7 @@
roles_path = {{ cookiecutter.ansiblecfg_molecule_dir }}/roles:{{ cookiecutter.ansiblecfg_molecule_dir }}/../roles:../:../../
library = {{ cookiecutter.ansiblecfg_ansible_library_path }}
ansible_managed = {{ cookiecutter.ansiblecfg_ansible_managed }}
{{- cookiecutter.ansiblecfg_defaults }}

[ssh_connection]
{{- cookiecutter.ansiblecfg_ssh_connection }}
20 changes: 20 additions & 0 deletions molecule/core.py
Expand Up @@ -372,14 +372,34 @@ def _get_disabled(self):
# Ability to turn off features until we roll them out.
return self.config.config.get('_disabled', [])

def _dict_to_inisection_string(self, data, padding=15):
result = ''
for key, value in data.iteritems():
result += '\n%s = %s' % (key.ljust(padding), value)

return result

def _get_cookiecutter_context(self, molecule_dir):
state_file = self.config.config['molecule']['state_file']
serverspec_dir = self.config.config['molecule']['serverspec_dir']
ansiblecfg_defaults = self.config.config['ansible'][
'ansiblecfg_defaults']
ansiblecfg_ssh_connection = self.config.config['ansible'][
'ansiblecfg_ssh_connection']

# This is required because cookiecutter apparently casts all of its
# context to str, thus we can not do any looping in the template
ansiblecfg_defaults_string = self._dict_to_inisection_string(
ansiblecfg_defaults)
ansiblecfg_ssh_connection_string = self._dict_to_inisection_string(
ansiblecfg_ssh_connection)

return {
'repo_name': molecule_dir,
'ansiblecfg_molecule_dir': molecule_dir,
'ansiblecfg_ansible_library_path': 'library',
'rakefile_state_file': state_file,
'rakefile_serverspec_dir': serverspec_dir,
'ansiblecfg_defaults': ansiblecfg_defaults_string,
'ansiblecfg_ssh_connection': ansiblecfg_ssh_connection_string
}

0 comments on commit 8a59c00

Please sign in to comment.