From 491822c510ca9010d36aeecbcf7716828088cb1b Mon Sep 17 00:00:00 2001 From: Jeffrey Zhang Date: Tue, 17 May 2016 20:16:42 +0800 Subject: [PATCH] Bump the ansible to 2 DocImpact Change-Id: I3cdfbf84919de80f535c030bd146787ecda40dec partial-Implements: blueprint ansible2 --- .gitignore | 3 + ansible/action_plugins/merge_configs.py | 91 +++++++++++-------------- ansible/library/kolla_docker.py | 21 ++++-- ansible/roles/mariadb/tasks/start.yml | 6 ++ dev/vagrant/bootstrap.sh | 2 +- tools/setup_gate.sh | 2 +- 6 files changed, 65 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index 7cae5350e5..5e26231d87 100644 --- a/.gitignore +++ b/.gitignore @@ -43,5 +43,8 @@ dev/vagrant/storage/ # Files created by reno build releasenotes/build +# Files generated by Ansible +ansible/site.retry + # Others .DS_Store diff --git a/ansible/action_plugins/merge_configs.py b/ansible/action_plugins/merge_configs.py index 2d2b6545bc..b5df51fdc2 100644 --- a/ansible/action_plugins/merge_configs.py +++ b/ansible/action_plugins/merge_configs.py @@ -18,81 +18,70 @@ from ConfigParser import ConfigParser from cStringIO import StringIO import os -from ansible.runner.return_data import ReturnData -from ansible import utils -from ansible.utils import template +from ansible.plugins.action import ActionBase -class ActionModule(object): +class ActionModule(ActionBase): TRANSFERS_FILES = True - def __init__(self, runner): - self.runner = runner - - def read_config(self, source, inject, config): + def read_config(self, source, config): # Only use config if present if os.access(source, os.R_OK): - # template the source data locally & get ready to transfer - resultant = template.template_from_file(self.runner.basedir, - source, inject) - - # Read in new results and merge this with the existing config - fakefile = StringIO(resultant) + with open(source, 'r') as f: + template_data = f.read() + result = self._templar.template(template_data) + fakefile = StringIO(result) config.readfp(fakefile) fakefile.close() - def run(self, conn, tmp, module_name, module_args, inject, - complex_args=None, **kwargs): - args = {} - if complex_args: - args.update(complex_args) - args.update(utils.parse_kv(module_args)) + def run(self, tmp=None, task_vars=None): - dest = args.get('dest') - extra_vars = args.get('vars') - sources = args.get('sources') + if task_vars is None: + task_vars = dict() + result = super(ActionModule, self).run(tmp, task_vars) - if extra_vars: - # Extend 'inject' args used in templating - if isinstance(extra_vars, dict): - inject.update(extra_vars) - else: - inject.update(utils.parse_kv(extra_vars)) + if not tmp: + tmp = self._make_tmp_path() + + sources = self._task.args.get('sources', None) + extra_vars = self._task.args.get('vars', list()) - # Catch the case where sources is a str() if not isinstance(sources, list): sources = [sources] + temp_vars = task_vars.copy() + temp_vars.update(extra_vars) + config = ConfigParser() + old_vars = self._templar._available_variables + self._templar.set_available_variables(temp_vars) for source in sources: - # template the source string - source = template.template(self.runner.basedir, source, inject) - - try: - self.read_config(source, inject, config) - except Exception as e: - return ReturnData(conn=conn, comm_ok=False, - result={'failed': True, 'msg': str(e)}) + self.read_config(source, config) + self._templar.set_available_variables(old_vars) # Dump configparser to string via an emulated file + fakefile = StringIO() config.write(fakefile) - # Template the file to fill out any variables - content = template.template(self.runner.basedir, fakefile.getvalue(), - inject) + + remote_path = self._connection._shell.join_path(tmp, 'src') + xfered = self._transfer_data(remote_path, fakefile.getvalue()) fakefile.close() - # Ship this content over to a new file for use with the copy module - xfered = self.runner._transfer_str(conn, tmp, 'source', content) + new_module_args = self._task.args.copy() + del new_module_args['vars'] + del new_module_args['sources'] - copy_module_args = dict( - src=xfered, - dest=dest, - original_basename=os.path.basename(source), - follow=True, + new_module_args.update( + dict( + src=xfered + ) ) - return self.runner._execute_module(conn, tmp, 'copy', '', - inject=inject, - complex_args=copy_module_args) + + result.update(self._execute_module(module_name='copy', + module_args=new_module_args, + task_vars=task_vars, + tmp=tmp)) + return result diff --git a/ansible/library/kolla_docker.py b/ansible/library/kolla_docker.py index 50d37deeb8..e8e39490d1 100644 --- a/ansible/library/kolla_docker.py +++ b/ansible/library/kolla_docker.py @@ -683,7 +683,8 @@ def generate_module(): ] return AnsibleModule( argument_spec=argument_spec, - required_together=required_together + required_together=required_together, + bypass_checks=True ) @@ -691,15 +692,21 @@ def generate_nested_module(): module = generate_module() # We unnest the common dict and the update it with the other options - new_args = module.params.get('common_options') - new_args.update(module._load_params()[0]) - module.params = new_args + new_args = module.params.pop('common_options', dict()) + + # NOTE(jeffrey4l): merge the environment + env = module.params.pop('environment', dict()) + if env: + new_args['environment'].update(env) + + for key, value in module.params.iteritems(): + if key in new_args and value is None: + continue + new_args[key] = value # Override ARGS to ensure new args are used - global MODULE_ARGS global MODULE_COMPLEX_ARGS - MODULE_ARGS = '' - MODULE_COMPLEX_ARGS = json.dumps(module.params) + MODULE_COMPLEX_ARGS = json.dumps(new_args) # Reprocess the args now that the common dict has been unnested return generate_module() diff --git a/ansible/roles/mariadb/tasks/start.yml b/ansible/roles/mariadb/tasks/start.yml index e75b3e0d17..3eff6ebeea 100644 --- a/ansible/roles/mariadb/tasks/start.yml +++ b/ansible/roles/mariadb/tasks/start.yml @@ -14,6 +14,8 @@ ( groups['mariadb'] | length ) == 1 or ( delegate_host == 'None' and inventory_hostname != groups['mariadb'][0] ) +# TODO(jeffrey4l), remove the task check when the wair_for bug is fixed +# https://github.com/ansible/ansible-modules-core/issues/2788 - name: Waiting for MariaDB service to be ready wait_for: host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}" @@ -21,3 +23,7 @@ connect_timeout: 1 timeout: 60 search_regex: "MariaDB" + register: check_mariadb_port + until: check_mariadb_port | success + retries: 10 + delay: 6 diff --git a/dev/vagrant/bootstrap.sh b/dev/vagrant/bootstrap.sh index ace58ecba1..a6c6de0caf 100644 --- a/dev/vagrant/bootstrap.sh +++ b/dev/vagrant/bootstrap.sh @@ -152,7 +152,7 @@ function configure_operator { exit 1 fi - pip install --upgrade "ansible<2" python-openstackclient python-neutronclient tox + pip install --upgrade "ansible>=2" python-openstackclient python-neutronclient tox pip install ${KOLLA_PATH} diff --git a/tools/setup_gate.sh b/tools/setup_gate.sh index e7c75425d7..91caa72dc7 100755 --- a/tools/setup_gate.sh +++ b/tools/setup_gate.sh @@ -98,7 +98,7 @@ function setup_ansible { mkdir /tmp/kolla # TODO(SamYaple): Move to virtualenv - sudo -H pip install -U "ansible<2" "docker-py>=1.6.0" "python-openstackclient" "python-neutronclient" + sudo -H pip install -U "ansible>=2" "docker-py>=1.6.0" "python-openstackclient" "python-neutronclient" detect_distro setup_inventory