Skip to content

Commit d519324

Browse files
caladdbsanders
authored andcommitted
FEATURE: Ansible module for attribute info
An Ansible module for returning attribute info for a given scope. The module takes several 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. `attr` to specify attributes to return data about. Can be a shell syntax glob pattern. `shadow` to control if shadow attributes are returned in the data. Defaults to True. The data returned is a list of `attributes`. Example playbook: ``` --- - hosts: localhost tasks: - name: Get attributes for the backend appliance stacki_attribute_info: name: backend scope: appliance register: result - name: Appliance attribute output debug: var: result - name: Get the os.version attribute for backend-0-0 stacki_attribute_info: name: backend-0-0 scope: host attr: os.version register: result - name: Host backend-0-0 attribute output debug: var: result ``` Output of the debug commands, showing the structure of the data returned: ``` TASK [Appliance attribute output] *************************************************************************************************************** ok: [localhost] => { "result": { "attributes": [ { "appliance": "backend", "attr": "kickstartable", "scope": "appliance", "type": "var", "value": "True" }, { "appliance": "backend", "attr": "managed", "scope": "appliance", "type": "var", "value": "True" }, { "appliance": "backend", "attr": "node", "scope": "appliance", "type": "var", "value": "backend" } ], "changed": false, "failed": false } } TASK [Host backend-0-0 attribute output] ******************************************************************************************************** ok: [localhost] => { "result": { "attributes": [ { "attr": "os.version", "host": "backend-0-0", "scope": "host", "type": "const", "value": "12.x" } ], "changed": false, "failed": false } } ```
1 parent eb8292f commit d519324

File tree

2 files changed

+509
-0
lines changed

2 files changed

+509
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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_attribute_info
9+
short_description: Return data about Stacki attributes
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+
attr:
32+
description:
33+
- A shell syntax glob pattern to specify attributes to return data about
34+
type: str
35+
required: false
36+
37+
shadow:
38+
description:
39+
- Should shadow attributes be returned along with non-shadow attributes
40+
type: bool
41+
required: false
42+
default: yes
43+
"""
44+
45+
EXAMPLES = """
46+
- name: Get all global data
47+
stacki_attribute_info:
48+
register: results
49+
50+
- name: Get data about backend appliance
51+
stacki_attribute_info:
52+
name: backend
53+
scope: appliance
54+
register: results
55+
56+
- name: Get the os.version attribute for backend-0-0
57+
stacki_attribute_info:
58+
name: backend-0-0
59+
scope: host
60+
attr: os.version
61+
register: result
62+
"""
63+
64+
RETURN = """
65+
attributes:
66+
description:
67+
- List of attributes
68+
returned: on success
69+
type: complex
70+
contains:
71+
appliance:
72+
description:
73+
- Name of the appliance for this data
74+
type: str
75+
returned: scope is appliance
76+
77+
os:
78+
description:
79+
- Name of the os for this data
80+
type: str
81+
returned: scope is os
82+
83+
environment:
84+
description:
85+
- Name of the environment for this data
86+
type: str
87+
returned: scope is environment
88+
89+
host:
90+
description:
91+
- Name of the host for this data
92+
type: str
93+
returned: scope is host
94+
95+
scope:
96+
description:
97+
- The scope source of the data
98+
type: str
99+
choices: ['global', 'appliance', 'os', 'environment', 'host']
100+
101+
type:
102+
description:
103+
- The type of the attribute
104+
type: str
105+
choices: ['const', 'shadow', 'var']
106+
107+
attr:
108+
description:
109+
- The attribute name
110+
type: str
111+
112+
value:
113+
description:
114+
- The value assigned to the attribute
115+
type: str
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+
attr=dict(type="str", required=False, default=None),
131+
shadow=dict(type="bool", required=False, default=True)
132+
)
133+
134+
# Create our module object
135+
module = AnsibleModule(
136+
argument_spec=argument_spec,
137+
supports_check_mode=True
138+
)
139+
140+
# Initialize a blank result
141+
result = {
142+
"changed": False,
143+
"attributes": []
144+
}
145+
146+
# Bail if the user is just checking syntax of their playbook
147+
if module.check_mode:
148+
module.exit_json(**result)
149+
150+
# Fetch our info from Stacki
151+
args = ["scope=" + module.params["scope"]]
152+
153+
if module.params["name"]:
154+
args.append(module.params["name"])
155+
156+
for field in ("attr", "shadow"):
157+
if module.params[field] is not None:
158+
args.append(f"{field}={module.params[field]}")
159+
160+
try:
161+
for attribute in run_stack_command("list.attr", args):
162+
# Make sure attribute value is a string
163+
attribute["value"] = str(attribute["value"])
164+
165+
# Add it to the results
166+
result["attributes"].append(attribute)
167+
168+
except StackCommandError as e:
169+
# Fetching the data failed
170+
module.fail_json(msg=e.message, **result)
171+
172+
# Return our data
173+
module.exit_json(**result)
174+
175+
176+
if __name__ == "__main__":
177+
main()

0 commit comments

Comments
 (0)