| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (C) 2018 IBM CORPORATION |
| 4 |
# Author(s): Tzur Eliyahu <[email protected]> |
| 5 |
# |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: ibm_sa_host_ports |
| 13 |
short_description: Add host ports on IBM Spectrum Accelerate Family storage systems |
| 14 |
|
| 15 |
description: |
| 16 |
- This module adds ports to or removes them from the hosts on IBM Spectrum Accelerate Family storage systems. |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: none |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
|
| 23 |
options: |
| 24 |
host: |
| 25 |
description: |
| 26 |
- Host name. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Host ports state. |
| 32 |
default: "present" |
| 33 |
choices: ["present", "absent"] |
| 34 |
type: str |
| 35 |
iscsi_name: |
| 36 |
description: |
| 37 |
- The iSCSI initiator name. |
| 38 |
type: str |
| 39 |
fcaddress: |
| 40 |
description: |
| 41 |
- Fiber channel address. |
| 42 |
type: str |
| 43 |
num_of_visible_targets: |
| 44 |
description: |
| 45 |
- Number of visible targets. |
| 46 |
type: str |
| 47 |
|
| 48 |
extends_documentation_fragment: |
| 49 |
- community.general._ibm_storage |
| 50 |
- community.general._attributes |
| 51 |
|
| 52 |
author: |
| 53 |
- Tzur Eliyahu (@tzure) |
| 54 |
""" |
| 55 |
|
| 56 |
EXAMPLES = r""" |
| 57 |
- name: Add ports for host. |
| 58 |
community.general.ibm_sa_host_ports: |
| 59 |
host: test_host |
| 60 |
iscsi_name: iqn.1994-05.com*** |
| 61 |
username: admin |
| 62 |
password: secret |
| 63 |
endpoints: hostdev-system |
| 64 |
state: present |
| 65 |
|
| 66 |
- name: Remove ports for host. |
| 67 |
community.general.ibm_sa_host_ports: |
| 68 |
host: test_host |
| 69 |
iscsi_name: iqn.1994-05.com*** |
| 70 |
username: admin |
| 71 |
password: secret |
| 72 |
endpoints: hostdev-system |
| 73 |
state: absent |
| 74 |
""" |
| 75 |
RETURN = r""" |
| 76 |
""" |
| 77 |
|
| 78 |
from ansible.module_utils.basic import AnsibleModule |
| 79 |
|
| 80 |
from ansible_collections.community.general.plugins.module_utils._ibm_sa_utils import ( |
| 81 |
connect_ssl, |
| 82 |
execute_pyxcli_command, |
| 83 |
is_pyxcli_installed, |
| 84 |
spectrum_accelerate_spec, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
def main(): |
| 89 |
argument_spec = spectrum_accelerate_spec() |
| 90 |
argument_spec.update( |
| 91 |
dict( |
| 92 |
state=dict(default="present", choices=["present", "absent"]), |
| 93 |
host=dict(required=True), |
| 94 |
iscsi_name=dict(), |
| 95 |
fcaddress=dict(), |
| 96 |
num_of_visible_targets=dict(), |
| 97 |
) |
| 98 |
) |
| 99 |
|
| 100 |
module = AnsibleModule(argument_spec) |
| 101 |
is_pyxcli_installed(module) |
| 102 |
|
| 103 |
xcli_client = connect_ssl(module) |
| 104 |
# required args |
| 105 |
ports = [] |
| 106 |
try: |
| 107 |
ports = xcli_client.cmd.host_list_ports(host=module.params.get("host")).as_list |
| 108 |
except Exception: |
| 109 |
pass |
| 110 |
state = module.params["state"] |
| 111 |
port_exists = False |
| 112 |
ports = [port.get("port_name") for port in ports] |
| 113 |
|
| 114 |
fc_ports = module.params.get("fcaddress") if module.params.get("fcaddress") else [] |
| 115 |
iscsi_ports = module.params.get("iscsi_name") if module.params.get("iscsi_name") else [] |
| 116 |
for port in ports: |
| 117 |
if port in iscsi_ports or port in fc_ports: |
| 118 |
port_exists = True |
| 119 |
break |
| 120 |
state_changed = False |
| 121 |
if state == "present" and not port_exists: |
| 122 |
state_changed = execute_pyxcli_command(module, "host_add_port", xcli_client) |
| 123 |
if state == "absent" and port_exists: |
| 124 |
state_changed = execute_pyxcli_command(module, "host_remove_port", xcli_client) |
| 125 |
|
| 126 |
module.exit_json(changed=state_changed) |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == "__main__": |
| 130 |
main() |