Add check flag to ansible function arguments

This commit is contained in:
Mark Goddard 2017-07-05 19:01:42 +01:00
parent 9a7fae1973
commit 1577ab3c5e
3 changed files with 9 additions and 6 deletions

View File

@ -104,7 +104,8 @@ def _get_vars_files(config_path):
def build_args(parsed_args, playbooks, def build_args(parsed_args, playbooks,
extra_vars=None, limit=None, tags=None, verbose_level=None): extra_vars=None, limit=None, tags=None, verbose_level=None,
check=None):
"""Build arguments required for running Ansible playbooks.""" """Build arguments required for running Ansible playbooks."""
cmd = ["ansible-playbook"] cmd = ["ansible-playbook"]
if verbose_level: if verbose_level:
@ -123,7 +124,7 @@ def build_args(parsed_args, playbooks,
cmd += ["-e", "%s=%s" % (extra_var_name, extra_var_value)] cmd += ["-e", "%s=%s" % (extra_var_name, extra_var_value)]
if parsed_args.become: if parsed_args.become:
cmd += ["--become"] cmd += ["--become"]
if parsed_args.check: if check or (parsed_args.check and check is None):
cmd += ["--check"] cmd += ["--check"]
if parsed_args.limit or limit: if parsed_args.limit or limit:
limits = [l for l in [parsed_args.limit, limit] if l] limits = [l for l in [parsed_args.limit, limit] if l]
@ -137,12 +138,12 @@ def build_args(parsed_args, playbooks,
def run_playbooks(parsed_args, playbooks, def run_playbooks(parsed_args, playbooks,
extra_vars=None, limit=None, tags=None, quiet=False, extra_vars=None, limit=None, tags=None, quiet=False,
verbose_level=None): verbose_level=None, check=None):
"""Run a Kayobe Ansible playbook.""" """Run a Kayobe Ansible playbook."""
_validate_args(parsed_args, playbooks) _validate_args(parsed_args, playbooks)
cmd = build_args(parsed_args, playbooks, cmd = build_args(parsed_args, playbooks,
extra_vars=extra_vars, limit=limit, tags=tags, extra_vars=extra_vars, limit=limit, tags=tags,
verbose_level=verbose_level) verbose_level=verbose_level, check=check)
try: try:
utils.run_command(cmd, quiet=quiet) utils.run_command(cmd, quiet=quiet)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:

View File

@ -19,7 +19,6 @@ import subprocess
import sys import sys
from kayobe import utils from kayobe import utils
from kayobe import vault
DEFAULT_CONFIG_PATH = "/etc/kolla" DEFAULT_CONFIG_PATH = "/etc/kolla"

View File

@ -207,6 +207,7 @@ class TestCase(unittest.TestCase):
"limit": "group2:host2", "limit": "group2:host2",
"tags": "tag3,tag4", "tags": "tag3,tag4",
"verbose_level": 0, "verbose_level": 0,
"check": True,
} }
ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"], ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"],
**kwargs) **kwargs)
@ -217,6 +218,7 @@ class TestCase(unittest.TestCase):
"-e", "@/etc/kayobe/vars-file2.yaml", "-e", "@/etc/kayobe/vars-file2.yaml",
"-e", "ev_name1=ev_value1", "-e", "ev_name1=ev_value1",
"-e", "ev_name2=ev_value2", "-e", "ev_name2=ev_value2",
"--check",
"--limit", "group1:host1:&group2:host2", "--limit", "group1:host1:&group2:host2",
"--tags", "tag1,tag2,tag3,tag4", "--tags", "tag1,tag2,tag3,tag4",
"playbook1.yml", "playbook1.yml",
@ -263,7 +265,8 @@ class TestCase(unittest.TestCase):
extra_vars={ extra_vars={
"dump_path": dump_dir, "dump_path": dump_dir,
}, },
quiet=True, verbose_level=None) quiet=True, verbose_level=None,
check=False)
mock_rmtree.assert_called_once_with(dump_dir) mock_rmtree.assert_called_once_with(dump_dir)
mock_listdir.assert_called_once_with(dump_dir) mock_listdir.assert_called_once_with(dump_dir)
mock_read.assert_has_calls([ mock_read.assert_has_calls([