Bump the ansible to 2

DocImpact

Change-Id: I3cdfbf84919de80f535c030bd146787ecda40dec
partial-Implements: blueprint ansible2
This commit is contained in:
Jeffrey Zhang 2016-05-17 20:16:42 +08:00
parent c98e9c44f2
commit 491822c510
6 changed files with 65 additions and 60 deletions

3
.gitignore vendored
View File

@ -43,5 +43,8 @@ dev/vagrant/storage/
# Files created by reno build
releasenotes/build
# Files generated by Ansible
ansible/site.retry
# Others
.DS_Store

View File

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

View File

@ -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()

View File

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

View File

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

View File

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