| 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_proxy |
| 20 |
version_added: '1.0.0' |
| 21 |
author: |
| 22 |
- Pure Storage ansible Team (@sdodsley) <[email protected]> |
| 23 |
short_description: Configure FlashArray phonehome HTTPs proxy settings |
| 24 |
description: |
| 25 |
- Set or erase configuration for the HTTPS phonehome proxy settings. |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Set or delete proxy configuration |
| 30 |
default: present |
| 31 |
type: str |
| 32 |
choices: [ absent, present ] |
| 33 |
protocol: |
| 34 |
description: |
| 35 |
- The proxy protocol. |
| 36 |
choices: [http, https ] |
| 37 |
default: https |
| 38 |
type: str |
| 39 |
version_added: '1.20.0' |
| 40 |
host: |
| 41 |
description: |
| 42 |
- The proxy host name. |
| 43 |
type: str |
| 44 |
port: |
| 45 |
description: |
| 46 |
- The proxy TCP/IP port number. |
| 47 |
type: int |
| 48 |
extends_documentation_fragment: |
| 49 |
- purestorage.flasharray.purestorage.fa |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Delete existing proxy settings |
| 54 |
purestorage.flasharray.purefa_proxy: |
| 55 |
state: absent |
| 56 |
fa_url: 10.10.10.2 |
| 57 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 58 |
|
| 59 |
- name: Set proxy settings |
| 60 |
purestorage.flasharray.purefa_proxy: |
| 61 |
host: purestorage.com |
| 62 |
port: 8080 |
| 63 |
fa_url: 10.10.10.2 |
| 64 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 65 |
""" |
| 66 |
|
| 67 |
RETURN = r""" |
| 68 |
""" |
| 69 |
|
| 70 |
|
| 71 |
from ansible.module_utils.basic import AnsibleModule |
| 72 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 73 |
get_array, |
| 74 |
purefa_argument_spec, |
| 75 |
) |
| 76 |
|
| 77 |
HAS_PURESTORAGE = True |
| 78 |
try: |
| 79 |
from pypureclient.flasharray import SupportPatch |
| 80 |
except ImportError: |
| 81 |
HAS_PURESTORAGE = False |
| 82 |
|
| 83 |
|
| 84 |
def delete_proxy(module, array): |
| 85 |
"""Delete proxy settings""" |
| 86 |
changed = False |
| 87 |
current_proxy = list(array.get_support().items)[0].proxy |
| 88 |
if current_proxy != "": |
| 89 |
changed = True |
| 90 |
if not module.check_mode: |
| 91 |
res = array.patch_support(support=SupportPatch(proxy="")) |
| 92 |
if res.status_code != 200: |
| 93 |
module.fail_json( |
| 94 |
msg="Delete proxy settings failed. Error: {0}".format( |
| 95 |
res.errors[0].message |
| 96 |
) |
| 97 |
) |
| 98 |
module.exit_json(changed=changed) |
| 99 |
|
| 100 |
|
| 101 |
def create_proxy(module, array): |
| 102 |
"""Set proxy settings""" |
| 103 |
changed = False |
| 104 |
current_proxy = list(array.get_support().items)[0].proxy |
| 105 |
new_proxy = ( |
| 106 |
module.params["protocol"] |
| 107 |
+ "://" |
| 108 |
+ module.params["host"] |
| 109 |
+ ":" |
| 110 |
+ str(module.params["port"]) |
| 111 |
) |
| 112 |
if new_proxy != current_proxy: |
| 113 |
changed = True |
| 114 |
if not module.check_mode: |
| 115 |
res = array.patch_support(support=SupportPatch(proxy=new_proxy)) |
| 116 |
if res.status_code != 200: |
| 117 |
module.fail_json( |
| 118 |
msg="Set phone home proxy failed. Error: {0}".format( |
| 119 |
res.errors[0].message |
| 120 |
) |
| 121 |
) |
| 122 |
|
| 123 |
module.exit_json(changed=changed) |
| 124 |
|
| 125 |
|
| 126 |
def main(): |
| 127 |
argument_spec = purefa_argument_spec() |
| 128 |
argument_spec.update( |
| 129 |
dict( |
| 130 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 131 |
protocol=dict(type="str", default="https", choices=["http", "https"]), |
| 132 |
host=dict(type="str"), |
| 133 |
port=dict(type="int"), |
| 134 |
) |
| 135 |
) |
| 136 |
|
| 137 |
required_together = [["host", "port"]] |
| 138 |
|
| 139 |
module = AnsibleModule( |
| 140 |
argument_spec, required_together=required_together, supports_check_mode=True |
| 141 |
) |
| 142 |
if not HAS_PURESTORAGE: |
| 143 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 144 |
|
| 145 |
state = module.params["state"] |
| 146 |
array = get_array(module) |
| 147 |
|
| 148 |
if state == "absent": |
| 149 |
delete_proxy(module, array) |
| 150 |
elif state == "present": |
| 151 |
create_proxy(module, array) |
| 152 |
else: |
| 153 |
module.exit_json(changed=False) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |