Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_banner.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_banner
20 version_added: '1.0.0'
21 short_description: Configure Pure Storage FlashArray GUI and SSH MOTD message
22 description:
23 - Configure MOTD for Pure Storage FlashArrays.
24 - This will be shown during an SSH or GUI login to the array.
25 - Multiple line messages can be achieved using \\n.
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 state:
30 description:
31 - Set or delete the MOTD
32 default: present
33 type: str
34 choices: [ present, absent ]
35 banner:
36 description:
37 - Banner text, or MOTD, to use
38 type: str
39 default: "Welcome to the machine..."
40 context:
41 description:
42 - Name of fleet member on which to perform the operation.
43 - This requires the array receiving the request is a member of a fleet
44 and the context name to be a member of the same fleet.
45 type: str
46 default: ""
47 version_added: '1.39.0'
48 extends_documentation_fragment:
49 - purestorage.flasharray.purestorage.fa
50 """
51
52 EXAMPLES = r"""
53 - name: Set new banner text
54 purestorage.flasharray.purefa_banner:
55 banner: "Banner over\ntwo lines"
56 state: present
57 fa_url: 10.10.10.2
58 api_token: e31060a7-21fc-e277-6240-25983c6c4592
59
60 - name: Delete banner text
61 purestorage.flasharray.purefa_banner:
62 state: absent
63 fa_url: 10.10.10.2
64 api_token: e31060a7-21fc-e277-6240-25983c6c4592
65 """
66
67 RETURN = r"""
68 """
69
70 from ansible.module_utils.basic import AnsibleModule
71 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
72 get_array,
73 purefa_argument_spec,
74 )
75 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
76 LooseVersion,
77 )
78
79 HAS_PURESTORAGE = True
80 try:
81 from pypureclient.flasharray import Arrays
82 except ImportError:
83 HAS_PURESTORAGE = False
84
85 CONTEXT_VERSION = "2.38"
86
87
88 def set_banner(module, array):
89 """Set MOTD banner text"""
90 changed = True
91 api_version = array.get_rest_version()
92 if not module.params["banner"]:
93 module.fail_json(msg="Invalid MOTD banner given")
94 if not module.check_mode:
95 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
96 res = array.patch_arrays(
97 array=Arrays(banner=module.params["banner"]),
98 context_names=[module.params["context"]],
99 )
100 else:
101 res = array.patch_arrays(array=Arrays(banner=module.params["banner"]))
102 if res.status_code != 200:
103 module.fail_json(msg="Failed to set MOTD banner text")
104
105 module.exit_json(changed=changed)
106
107
108 def delete_banner(module, array):
109 """Delete MOTD banner text"""
110 changed = True
111 api_version = array.get_rest_version()
112 if not module.check_mode:
113 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
114 res = array.patch_arrays(
115 array=Arrays(banner=""), context_names=[module.params["context"]]
116 )
117 else:
118 res = array.patch_arrays(array=Arrays(banner=""))
119 if res.status_code != 200:
120 module.fail_json(msg="Failed to delete current MOTD banner text")
121 module.exit_json(changed=changed)
122
123
124 def main():
125 argument_spec = purefa_argument_spec()
126 argument_spec.update(
127 dict(
128 banner=dict(type="str", default="Welcome to the machine..."),
129 state=dict(type="str", default="present", choices=["present", "absent"]),
130 context=dict(type="str", default=""),
131 )
132 )
133
134 required_if = [("state", "present", ["banner"])]
135
136 module = AnsibleModule(
137 argument_spec, required_if=required_if, supports_check_mode=True
138 )
139
140 if not HAS_PURESTORAGE:
141 module.fail_json(msg="py-pure-client sdk is required for this module")
142
143 state = module.params["state"]
144 array = get_array(module)
145 api_version = array.get_rest_version()
146 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
147 current_banner = list(
148 array.get_arrays(context_names=[module.params["context"]]).items
149 )[0].banner
150 else:
151 current_banner = list(array.get_arrays().items)[0].banner
152 # set banner if empty value or value differs
153 if state == "present" and (
154 not current_banner or current_banner != module.params["banner"]
155 ):
156 set_banner(module, array)
157 # clear banner if it has a value
158 elif state == "absent" and current_banner:
159 delete_banner(module, array)
160
161 module.exit_json(changed=False)
162
163
164 if __name__ == "__main__":
165 main()