Support check and diff mode for merge_config and merge_yaml module

Change-Id: Ib2ca736e08f48da88bb74feb5cd1efce3b860ab7
Partially-Implements: blueprint ansible-check-mode
This commit is contained in:
Jeffrey Zhang 2018-05-15 10:58:50 +08:00
parent b60468f06e
commit 1db352f007
5 changed files with 66 additions and 27 deletions

View File

@ -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
@ -131,22 +134,34 @@ class ActionModule(action.ActionBase):
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)
new_module_args.update(
dict(
src=xfered
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_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

View File

@ -17,6 +17,8 @@
import inspect
import os
import shutil
import tempfile
from yaml import dump
from yaml import safe_load
@ -28,6 +30,7 @@ except ImportError:
from yaml import Loader # noqa: F401
from ansible import constants
from ansible.plugins import action
@ -78,19 +81,31 @@ class ActionModule(action.ActionBase):
# 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

View File

@ -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"

View File

@ -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
@ -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" }

View File

@ -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.