| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: ibm_svctask_command |
| 15 |
short_description: This module implements SSH Client which helps to run |
| 16 |
svctask CLI command(s) on IBM Storage Virtualize family systems |
| 17 |
version_added: "1.2.0" |
| 18 |
description: |
| 19 |
- Runs svctask and satask CLI command(s) on IBM Storage Virtualize Family systems. |
| 20 |
In case any command fails while running this module, then the |
| 21 |
module stops processing further commands in the list. |
| 22 |
Paramiko must be installed to use this module. |
| 23 |
author: |
| 24 |
- Shilpi Jain (@Shilpi-Jain1) |
| 25 |
- Sumit Kumar Gupta (@sumitguptaibm) |
| 26 |
- Rahul Pawar (@rahulpawaribm) |
| 27 |
|
| 28 |
options: |
| 29 |
command: |
| 30 |
description: |
| 31 |
- A list containing svctask CLI commands to be executed on storage. |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
usesshkey: |
| 35 |
description: |
| 36 |
- For key-pair based SSH connection, set this field as "yes". |
| 37 |
Provide full path of key in key_filename field. |
| 38 |
If not provided, default path of SSH key is used. |
| 39 |
type: str |
| 40 |
choices: [ 'yes', 'no'] |
| 41 |
default: 'no' |
| 42 |
key_filename: |
| 43 |
description: |
| 44 |
- SSH client private key filename. By default, ~/.ssh/id_rsa is used. |
| 45 |
type: str |
| 46 |
clustername: |
| 47 |
description: |
| 48 |
- The hostname or management IP or Partition IP of the Storage Virtualize system. |
| 49 |
type: str |
| 50 |
required: true |
| 51 |
domain: |
| 52 |
description: |
| 53 |
- Domain for the Storage Virtualize storage system. |
| 54 |
- Valid when hostname is used for the parameter I(clustername). |
| 55 |
type: str |
| 56 |
username: |
| 57 |
description: |
| 58 |
- Username for the Storage Virtualize system. |
| 59 |
required: true |
| 60 |
type: str |
| 61 |
password: |
| 62 |
description: |
| 63 |
- Password for the Storage Virtualize system. |
| 64 |
type: str |
| 65 |
log_path: |
| 66 |
description: |
| 67 |
- Path of debug log file. |
| 68 |
type: str |
| 69 |
notes: |
| 70 |
- This module supports logging in via partition IP. |
| 71 |
''' |
| 72 |
|
| 73 |
EXAMPLES = ''' |
| 74 |
- name: Run svctask CLI commands using SSH client with password |
| 75 |
ibm.storage_virtualize.ibm_svctask_command: |
| 76 |
command: [ |
| 77 |
"svctask mkvdisk -name {{ volname }} -mdiskgrp '{{ pool }}' -easytier '{{ easy_tier }}' -size {{ size }} -unit {{ unit }}", |
| 78 |
"svctask rmvdisk {{ volname }}" |
| 79 |
] |
| 80 |
clustername: "{{ clustername }}" |
| 81 |
username: "{{ username }}" |
| 82 |
password: "{{ password }}" |
| 83 |
log_path: /tmp/ansible.log |
| 84 |
- name: Run svctask CLI command using passwordless SSH Client |
| 85 |
ibm.storage_virtualize.ibm_svctask_command: |
| 86 |
command: [ |
| 87 |
"svctask mkvdisk -name vol0 -mdiskgrp pool0 -easytier off -size 1 -unit gb", |
| 88 |
"svctask rmvdisk vol0" |
| 89 |
] |
| 90 |
clustername: "{{ clustername }}" |
| 91 |
username: "{{ username }}" |
| 92 |
usesshkey: 'yes' |
| 93 |
log_path: /tmp/ansible.log |
| 94 |
- name: Run satask CLI command |
| 95 |
ibm.storage_virtualize.ibm_svctask_command: |
| 96 |
command: "satask snap" |
| 97 |
clustername: "{{ clustername }}" |
| 98 |
username: "{{ username }}" |
| 99 |
password: "{{ password }}" |
| 100 |
log_path: /tmp/ansible.log |
| 101 |
- name: Generate and export system-signed root CA certificate |
| 102 |
ibm.storage_virtualize.ibm_svctask_command: |
| 103 |
command: [ |
| 104 |
"svctask chsystemcert -mksystemsigned", |
| 105 |
"svctask chsystemcert -exportrootcacert" |
| 106 |
] |
| 107 |
clustername: "{{ clustername }}" |
| 108 |
username: "{{ username }}" |
| 109 |
password: "{{ password }}" |
| 110 |
log_path: /tmp/ansible.log |
| 111 |
''' |
| 112 |
|
| 113 |
RETURN = '''#''' |
| 114 |
|
| 115 |
from traceback import format_exc |
| 116 |
from ansible.module_utils.basic import AnsibleModule |
| 117 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import svc_ssh_argument_spec, get_logger |
| 118 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_ssh import IBMSVCssh |
| 119 |
from ansible.module_utils._text import to_native |
| 120 |
|
| 121 |
|
| 122 |
class IBMSVCsshClient(object): |
| 123 |
def __init__( |
| 124 |
self, |
| 125 |
timeout=30, |
| 126 |
cmd_timeout=30.0): |
| 127 |
""" |
| 128 |
Constructor for SSH client class. |
| 129 |
""" |
| 130 |
|
| 131 |
argument_spec = svc_ssh_argument_spec() |
| 132 |
|
| 133 |
argument_spec.update( |
| 134 |
dict( |
| 135 |
password=dict(type='str', required=False, no_log=True), |
| 136 |
command=dict(type='list', elements='str', required=False), |
| 137 |
usesshkey=dict(type='str', required=False, default='no', choices=['yes', 'no']), |
| 138 |
key_filename=dict(type='str', required=False) |
| 139 |
) |
| 140 |
) |
| 141 |
|
| 142 |
self.module = AnsibleModule(argument_spec=argument_spec, |
| 143 |
supports_check_mode=True) |
| 144 |
|
| 145 |
|
| 146 |
log_path = self.module.params['log_path'] |
| 147 |
log = get_logger(self.__class__.__name__, log_path) |
| 148 |
self.log = log.info |
| 149 |
|
| 150 |
|
| 151 |
self.command = self.module.params['command'] |
| 152 |
|
| 153 |
|
| 154 |
self.usesshkey = self.module.params['usesshkey'] |
| 155 |
self.key_filename = self.module.params['key_filename'] |
| 156 |
|
| 157 |
|
| 158 |
self.clustername = self.module.params['clustername'] |
| 159 |
self.username = self.module.params['username'] |
| 160 |
self.log_path = log_path |
| 161 |
|
| 162 |
|
| 163 |
self.domain = self.module.params.get('domain', '') |
| 164 |
self.password = self.module.params.get('password', '') |
| 165 |
|
| 166 |
|
| 167 |
if not self.command: |
| 168 |
self.module.fail_json(msg='Missing mandatory parameter: command') |
| 169 |
|
| 170 |
if not self.password: |
| 171 |
if self.usesshkey == 'yes': |
| 172 |
self.log("password is none and use ssh private key. Check for its path") |
| 173 |
if self.key_filename: |
| 174 |
self.log("key file_name is provided, use it") |
| 175 |
self.look_for_keys = True |
| 176 |
else: |
| 177 |
self.log("key file_name is not provided, use default one, ~/.ssh/id_rsa.pub") |
| 178 |
self.look_for_keys = True |
| 179 |
else: |
| 180 |
self.log("password is none and SSH key is not provided") |
| 181 |
self.module.fail_json(msg="You must pass in either password or key for ssh") |
| 182 |
else: |
| 183 |
self.log("password is given") |
| 184 |
self.look_for_keys = False |
| 185 |
|
| 186 |
self.ssh_client = IBMSVCssh( |
| 187 |
module=self.module, |
| 188 |
clustername=self.module.params['clustername'], |
| 189 |
domain=self.domain, |
| 190 |
username=self.module.params['username'], |
| 191 |
password=self.module.params['password'], |
| 192 |
look_for_keys=self.look_for_keys, |
| 193 |
key_filename=self.key_filename, |
| 194 |
log_path=log_path |
| 195 |
) |
| 196 |
|
| 197 |
def send_svctask_command(self): |
| 198 |
message = "" |
| 199 |
if self.ssh_client.is_client_connected: |
| 200 |
for cmd in self.command: |
| 201 |
self.log("Executing CLI command: %s", cmd) |
| 202 |
stdin, stdout, stderr = self.ssh_client.client.exec_command(cmd) |
| 203 |
for line in stdout.readlines(): |
| 204 |
message += line |
| 205 |
self.log(line) |
| 206 |
rc = stdout.channel.recv_exit_status() |
| 207 |
if rc > 0: |
| 208 |
result = stderr.read() |
| 209 |
if len(result) > 0: |
| 210 |
result = result.decode('utf-8') |
| 211 |
self.log("Error in executing CLI command: %s", cmd) |
| 212 |
self.log("%s", result) |
| 213 |
message += result |
| 214 |
else: |
| 215 |
message = "Unknown error" |
| 216 |
self.ssh_client._svc_disconnect() |
| 217 |
self.module.fail_json(msg=message, rc=rc) |
| 218 |
else: |
| 219 |
message = "SSH client is not connected" |
| 220 |
self.ssh_client._svc_disconnect() |
| 221 |
self.module.exit_json(msg=message, rc=rc, changed=True) |
| 222 |
|
| 223 |
|
| 224 |
def main(): |
| 225 |
v = IBMSVCsshClient() |
| 226 |
try: |
| 227 |
if not v.ssh_client.is_client_connected: |
| 228 |
v.log("SSH Connection failed, retry") |
| 229 |
v.module.exit_json(msg="SSH Connection failed, retry", changed=False) |
| 230 |
else: |
| 231 |
v.send_svctask_command() |
| 232 |
except Exception as e: |
| 233 |
v.ssh_client._svc_disconnect() |
| 234 |
v.log("Exception in running command(): \n%s", format_exc()) |
| 235 |
v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e)) |
| 236 |
|
| 237 |
|
| 238 |
if __name__ == '__main__': |
| 239 |
main() |