Skip to content

Commit 95121ff

Browse files
authored
Merge pull request #238 from stackhpc/wallaby_backport_diff_mode
Wallaby backport: diff mode
2 parents 1c1826d + 83e102c commit 95121ff

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

doc/source/usage.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,13 @@ playbooks to be limited to matching plays and tasks. The ``--kolla-tags
6969
limited to matching plays and tasks. The ``--skip-tags <TAGS>`` and
7070
``--kolla-skip-tags <TAGS>`` arguments allow for avoiding execution of matching
7171
plays and tasks.
72+
73+
Check and diff mode
74+
-------------------
75+
76+
Ansible supports `check and diff modes
77+
<https://docs.ansible.com/ansible/latest/user_guide/playbooks_checkmode.html>`_,
78+
which can be used to improve visibility into changes that would be made on
79+
target systems. The Kayobe CLI supports the ``--check`` argument, and since
80+
11.0.0, the ``--diff`` argument. Note that these modes are not always
81+
guaranteed to work, when some tasks are dependent on earlier ones.

kayobe/ansible.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def add_args(parser):
4848
help="path to Kayobe configuration. "
4949
"(default=$%s or %s)" %
5050
(CONFIG_PATH_ENV, DEFAULT_CONFIG_PATH))
51+
parser.add_argument("-D", "--diff", action="store_true",
52+
help="when changing (small) files and templates, show "
53+
"the differences in those files; works great "
54+
"with --check")
5155
parser.add_argument("--environment", default=default_environment,
5256
help="specify environment name (default=$%s or None)" %
5357
ENVIRONMENT_ENV)
@@ -161,7 +165,7 @@ def _get_vars_files(vars_paths):
161165

162166
def build_args(parsed_args, playbooks,
163167
extra_vars=None, limit=None, tags=None, verbose_level=None,
164-
check=None, ignore_limit=False, list_tasks=None):
168+
check=None, ignore_limit=False, list_tasks=None, diff=None):
165169
"""Build arguments required for running Ansible playbooks."""
166170
cmd = ["ansible-playbook"]
167171
if verbose_level:
@@ -193,6 +197,8 @@ def build_args(parsed_args, playbooks,
193197
cmd += ["--become"]
194198
if check or (parsed_args.check and check is None):
195199
cmd += ["--check"]
200+
if diff or (parsed_args.diff and diff is None):
201+
cmd += ["--diff"]
196202
if not ignore_limit and (parsed_args.limit or limit):
197203
limit_arg = utils.intersect_limits(parsed_args.limit, limit)
198204
cmd += ["--limit", limit_arg]
@@ -227,13 +233,14 @@ def _get_environment(parsed_args):
227233
def run_playbooks(parsed_args, playbooks,
228234
extra_vars=None, limit=None, tags=None, quiet=False,
229235
check_output=False, verbose_level=None, check=None,
230-
ignore_limit=False, list_tasks=None):
236+
ignore_limit=False, list_tasks=None, diff=None):
231237
"""Run a Kayobe Ansible playbook."""
232238
_validate_args(parsed_args, playbooks)
233239
cmd = build_args(parsed_args, playbooks,
234240
extra_vars=extra_vars, limit=limit, tags=tags,
235241
verbose_level=verbose_level, check=check,
236-
ignore_limit=ignore_limit, list_tasks=list_tasks)
242+
ignore_limit=ignore_limit, list_tasks=list_tasks,
243+
diff=diff)
237244
env = _get_environment(parsed_args)
238245
try:
239246
utils.run_command(cmd, check_output=check_output, quiet=quiet, env=env)
@@ -269,7 +276,7 @@ def config_dump(parsed_args, host=None, hosts=None, var_name=None,
269276
run_playbook(parsed_args, playbook_path,
270277
extra_vars=extra_vars, tags=tags, check_output=True,
271278
verbose_level=verbose_level, check=False,
272-
list_tasks=False)
279+
list_tasks=False, diff=False)
273280
hostvars = {}
274281
for path in os.listdir(dump_dir):
275282
LOG.debug("Found dump file %s", path)

kayobe/kolla_ansible.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ def _get_environment(parsed_args):
157157
ansible_cfg_path = os.path.join(parsed_args.config_path, "ansible.cfg")
158158
if utils.is_readable_file(ansible_cfg_path)["result"]:
159159
env.setdefault("ANSIBLE_CONFIG", ansible_cfg_path)
160+
# kolla-ansible allows passing additional arguments to ansible-playbook via
161+
# EXTRA_OPTS.
162+
if parsed_args.check or parsed_args.diff:
163+
extra_opts = env.setdefault("EXTRA_OPTS", "")
164+
if parsed_args.check and "--check" not in extra_opts:
165+
env["EXTRA_OPTS"] += " --check"
166+
if parsed_args.diff and "--diff" not in extra_opts:
167+
env["EXTRA_OPTS"] += " --diff"
160168
return env
161169

162170

