-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathstacki_group.py
More file actions
101 lines (82 loc) · 2.28 KB
/
stacki_group.py
File metadata and controls
101 lines (82 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# @copyright@
# Copyright (c) 2006 - 2020 Teradata
# All rights reserved. Stacki(r) v5.x stacki.com
# https://github.com/Teradata/stacki/blob/master/LICENSE.txt
# @copyright@
DOCUMENTATION = """
module: stacki_group
short_description: Manage Stacki groups
description:
- Add and remove Stacki groups
options:
name:
description:
- The name of the group to manage
required: true
type: str
state:
description:
- If present, then an group will be added (if needed)
- If absent, then the group will be removed
type: str
choices: [ absent, present ]
default: present
"""
EXAMPLES = """
- name: Add a group
stacki_group:
name: test
- name: Remove a group
stacki_group:
name: test
state: absent
"""
RETURN = """ # """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.stacki import run_stack_command, StackCommandError
def main():
# Define the arguments for this module
argument_spec = dict(
name=dict(type="str", required=True),
state=dict(type="str", default="present", choices=["absent", "present"])
)
# Create our module object
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True
)
# Initialize a blank result
result = {
"changed": False
}
# Bail if the user is just checking syntax of their playbook
if module.check_mode:
module.exit_json(**result)
# Fetch our group info from Stacki
try:
groups = run_stack_command("list.group", [module.params["name"]])
except StackCommandError as e:
# If the group doesn't exist, it will raise an error
groups = []
if len(groups) > 1:
# No more than one group should match
module.fail_json(msg="error - more than one group matches name", **result)
try:
# Are we adding or removing?
if module.params["state"] == "present":
if len(groups) == 0:
# Adding a new group
run_stack_command("add.group", [module.params["name"]])
result["changed"] = True
else:
# Only remove an group that actually exists
if len(groups):
run_stack_command("remove.group", [module.params["name"]])
result["changed"] = True
except StackCommandError as e:
# Fetching the data failed
module.fail_json(msg=e.message, **result)
# Return our data
module.exit_json(**result)
if __name__ == "__main__":
main()