| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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_vnc |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Enable or Disable VNC port for installed apps |
| 22 |
description: |
| 23 |
- Enablke or Disable VNC access for installed apps |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Define state of VNC |
| 30 |
type: str |
| 31 |
default: present |
| 32 |
choices: [ present, absent ] |
| 33 |
name: |
| 34 |
description: |
| 35 |
- Name of app |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
extends_documentation_fragment: |
| 39 |
- purestorage.flasharray.purestorage.fa |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = r""" |
| 43 |
- name: Enable VNC for application test |
| 44 |
purestorage.flasharray.purefa_vnc: |
| 45 |
name: test |
| 46 |
fa_url: 10.10.10.2 |
| 47 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 48 |
|
| 49 |
- name: Disable VNC for application test |
| 50 |
purestorage.flasharray.purefa_vnc: |
| 51 |
name: test |
| 52 |
state: absent |
| 53 |
fa_url: 10.10.10.2 |
| 54 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 55 |
""" |
| 56 |
|
| 57 |
RETURN = r""" |
| 58 |
vnc: |
| 59 |
description: VNC port information for application |
| 60 |
type: dict |
| 61 |
returned: success |
| 62 |
contains: |
| 63 |
status: |
| 64 |
description: Status of application |
| 65 |
type: str |
| 66 |
sample: 'healthy' |
| 67 |
index: |
| 68 |
description: Application index number |
| 69 |
type: int |
| 70 |
version: |
| 71 |
description: Application version installed |
| 72 |
type: str |
| 73 |
sample: '5.2.1' |
| 74 |
vnc: |
| 75 |
description: IP address and port number for VNC connection |
| 76 |
type: list |
| 77 |
sample: ['10.21.200.34:5900'] |
| 78 |
name: |
| 79 |
description: Application name |
| 80 |
type: str |
| 81 |
""" |
| 82 |
|
| 83 |
HAS_PURESTORAGE = True |
| 84 |
try: |
| 85 |
from pypureclient.flasharray import App |
| 86 |
except ImportError: |
| 87 |
HAS_PURESTORAGE = False |
| 88 |
|
| 89 |
from ansible.module_utils.basic import AnsibleModule |
| 90 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 91 |
get_array, |
| 92 |
purefa_argument_spec, |
| 93 |
) |
| 94 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 95 |
LooseVersion, |
| 96 |
) |
| 97 |
|
| 98 |
MAX_API_VERSION = "2.36" |
| 99 |
|
| 100 |
|
| 101 |
def enable_vnc(module, array, app): |
| 102 |
"""Enable VNC port""" |
| 103 |
changed = False |
| 104 |
vnc_fact = [] |
| 105 |
if not app.vnc_enabled: |
| 106 |
changed = True |
| 107 |
if not module.check_mode: |
| 108 |
res = array.patch_apps( |
| 109 |
names=[module.params["name"]], app=App(vnc_enabled=True) |
| 110 |
) |
| 111 |
if res.status_code == 200: |
| 112 |
vnc_nodes = list( |
| 113 |
array.get_apps_nodes(app_names=[module.params["name"]]).items |
| 114 |
)[0] |
| 115 |
vnc_fact = { |
| 116 |
"status": vnc_nodes.status, |
| 117 |
"index": vnc_nodes.index, |
| 118 |
"version": vnc_nodes.version, |
| 119 |
"vnc": vnc_nodes.vnc, |
| 120 |
"name": module.params["name"], |
| 121 |
} |
| 122 |
else: |
| 123 |
module.fail_json( |
| 124 |
msg="Enabling VNC for {0} failed. Error: {1}".format( |
| 125 |
module.params["name"], res.errors[0].message |
| 126 |
) |
| 127 |
) |
| 128 |
module.exit_json(changed=changed, vnc=vnc_fact) |
| 129 |
|
| 130 |
|
| 131 |
def disable_vnc(module, array, app): |
| 132 |
"""Disable VNC port""" |
| 133 |
changed = False |
| 134 |
if app.vnc_enabled: |
| 135 |
changed = True |
| 136 |
if not module.check_mode: |
| 137 |
res = array.patch_apps( |
| 138 |
names=[module.params["name"]], app=App(vnc_enabled=False) |
| 139 |
) |
| 140 |
if res.status_code != 200: |
| 141 |
module.fail_json( |
| 142 |
msg="Disabling VNC for {0} failed. Error: {1}".format( |
| 143 |
module.params["name"], res.errors[0].message |
| 144 |
) |
| 145 |
) |
| 146 |
module.exit_json(changed=changed) |
| 147 |
|
| 148 |
|
| 149 |
def main(): |
| 150 |
argument_spec = purefa_argument_spec() |
| 151 |
argument_spec.update( |
| 152 |
dict( |
| 153 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 154 |
name=dict(type="str", required=True), |
| 155 |
) |
| 156 |
) |
| 157 |
|
| 158 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 159 |
|
| 160 |
if not HAS_PURESTORAGE: |
| 161 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 162 |
|
| 163 |
array = get_array(module) |
| 164 |
api_version = array.get_rest_version() |
| 165 |
if LooseVersion(MAX_API_VERSION) <= LooseVersion(api_version): |
| 166 |
module.warn("VNC feature deprecated from Purity//FA 6.8.0 and higher") |
| 167 |
module.exit_json(changed=False) |
| 168 |
res = array.get_apps(names=[module.params["name"]]) |
| 169 |
if res.status_code != 200: |
| 170 |
module.fail_json( |
| 171 |
msg="Selected application {0} does not exist".format(module.params["name"]) |
| 172 |
) |
| 173 |
app = list(res.items)[0] |
| 174 |
if not app.enabled: |
| 175 |
module.fail_json( |
| 176 |
msg="Application {0} is not enabled".format(module.params["name"]) |
| 177 |
) |
| 178 |
if module.params["state"] == "present": |
| 179 |
enable_vnc(module, array, app) |
| 180 |
else: |
| 181 |
disable_vnc(module, array, app) |
| 182 |
module.exit_json(changed=False) |
| 183 |
|
| 184 |
|
| 185 |
if __name__ == "__main__": |
| 186 |
main() |