Skip to content

Commit 8e96f30

Browse files
fix: change type of ansible_pre_tasks and ansible_post_tasks to a list. (#588)
Issue #587
1 parent 9c6a069 commit 8e96f30

File tree

4 files changed

+106
-35
lines changed

4 files changed

+106
-35
lines changed

docs/advanced/default.yml.spec.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
The following is the full spec file for a `default.yml` that controls how Splunk gets provisioned.
1111

1212
```
13-
ansible_post_tasks: <str>
14-
* Comma-separated list of paths or URLs to custom Ansible playbooks to run AFTER Splunk has been setup using the provided site.yml
15-
* Default: null
13+
ansible_post_tasks: <list>
14+
* list of paths or URLs to custom Ansible playbooks to run AFTER Splunk has been setup using the provided site.yml
15+
* Default: []
1616
17-
ansible_pre_tasks: <str>
18-
* Comma-separated list of paths or URLs to custom Ansible playbooks to run BEFORE Splunk sets up using the provided site.yml
19-
* Default: null
17+
ansible_pre_tasks: <list>
18+
* list of paths or URLs to custom Ansible playbooks to run BEFORE Splunk sets up using the provided site.yml
19+
* Default: []
2020
2121
ansible_environment: <dict>
2222
* Map of environment variables used only during the execution context of all the Ansible tasks. For more information, see https://docs.ansible.com/ansible/latest/user_guide/playbooks_environment.html

inventory/environ.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,32 @@ def getLaunchConf(vars_scope):
411411
launch.update({k:v for k,v in [x.split("=", 1) for x in settings.split(",")]})
412412
vars_scope["splunk"]["launch"] = launch
413413

414+
def ensureListValue(value, separator):
415+
if isinstance(value, list):
416+
return value
417+
elif (not value) or (not value.strip()):
418+
return []
419+
else:
420+
return splitAndStrip(value, separator)
421+
422+
def splitAndStrip(value, separator):
423+
if not value:
424+
return []
425+
return [x.strip() for x in value.split(separator)]
426+
427+
def transformEnvironmentVariable(environmentVariableName, transform, default):
428+
if environmentVariableName in os.environ:
429+
return transform(os.environ.get(environmentVariableName))
430+
else:
431+
return default
432+
414433
def getAnsibleContext(vars_scope):
415434
"""
416435
Parse parameters that influence Ansible execution
417436
"""
418-
vars_scope["ansible_pre_tasks"] = os.environ.get("SPLUNK_ANSIBLE_PRE_TASKS", vars_scope.get("ansible_pre_tasks"))
419-
vars_scope["ansible_post_tasks"] = os.environ.get("SPLUNK_ANSIBLE_POST_TASKS", vars_scope.get("ansible_post_tasks"))
437+
stringSeparator = ","
438+
vars_scope["ansible_pre_tasks"] = transformEnvironmentVariable("SPLUNK_ANSIBLE_PRE_TASKS", lambda v: splitAndStrip(v, stringSeparator), ensureListValue(vars_scope.get("ansible_pre_tasks"), stringSeparator))
439+
vars_scope["ansible_post_tasks"] = transformEnvironmentVariable("SPLUNK_ANSIBLE_POST_TASKS", lambda v: splitAndStrip(v, stringSeparator), ensureListValue(vars_scope.get("ansible_post_tasks"), stringSeparator))
420440
vars_scope["ansible_environment"] = vars_scope.get("ansible_environment") or {}
421441
env = os.environ.get("SPLUNK_ANSIBLE_ENV")
422442
if env:

site.yml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
- block:
1010
- name: Execute pre-setup playbooks
11+
loop: "{{ ansible_pre_tasks }}"
12+
loop_control:
13+
loop_var: ansible_pre_tasks_item
1114
include_tasks: execute_adhoc_plays.yml
1215
vars:
13-
playbook: "{{ ansible_pre_tasks }}"
16+
playbook: "{{ ansible_pre_tasks_item }}"
1417
when:
15-
- ansible_pre_tasks is defined
16-
- ansible_pre_tasks is not none
17-
- ansible_pre_tasks is match("^(http|https|file)://.*")
18+
- ansible_pre_tasks_item is defined
19+
- ansible_pre_tasks_item is not none
20+
- ansible_pre_tasks_item is match("^(http|https|file)://.*")
1821

1922
- name: Provision role
2023
include_role:
@@ -23,13 +26,16 @@
2326
- splunk.role is defined
2427

2528
- name: Execute post-setup playbooks
29+
loop: "{{ ansible_post_tasks }}"
30+
loop_control:
31+
loop_var: ansible_post_tasks_item
2632
include_tasks: execute_adhoc_plays.yml
2733
vars:
28-
playbook: "{{ ansible_post_tasks }}"
34+
playbook: "{{ ansible_post_tasks_item }}"
2935
when:
30-
- ansible_post_tasks is defined
31-
- ansible_post_tasks is not none
32-
- ansible_post_tasks is match("^(http|https|file)://.*")
36+
- ansible_post_tasks_item is defined
37+
- ansible_post_tasks_item is not none
38+
- ansible_post_tasks_item is match("^(http|https|file)://.*")
3339

3440
- name: Check all instances for required restarts
3541
include_tasks: ./roles/splunk_common/tasks/check_for_required_restarts.yml

tests/small/test_environ.py

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -307,31 +307,76 @@ def test_getLaunchConf(default_yml, os_env, output):
307307
environ.getLaunchConf(vars_scope)
308308
assert vars_scope["splunk"] == output
309309

310+
@pytest.mark.parametrize(("value", "separator", "output"),
311+
[
312+
# Check null value
313+
(None, ",", []),
314+
# Check empty value
315+
("", ",", []),
316+
# Check string value
317+
("a", ",", ["a"]),
318+
# Check comma separated string value
319+
("a,b,c", ",", ["a", "b", "c"]),
320+
# Check list value
321+
(["a"], ",", ["a"]),
322+
(["a", "b", "c"], ",", ["a", "b", "c"])
323+
]
324+
)
325+
def test_ensureListValue(value, separator, output):
326+
result = environ.ensureListValue(value, separator)
327+
assert result == output
328+
329+
@pytest.mark.parametrize(("value", "separator", "output"),
330+
[
331+
# Check null value
332+
(None, ",", []),
333+
# Check empty value
334+
("", ",", []),
335+
# Check string value
336+
("a", ",", ["a"]),
337+
# Check comma separated string value
338+
("a,b,c", ",", ["a", "b", "c"]),
339+
# Check comma separated string value with whitespaces
340+
(" a, b,c ", ",", ["a", "b", "c"]),
341+
]
342+
)
343+
def test_splitAndStrip(value, separator, output):
344+
result = environ.splitAndStrip(value, separator)
345+
assert result == output
346+
310347
@pytest.mark.parametrize(("default_yml", "os_env", "output"),
311348
[
312349
# Check null parameters
313-
({}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {}}),
350+
({}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {}}),
314351
# Check ansible_pre_tasks using defaults or env vars
315-
({"ansible_pre_tasks": ""}, {}, {"ansible_pre_tasks": "", "ansible_post_tasks": None, "ansible_environment": {}}),
316-
({"ansible_pre_tasks": "a"}, {}, {"ansible_pre_tasks": "a", "ansible_post_tasks": None, "ansible_environment": {}}),
317-
({"ansible_pre_tasks": "a,b,c"}, {}, {"ansible_pre_tasks": "a,b,c", "ansible_post_tasks": None, "ansible_environment": {}}),
318-
({}, {"SPLUNK_ANSIBLE_PRE_TASKS": "d"}, {"ansible_pre_tasks": "d", "ansible_post_tasks": None, "ansible_environment": {}}),
319-
({}, {"SPLUNK_ANSIBLE_PRE_TASKS": "e,f,g"}, {"ansible_pre_tasks": "e,f,g", "ansible_post_tasks": None, "ansible_environment": {}}),
320-
({"ansible_pre_tasks": "a,b,c"}, {"SPLUNK_ANSIBLE_PRE_TASKS": "e,f,g"}, {"ansible_pre_tasks": "e,f,g", "ansible_post_tasks": None, "ansible_environment": {}}),
352+
({"ansible_pre_tasks": ""}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {}}),
353+
({"ansible_pre_tasks": None}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {}}),
354+
({"ansible_pre_tasks": "a"}, {}, {"ansible_pre_tasks": ["a"], "ansible_post_tasks": [], "ansible_environment": {}}),
355+
({"ansible_pre_tasks": ["a"]}, {}, {"ansible_pre_tasks": ["a"], "ansible_post_tasks": [], "ansible_environment": {}}),
356+
({"ansible_pre_tasks": "a,b,c"}, {}, {"ansible_pre_tasks": ["a","b","c"], "ansible_post_tasks": [], "ansible_environment": {}}),
357+
({"ansible_pre_tasks": ["a","b","c"]}, {}, {"ansible_pre_tasks": ["a","b","c"], "ansible_post_tasks": [], "ansible_environment": {}}),
358+
({}, {"SPLUNK_ANSIBLE_PRE_TASKS": "d"}, {"ansible_pre_tasks": ["d"], "ansible_post_tasks": [], "ansible_environment": {}}),
359+
({}, {"SPLUNK_ANSIBLE_PRE_TASKS": "e,f,g"}, {"ansible_pre_tasks": ["e","f","g"], "ansible_post_tasks": [], "ansible_environment": {}}),
360+
({"ansible_pre_tasks": "a,b,c"}, {"SPLUNK_ANSIBLE_PRE_TASKS": "e,f,g"}, {"ansible_pre_tasks": ["e","f","g"], "ansible_post_tasks": [], "ansible_environment": {}}),
361+
({"ansible_pre_tasks": ["a","b","c"]}, {"SPLUNK_ANSIBLE_PRE_TASKS": "e,f,g"}, {"ansible_pre_tasks": ["e","f","g"], "ansible_post_tasks": [], "ansible_environment": {}}),
321362
# Check ansible_post_tasks using defaults or env vars
322-
({"ansible_post_tasks": ""}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": "", "ansible_environment": {}}),
323-
({"ansible_post_tasks": "a"}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": "a", "ansible_environment": {}}),
324-
({"ansible_post_tasks": "a,b,c"}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": "a,b,c", "ansible_environment": {}}),
325-
({}, {"SPLUNK_ANSIBLE_POST_TASKS": "d"}, {"ansible_pre_tasks": None, "ansible_post_tasks": "d", "ansible_environment": {}}),
326-
({}, {"SPLUNK_ANSIBLE_POST_TASKS": "e,f,g"}, {"ansible_pre_tasks": None, "ansible_post_tasks": "e,f,g", "ansible_environment": {}}),
327-
({"ansible_post_tasks": "a,b,c"}, {"SPLUNK_ANSIBLE_POST_TASKS": "e,f,g"}, {"ansible_pre_tasks": None, "ansible_post_tasks": "e,f,g", "ansible_environment": {}}),
363+
({"ansible_post_tasks": ""}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {}}),
364+
({"ansible_post_tasks": None}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {}}),
365+
({"ansible_post_tasks": "a"}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["a"], "ansible_environment": {}}),
366+
({"ansible_post_tasks": ["a"]}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["a"], "ansible_environment": {}}),
367+
({"ansible_post_tasks": "a,b,c"}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["a","b","c"], "ansible_environment": {}}),
368+
({"ansible_post_tasks": ["a","b","c"]}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["a","b","c"], "ansible_environment": {}}),
369+
({}, {"SPLUNK_ANSIBLE_POST_TASKS": "d"}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["d"], "ansible_environment": {}}),
370+
({}, {"SPLUNK_ANSIBLE_POST_TASKS": "e,f,g"}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["e","f","g"], "ansible_environment": {}}),
371+
({"ansible_post_tasks": "a,b,c"}, {"SPLUNK_ANSIBLE_POST_TASKS": "e,f,g"}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["e","f","g"], "ansible_environment": {}}),
372+
({"ansible_post_tasks": ["a","b","c"]}, {"SPLUNK_ANSIBLE_POST_TASKS": "e,f,g"}, {"ansible_pre_tasks": [], "ansible_post_tasks": ["e","f","g"], "ansible_environment": {}}),
328373
# Check ansible_environment using defaults or env vars
329-
({"ansible_environment": None}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {}}),
330-
({"ansible_environment": {"a": "b"}}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {"a": "b"}}),
331-
({"ansible_environment": {"a": "b", "d": "e"}}, {}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {"a": "b", "d": "e"}}),
332-
({}, {"SPLUNK_ANSIBLE_ENV": "a=b"}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {"a": "b"}}),
333-
({}, {"SPLUNK_ANSIBLE_ENV": "a=b,x=y"}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {"a": "b", "x": "y"}}),
334-
({"ansible_environment": {"a": "c", "d": "e"}}, {"SPLUNK_ANSIBLE_ENV": "a=b,x=y"}, {"ansible_pre_tasks": None, "ansible_post_tasks": None, "ansible_environment": {"a": "b", "d": "e", "x": "y"}}),
374+
({"ansible_environment": None}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {}}),
375+
({"ansible_environment": {"a": "b"}}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {"a": "b"}}),
376+
({"ansible_environment": {"a": "b", "d": "e"}}, {}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {"a": "b", "d": "e"}}),
377+
({}, {"SPLUNK_ANSIBLE_ENV": "a=b"}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {"a": "b"}}),
378+
({}, {"SPLUNK_ANSIBLE_ENV": "a=b,x=y"}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {"a": "b", "x": "y"}}),
379+
({"ansible_environment": {"a": "c", "d": "e"}}, {"SPLUNK_ANSIBLE_ENV": "a=b,x=y"}, {"ansible_pre_tasks": [], "ansible_post_tasks": [], "ansible_environment": {"a": "b", "d": "e", "x": "y"}}),
335380
]
336381
)
337382
def test_getAnsibleContext(default_yml, os_env, output):

0 commit comments

Comments
 (0)