Skip to content

Commit 0b573f0

Browse files
cjy008bsanders
authored andcommitted
FEATURE: Ansible module for getting group info
1 parent 23888bc commit 0b573f0

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# @copyright@
2+
# Copyright (c) 2006 - 2020 Teradata
3+
# All rights reserved. Stacki(r) v5.x stacki.com
4+
# https://github.com/Teradata/stacki/blob/master/LICENSE.txt
5+
# @copyright@
6+
7+
from ansible.module_utils.basic import AnsibleModule
8+
from ansible.module_utils.stacki import run_stack_command, StackCommandError
9+
10+
DOCUMENTATION = """
11+
module: stacki_group_info
12+
short_description: Return data about Stacki groups
13+
description: List the current groups and the number of member hosts in each.
14+
"""
15+
16+
EXAMPLES = """
17+
- name: Get info on all the groups
18+
stacki_group_info:
19+
register: result
20+
"""
21+
22+
RETURN = """
23+
groups:
24+
description:
25+
- List of groups
26+
returned_on: success
27+
type: complex
28+
contains:
29+
group:
30+
description:
31+
- Name of the group
32+
type: str
33+
hosts:
34+
description:
35+
- List of hosts in the given group
36+
type: list
37+
elements: str
38+
"""
39+
40+
41+
def main():
42+
# Create our module object
43+
module = AnsibleModule(
44+
argument_spec=dict(),
45+
supports_check_mode=True
46+
)
47+
48+
# Initialize a blank result
49+
result = {
50+
"changed": False,
51+
"groups": []
52+
}
53+
54+
# Bail if the user is just checking syntax of their playbook
55+
if module.check_mode:
56+
module.exit_json(**result)
57+
58+
try:
59+
for group in run_stack_command("list.group"):
60+
group["hosts"] = group["hosts"].split()
61+
62+
# Add it to the results
63+
result["groups"].append(group)
64+
65+
except StackCommandError as e:
66+
# Fetching the data failed
67+
module.fail_json(msg=e.message, **result)
68+
69+
# Return our data
70+
module.exit_json(**result)
71+
72+
73+
if __name__ == "__main__":
74+
main()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TestStackiGroupInfo:
2+
def test_no_groups(self, run_ansible_module):
3+
result = run_ansible_module("stacki_group_info")
4+
5+
assert result.status == "SUCCESS"
6+
assert result.data["changed"] is False
7+
assert len(result.data["groups"]) == 0
8+
9+
def test_single_groups(self, add_group, run_ansible_module):
10+
result = run_ansible_module("stacki_group_info")
11+
12+
assert result.status == "SUCCESS"
13+
assert result.data["changed"] is False
14+
assert len(result.data["groups"]) == 1
15+
16+
def test_multiple_groups(self, host, add_group, add_host, run_ansible_module):
17+
add_group("test2")
18+
add_host("backend-0-1", "0", "1", "backend")
19+
20+
result = host.run("stack add host group backend-0-0 group=test")
21+
22+
assert result.rc == 0
23+
24+
result = host.run("stack add host group backend-0-1 group=test")
25+
26+
assert result.rc == 0
27+
28+
result = host.run("stack add host group backend-0-1 group=test2")
29+
30+
assert result.rc == 0
31+
32+
result = run_ansible_module("stacki_group_info")
33+
34+
assert result.status == "SUCCESS"
35+
assert result.data["changed"] is False
36+
assert len(result.data["groups"]) == 2
37+
38+
assert result.data["groups"][0]["group"] == "test"
39+
assert len(result.data["groups"][0]["hosts"]) == 2
40+
41+
assert result.data["groups"][1]["group"] == "test2"
42+
assert len(result.data["groups"][1]["hosts"]) == 1

0 commit comments

Comments
 (0)