Skip to content

Commit ca4b988

Browse files
caladdbsanders
authored andcommitted
FEATURE: Ansible module for storage partition info
An Ansible module for returning storage partition info for a given scope. The module takes two optional parameters: `scope` for specifying the scope of the data to return. Valid choices are: `global` (default), `appliance`, `os`, `environment`, and `host`. `name` for requesting the data for a specific item in the scope. If name is not provided, then all data for that scope is returned. The data returned is a list of `partitions`. Example playbook: ``` --- - hosts: localhost tasks: - name: Get global partition info stacki_storage_partition_info: register: result - name: Global partition output debug: var: result - name: Get host partition output for backend-0-0 stacki_storage_partition_info: name: backend-0-0 scope: host register: result - name: Host backend-0-0 partition output debug: var: result ``` Output of the debug commands, showing the structure of the data returned: ``` TASK [Global partition output] ********************************************************************************************************************************* ok: [localhost] => { "result": { "changed": false, "failed": false, "partitions": [ { "device": "/dev/sda1", "fstype": "ext4", "mountpoint": "/", "options": "", "partid": 1, "size": "10000" } ] } } TASK [Host backend-0-0 partition output] *********************************************************************************************************************** ok: [localhost] => { "result": { "changed": false, "failed": false, "partitions": [ { "device": "/dev/sda1", "fstype": "ext4", "host": "backend-0-0", "mountpoint": "/", "options": "", "partid": 1, "size": "10000", "source": "H" } ] } } ```
1 parent 8abc58f commit ca4b988

2 files changed

Lines changed: 418 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
DOCUMENTATION = """
8+
module: stacki_storage_partition_info
9+
short_description: Return data about Stacki storage partitions
10+
description:
11+
- If name is supplied, and scope is not global, returns data about a single scoped item
12+
- If name is supplied, and scope is global, then an error is returned
13+
- If name is not supplied, and scope is not global, then all data in that scope is returned
14+
- If name is not supplied, and scope is global, then all global data is returned
15+
16+
options:
17+
name:
18+
description:
19+
- The name of the scoped item to return data about
20+
type: str
21+
required: false
22+
23+
scope:
24+
description:
25+
- The scope to return data about
26+
type: str
27+
required: false
28+
choices: ['global', 'appliance', 'os', 'environment', 'host']
29+
default: global
30+
"""
31+
32+
EXAMPLES = """
33+
- name: Get all global data
34+
stacki_storage_partition_info:
35+
register: results
36+
37+
- name: Get data about backend appliance
38+
stacki_storage_partition_info:
39+
name: backend
40+
scope: appliance
41+
register: results
42+
43+
- name: Get data about all hosts
44+
stacki_storage_partition_info:
45+
scope: host
46+
register: results
47+
"""
48+
49+
RETURN = """
50+
partitions:
51+
description:
52+
- List of storage partitions
53+
returned: on success
54+
type: complex
55+
contains:
56+
appliance:
57+
description:
58+
- Name of the appliance for this data
59+
type: str
60+
returned: scope is appliance
61+
62+
os:
63+
description:
64+
- Name of the os for this data
65+
type: str
66+
returned: scope is os
67+
68+
environment:
69+
description:
70+
- Name of the environment for this data
71+
type: str
72+
returned: scope is environment
73+
74+
host:
75+
description:
76+
- Name of the host for this data
77+
type: str
78+
returned: scope is host
79+
80+
device:
81+
description:
82+
- Disk device of the partition
83+
type: str
84+
85+
partid:
86+
description:
87+
- Partition ID or None
88+
type: int
89+
90+
mountpoint:
91+
description:
92+
- Mount point of the partition
93+
type: str
94+
95+
size:
96+
description:
97+
- Size of the partition in megabytes as a string, or possibly 'recommended' or 'hibernation' if mountpoint is 'swap'
98+
type: str
99+
100+
fstype:
101+
description:
102+
- File system type of the partition
103+
type: str
104+
105+
options:
106+
description:
107+
- Partition options
108+
type: str
109+
110+
source:
111+
description:
112+
- The scope source of the data
113+
type: str
114+
choices: ['G', 'A', 'O', 'E', 'H']
115+
returned: scope is host
116+
"""
117+
118+
from ansible.module_utils.basic import AnsibleModule
119+
from ansible.module_utils.stacki import run_stack_command, StackCommandError
120+
121+
122+
def main():
123+
# Define the arguments for this module
124+
argument_spec = dict(
125+
name=dict(type="str", required=False, default=None),
126+
scope=dict(
127+
type="str", required=False, default="global",
128+
choices=["global", "appliance", "os", "environment", "host"]
129+
)
130+
)
131+
132+
# Create our module object
133+
module = AnsibleModule(
134+
argument_spec=argument_spec,
135+
supports_check_mode=True
136+
)
137+
138+
# Initialize a blank result
139+
result = {
140+
"changed": False,
141+
"partitions": []
142+
}
143+
144+
# Bail if the user is just checking syntax of their playbook
145+
if module.check_mode:
146+
module.exit_json(**result)
147+
148+
# Fetch our info from Stacki
149+
args = ["scope=" + module.params["scope"]]
150+
151+
if module.params["name"]:
152+
args.append(module.params["name"])
153+
154+
try:
155+
for partition in run_stack_command("list.storage.partition", args):
156+
# Make sure size is a string
157+
partition["size"] = str(partition["size"])
158+
159+
# Add it to the results
160+
result["partitions"].append(partition)
161+
162+
except StackCommandError as e:
163+
# Fetching the data failed
164+
module.fail_json(msg=e.message, **result)
165+
166+
# Return our data
167+
module.exit_json(**result)
168+
169+
170+
if __name__ == "__main__":
171+
main()

0 commit comments

Comments
 (0)