Create ansible config file and disable retry files

retry files are problematic since we might not have write permissions
from the ansible run from mistral. So we copy the
/etc/ansible/ansible.cfg and modify it to disable retry files.

Change-Id: Ie36143067d330e7f35e17c249d98be38f4002800
This commit is contained in:
Juan Antonio Osorio Robles 2017-06-26 16:17:07 +00:00
parent 70b1fbf171
commit 6a48446a40
2 changed files with 62 additions and 7 deletions

View File

@ -16,6 +16,7 @@ import json
import os import os
import shutil import shutil
import six import six
from six.moves import configparser
import tempfile import tempfile
import yaml import yaml
@ -25,6 +26,22 @@ from mistral_lib import actions
from oslo_concurrency import processutils from oslo_concurrency import processutils
def write_default_ansible_cfg(work_dir,
base_ansible_cfg='/etc/ansible/ansible.cfg'):
ansible_config_path = os.path.join(work_dir, 'ansible.cfg')
shutil.copy(base_ansible_cfg, ansible_config_path)
config = configparser.ConfigParser()
config.read(ansible_config_path)
config.set('defaults', 'retry_files_enabled', 'False')
with open(ansible_config_path, 'w') as configfile:
config.write(configfile)
return ansible_config_path
class AnsibleAction(actions.Action): class AnsibleAction(actions.Action):
"""Executes ansible module""" """Executes ansible module"""
@ -171,8 +188,10 @@ class AnsibleAction(actions.Action):
return mistral_workflow_utils.Result(error=msg) return mistral_workflow_utils.Result(error=msg)
try: try:
ansible_config_path = write_default_ansible_cfg(self.work_dir)
env_variables = { env_variables = {
'HOME': self.work_dir 'HOME': self.work_dir,
'ANSIBLE_CONFIG': ansible_config_path
} }
if self.extra_env_variables: if self.extra_env_variables:
@ -369,8 +388,10 @@ class AnsiblePlaybookAction(actions.Action):
return mistral_workflow_utils.Result(error=msg) return mistral_workflow_utils.Result(error=msg)
try: try:
ansible_config_path = write_default_ansible_cfg(self.work_dir)
env_variables = { env_variables = {
'HOME': self.work_dir 'HOME': self.work_dir,
'ANSIBLE_CONFIG': ansible_config_path
} }
if self.extra_env_variables: if self.extra_env_variables:

View File

@ -16,6 +16,8 @@
import json import json
import mock import mock
import os import os
from six.moves import configparser
import tempfile
from oslo_concurrency import processutils from oslo_concurrency import processutils
@ -35,18 +37,23 @@ class AnsibleActionTest(base.TestCase):
self.become_user = 'root' self.become_user = 'root'
self.ctx = mock.MagicMock() self.ctx = mock.MagicMock()
@mock.patch("tripleo_common.actions.ansible.write_default_ansible_cfg")
@mock.patch("oslo_concurrency.processutils.execute") @mock.patch("oslo_concurrency.processutils.execute")
def test_run(self, mock_execute): def test_run(self, mock_execute, mock_write_cfg):
mock_execute.return_value = ('', '') mock_execute.return_value = ('', '')
action = ansible.AnsibleAction( action = ansible.AnsibleAction(
hosts=self.hosts, module=self.module, remote_user=self.remote_user, hosts=self.hosts, module=self.module, remote_user=self.remote_user,
become=self.become, become_user=self.become_user) become=self.become, become_user=self.become_user)
ansible_config_path = os.path.join(action.work_dir, 'ansible.cfg')
mock_write_cfg.return_value = ansible_config_path
action.run(self.ctx) action.run(self.ctx)
env = { env = {
'HOME': action.work_dir 'HOME': action.work_dir,
'ANSIBLE_CONFIG': ansible_config_path
} }
mock_execute.assert_called_once_with( mock_execute.assert_called_once_with(
@ -72,8 +79,9 @@ class AnsiblePlaybookActionTest(base.TestCase):
self.verbosity = 1 self.verbosity = 1
self.ctx = mock.MagicMock() self.ctx = mock.MagicMock()
@mock.patch("tripleo_common.actions.ansible.write_default_ansible_cfg")
@mock.patch("oslo_concurrency.processutils.execute") @mock.patch("oslo_concurrency.processutils.execute")
def test_run(self, mock_execute): def test_run(self, mock_execute, mock_write_cfg):
mock_execute.return_value = ('', '') mock_execute.return_value = ('', '')
@ -82,12 +90,15 @@ class AnsiblePlaybookActionTest(base.TestCase):
remote_user=self.remote_user, become=self.become, remote_user=self.remote_user, become=self.become,
become_user=self.become_user, extra_vars=self.extra_vars, become_user=self.become_user, extra_vars=self.extra_vars,
verbosity=self.verbosity) verbosity=self.verbosity)
ansible_config_path = os.path.join(action.work_dir, 'ansible.cfg')
mock_write_cfg.return_value = ansible_config_path
action.run(self.ctx) action.run(self.ctx)
pb = os.path.join(action.work_dir, 'playbook.yaml') pb = os.path.join(action.work_dir, 'playbook.yaml')
env = { env = {
'HOME': action.work_dir 'HOME': action.work_dir,
'ANSIBLE_CONFIG': ansible_config_path
} }
mock_execute.assert_called_once_with( mock_execute.assert_called_once_with(
@ -96,3 +107,26 @@ class AnsiblePlaybookActionTest(base.TestCase):
'--extra-vars', json.dumps(self.extra_vars), '--extra-vars', json.dumps(self.extra_vars),
env_variables=env, cwd=action.work_dir, env_variables=env, cwd=action.work_dir,
log_errors=processutils.LogErrors.ALL) log_errors=processutils.LogErrors.ALL)
class CopyConfigFileTest(base.TestCase):
def test_copy_config_file(self):
with tempfile.NamedTemporaryFile() as ansible_cfg_file:
ansible_cfg_path = ansible_cfg_file.name
work_dir = tempfile.mkdtemp(prefix='ansible-mistral-action-test')
# Needed for the configparser to be able to read this file.
ansible_cfg_file.write(b'[defaults]\n')
ansible_cfg_file.flush()
resulting_ansible_config = ansible.write_default_ansible_cfg(
work_dir, base_ansible_cfg=ansible_cfg_path)
self.assertEqual(resulting_ansible_config,
os.path.join(work_dir, 'ansible.cfg'))
config = configparser.ConfigParser()
config.read(resulting_ansible_config)
retry_files_enabled = config.get('defaults', 'retry_files_enabled')
self.assertEqual(retry_files_enabled, 'False')