kayobe/tests/unit/test_ansible.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_run_playbooks_all_the_args(self, mock_validate, mock_vars,
6868
"-b",
6969
"-C",
7070
"--config-path", "/path/to/config",
71+
"-D",
7172
"--environment", "test-env",
7273
"-e", "ev_name1=ev_value1",
7374
"-i", "/path/to/inventory",
@@ -88,6 +89,7 @@ def test_run_playbooks_all_the_args(self, mock_validate, mock_vars,
8889
"-e", "ev_name1=ev_value1",
8990
"--become",
9091
"--check",
92+
"--diff",
9193
"--limit", "group1:host",
9294
"--tags", "tag1,tag2",
9395
"playbook1.yml",
@@ -117,6 +119,7 @@ def test_run_playbooks_all_the_long_args(self, mock_ask, mock_validate,
117119
"--become",
118120
"--check",
119121
"--config-path", "/path/to/config",
122+
"--diff",
120123
"--environment", "test-env",
121124
"--extra-vars", "ev_name1=ev_value1",
122125
"--inventory", "/path/to/inventory",
@@ -138,6 +141,7 @@ def test_run_playbooks_all_the_long_args(self, mock_ask, mock_validate,
138141
"-e", "ev_name1=ev_value1",
139142
"--become",
140143
"--check",
144+
"--diff",
141145
"--limit", "group1:host1",
142146
"--skip-tags", "tag3,tag4",
143147
"--tags", "tag1,tag2",
@@ -249,6 +253,7 @@ def test_run_playbooks_func_args(self, mock_validate, mock_vars, mock_run):
249253
"tags": "tag3,tag4",
250254
"verbose_level": 0,
251255
"check": True,
256+
"diff": True,
252257
}
253258
ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"],
254259
**kwargs)
@@ -260,6 +265,7 @@ def test_run_playbooks_func_args(self, mock_validate, mock_vars, mock_run):
260265
"-e", "ev_name1=ev_value1",
261266
"-e", "ev_name2='ev_value2'",
262267
"--check",
268+
"--diff",
263269
"--limit", "group1:host1:&group2:host2",
264270
"--tags", "tag1,tag2,tag3,tag4",
265271
"playbook1.yml",
@@ -426,7 +432,7 @@ def test_config_dump(self, mock_mkdtemp, mock_run, mock_listdir, mock_read,
426432
},
427433
check_output=True, tags=None,
428434
verbose_level=None, check=False,
429-
list_tasks=False)
435+
list_tasks=False, diff=False)
430436
mock_rmtree.assert_called_once_with(dump_dir)
431437
mock_listdir.assert_any_call(dump_dir)
432438
mock_read.assert_has_calls([

kayobe/tests/unit/test_kolla_ansible.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def test_run_all_the_args(self, mock_validate, mock_run):
5454
kolla_ansible.add_args(parser)
5555
vault.add_args(parser)
5656
args = [
57+
"-C",
58+
"-D",
5759
"--kolla-config-path", "/path/to/config",
5860
"-ke", "ev_name1=ev_value1",
5961
"-ki", "/path/to/inventory",
@@ -73,8 +75,9 @@ def test_run_all_the_args(self, mock_validate, mock_run):
7375
"--tags", "tag1,tag2",
7476
]
7577
expected_cmd = " ".join(expected_cmd)
78+
expected_env = {"EXTRA_OPTS": " --check --diff"}
7679
mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False,
77-
env={})
80+
env=expected_env)
7881

7982
@mock.patch.object(utils, "run_command")
8083
@mock.patch.object(kolla_ansible, "_validate_args")
@@ -87,6 +90,8 @@ def test_run_all_the_long_args(self, mock_ask, mock_validate, mock_run):
8790
mock_ask.return_value = "test-pass"
8891
args = [
8992
"--ask-vault-pass",
93+
"--check",
94+
"--diff",
9095
"--kolla-config-path", "/path/to/config",
9196
"--kolla-extra-vars", "ev_name1=ev_value1",
9297
"--kolla-inventory", "/path/to/inventory",
@@ -110,7 +115,8 @@ def test_run_all_the_long_args(self, mock_ask, mock_validate, mock_run):
110115
"--tags", "tag1,tag2",
111116
]
112117
expected_cmd = " ".join(expected_cmd)
113-
expected_env = {"KAYOBE_VAULT_PASSWORD": "test-pass"}
118+
expected_env = {"EXTRA_OPTS": " --check --diff",
119+
"KAYOBE_VAULT_PASSWORD": "test-pass"}
114120
expected_calls = [
115121
mock.call(["which", "kayobe-vault-password-helper"],
116122
check_output=True, universal_newlines=True),
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
features:
3+
- |
4+
Adds a ``--diff`` argument to kayobe CLI commands. This is passed through
5+
to ``ansible-playbook`` for Kayobe and Kolla Ansible playbooks, and can be
6+
used with the ``--check`` argument to see changes that would be made to
7+
files.
8+
upgrade:
9+
- |
10+
The ``--check`` argument to kayobe CLI commands is now passed through to
11+
Kolla Ansible playbooks.

0 commit comments

Comments
 (0)