Skip to content

Commit

Permalink
Support check and diff mode for merge_config and merge_yaml module
Browse files Browse the repository at this point in the history
Change-Id: Ib2ca736e08f48da88bb74feb5cd1efce3b860ab7
Partially-Implements: blueprint ansible-check-mode
  • Loading branch information
jeffrey4l committed Jun 12, 2018
1 parent b60468f commit 1db352f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
41 changes: 28 additions & 13 deletions ansible/action_plugins/merge_configs.py
Expand Up @@ -19,7 +19,10 @@
import collections
import inspect
import os
import shutil
import tempfile

from ansible import constants
from ansible.plugins import action
from six import StringIO

Expand Down Expand Up @@ -131,22 +134,34 @@ def run(self, tmp=None, task_vars=None):

fakefile = StringIO()
config.write(fakefile)

remote_path = self._connection._shell.join_path(tmp, 'src')
xfered = self._transfer_data(remote_path, fakefile.getvalue())
full_source = fakefile.getvalue()
fakefile.close()

new_module_args = self._task.args.copy()
new_module_args.pop('sources', None)
local_tempdir = tempfile.mkdtemp(dir=constants.DEFAULT_LOCAL_TMP)

try:
result_file = os.path.join(local_tempdir, 'source')
with open(result_file, 'wb') as f:
f.write(full_source)

new_task = self._task.copy()
new_task.args.pop('sources', None)

new_module_args.update(
dict(
src=xfered
new_task.args.update(
dict(
src=result_file
)
)
)

result.update(self._execute_module(module_name='copy',
module_args=new_module_args,
task_vars=task_vars,
tmp=tmp))
copy_action = self._shared_loader_obj.action_loader.get(
'copy',
task=new_task,
connection=self._connection,
play_context=self._play_context,
loader=self._loader,
templar=self._templar,
shared_loader_obj=self._shared_loader_obj)
result.update(copy_action.run(task_vars=task_vars))
finally:
shutil.rmtree(local_tempdir)
return result
43 changes: 29 additions & 14 deletions ansible/action_plugins/merge_yaml.py
Expand Up @@ -17,6 +17,8 @@

import inspect
import os
import shutil
import tempfile

from yaml import dump
from yaml import safe_load
Expand All @@ -28,6 +30,7 @@
from yaml import Loader # noqa: F401


from ansible import constants
from ansible.plugins import action


Expand Down Expand Up @@ -78,19 +81,31 @@ def run(self, tmp=None, task_vars=None):
# restore original vars
self._templar.set_available_variables(old_vars)

remote_path = self._connection._shell.join_path(tmp, 'src')
xfered = self._transfer_data(remote_path,
dump(output,
default_flow_style=False))
new_module_args = self._task.args.copy()
new_module_args.update(
dict(
src=xfered
local_tempdir = tempfile.mkdtemp(dir=constants.DEFAULT_LOCAL_TMP)

try:
result_file = os.path.join(local_tempdir, 'source')
with open(result_file, 'wb') as f:
f.write(dump(output, default_flow_style=False))

new_task = self._task.copy()
new_task.args.pop('sources', None)

new_task.args.update(
dict(
src=result_file
)
)
)
del new_module_args['sources']
result.update(self._execute_module(module_name='copy',
module_args=new_module_args,
task_vars=task_vars,
tmp=tmp))

copy_action = self._shared_loader_obj.action_loader.get(
'copy',
task=new_task,
connection=self._connection,
play_context=self._play_context,
loader=self._loader,
templar=self._templar,
shared_loader_obj=self._shared_loader_obj)
result.update(copy_action.run(task_vars=task_vars))
finally:
shutil.rmtree(local_tempdir)
return result
1 change: 1 addition & 0 deletions ansible/roles/common/tasks/config.yml
Expand Up @@ -240,6 +240,7 @@
owner: "{{ config_owner_user }}"
group: "{{ config_owner_group }}"
mode: "0770"
ignore_errors: "{{ ansible_check_mode }}"
when:
- item.value.enabled | bool
- item.key != "kolla-toolbox"
Expand Down
2 changes: 2 additions & 0 deletions ansible/roles/keystone/tasks/config.yml
Expand Up @@ -188,6 +188,7 @@
- name: Save the returned from cron jobs for building the crontab
set_fact:
cron_jobs: "{{ (cron_jobs_json.stdout | from_json).cron_jobs }}"
ignore_errors: "{{ ansible_check_mode }}"
when: keystone_token_provider == 'fernet'

- name: Copying files for keystone-fernet
Expand All @@ -199,6 +200,7 @@
mode: "0660"
become: true
register: keystone_fernet_confs
ignore_errors: "{{ ansible_check_mode }}"
with_items:
- { src: "crontab.j2", dest: "crontab" }
- { src: "fernet-rotate.sh.j2", dest: "fernet-rotate.sh" }
Expand Down
@@ -0,0 +1,6 @@
---
features:
- |
Support ansible check and diff module for generate configrations. You could
use ``EXTRA_OPTS='--check --diff' kolla-ansible genconfig`` to check what
the configration file will be like in dry-run mode.

0 comments on commit 1db352f

Please sign in to comment.