Skip to content

Commit 8bb5395

Browse files
caladdbsanders
authored andcommitted
FEATURE: Ansible module for getting box info
An Ansible module for returning box info from the Stacki database. The info returned is the same as `list box`. The module takes `name` as an optional parameter to filter the returned data by box name. The data returned is a list of `boxes`. Example playbook: ``` --- - hosts: localhost tasks: - name: Get info on all the boxes stacki_box_info: register: result - name: Multiple boxes output debug: var: result - name: Get the frontend box info stacki_box_info: name: frontend register: result - name: Single box output debug: var: result ``` Output of the debug commands, showing the structure of the data returned: ``` TASK [Multiple boxes output] *********************************** ok: [localhost] => { "result": { "boxes": [ { "carts": [ "vagrant" ], "name": "default", "os": "sles", "pallets": [ "stacki-5.6rc1_20200724_24b0c82-sles12", "SLES-12sp3-sles12" ], "repos": [] }, { "carts": [], "name": "frontend", "os": "sles", "pallets": [ "stacki-5.6rc1_20200724_24b0c82-sles12", "SLES-12sp3-sles12" ], "repos": [] } ], "changed": false, "failed": false } } TASK [Single box output] *********************************** ok: [localhost] => { "result": { "boxes": [ { "carts": [], "name": "frontend", "os": "sles", "pallets": [ "stacki-5.6rc1_20200724_24b0c82-sles12", "SLES-12sp3-sles12" ], "repos": [] } ], "changed": false, "failed": false } } ```
1 parent 11e8d25 commit 8bb5395

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_box_info
9+
short_description: Return data about Stacki boxes
10+
description:
11+
- If name is supplied, returns data about a single box
12+
- If name is not supplied, returns data about all boxes in the system
13+
14+
options:
15+
name:
16+
description:
17+
- The name of the box to return data about
18+
required: false
19+
"""
20+
21+
EXAMPLES = """
22+
- name: Get box default
23+
stacki_box_info:
24+
name: default
25+
register: default_box
26+
27+
- name: Get all boxes
28+
stacki_box_info:
29+
register: boxes
30+
"""
31+
32+
RETURN = """
33+
boxes:
34+
description:
35+
- List of boxes
36+
returned: on success
37+
type: complex
38+
contains:
39+
name:
40+
description:
41+
- Name of the box
42+
type: str
43+
44+
os:
45+
description:
46+
- OS of the box
47+
type: str
48+
49+
pallets:
50+
description:
51+
- Pallets in the box
52+
type: list
53+
elements: str
54+
55+
carts:
56+
description:
57+
- Carts in the box
58+
type: list
59+
elements: str
60+
61+
repos:
62+
description:
63+
- Repos in the box
64+
type: list
65+
elements: str
66+
"""
67+
68+
from ansible.module_utils.basic import AnsibleModule
69+
from ansible.module_utils.stacki import run_stack_command, StackCommandError
70+
71+
72+
def main():
73+
# Define the arguments for this module
74+
argument_spec = dict(
75+
name=dict(type="str", required=False, default=None)
76+
)
77+
78+
# Create our module object
79+
module = AnsibleModule(
80+
argument_spec=argument_spec,
81+
supports_check_mode=True
82+
)
83+
84+
# Initialize a blank result
85+
result = {
86+
"changed": False,
87+
"boxes": []
88+
}
89+
90+
# Bail if the user is just checking syntax of their playbook
91+
if module.check_mode:
92+
module.exit_json(**result)
93+
94+
# Fetch our box info from Stacki
95+
args = []
96+
if module.params["name"]:
97+
args.append(module.params["name"])
98+
99+
try:
100+
for box in run_stack_command("list.box", args):
101+
# Split pallets, carts, and repos into lists
102+
box["pallets"] = box["pallets"].split()
103+
box["carts"] = box["carts"].split()
104+
box["repos"] = box["repos"].split()
105+
106+
# Add it to the results
107+
result["boxes"].append(box)
108+
109+
except StackCommandError as e:
110+
# Fetching the data failed
111+
module.fail_json(msg=e.message, **result)
112+
113+
# Return our data
114+
module.exit_json(**result)
115+
116+
117+
if __name__ == "__main__":
118+
main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class TestStackiBoxInfo:
2+
def test_no_name(self, run_ansible_module):
3+
result = run_ansible_module("stacki_box_info")
4+
5+
assert result.status == "SUCCESS"
6+
assert result.data["changed"] == False
7+
8+
assert len(result.data["boxes"]) == 2
9+
10+
def test_with_name(self, run_ansible_module):
11+
result = run_ansible_module("stacki_box_info", name="default")
12+
13+
assert result.status == "SUCCESS"
14+
assert result.data["changed"] == False
15+
16+
assert len(result.data["boxes"]) == 1
17+
assert result.data["boxes"][0]["name"] == "default"
18+
19+
def test_bad_name(self, run_ansible_module):
20+
result = run_ansible_module("stacki_box_info", name="foo")
21+
22+
assert result.status == "FAILED!"
23+
assert result.data["changed"] == False
24+
25+
assert "error" in result.data["msg"]
26+
assert "not a valid box" in result.data["msg"]

0 commit comments

Comments
 (0)