| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: ibm_svcinfo_command |
| 13 |
short_description: This module implements SSH Client which helps to run |
| 14 |
svcinfo CLI command on IBM Storage Virtualize family systems |
| 15 |
version_added: "1.2.0" |
| 16 |
description: |
| 17 |
- Runs single svcinfo CLI command on IBM Storage Virtualize family systems. |
| 18 |
Filter options like filtervalue or pipe '|' with grep, awk, and others are |
| 19 |
not supported in the command in this module. |
| 20 |
Paramiko must be installed to use this module. |
| 21 |
author: |
| 22 |
- Shilpi Jain (@Shilpi-Jain1) |
| 23 |
options: |
| 24 |
command: |
| 25 |
description: |
| 26 |
- Single svcinfo CLI command to be executed on Storage Virtualize system. |
| 27 |
type: str |
| 28 |
usesshkey: |
| 29 |
description: |
| 30 |
- For key-pair based SSH connection, set this field as C('yes'). |
| 31 |
Provide full path of keyfile in key_filename field. |
| 32 |
If not provided, default path of SSH key is used. |
| 33 |
type: str |
| 34 |
choices: [ 'yes', 'no'] |
| 35 |
default: 'no' |
| 36 |
key_filename: |
| 37 |
description: |
| 38 |
- SSH client private key filename. By default, C(~/.ssh/id_rsa) is used. |
| 39 |
type: str |
| 40 |
clustername: |
| 41 |
description: |
| 42 |
- The hostname or management IP or Partition IP of the |
| 43 |
storage Virtualize system. |
| 44 |
type: str |
| 45 |
required: true |
| 46 |
domain: |
| 47 |
description: |
| 48 |
- Domain for the Storage Virtualize storage system. |
| 49 |
- Valid when hostname is used for the parameter I(clustername). |
| 50 |
type: str |
| 51 |
username: |
| 52 |
description: |
| 53 |
- Username for the Storage Virtualize system. |
| 54 |
required: true |
| 55 |
type: str |
| 56 |
password: |
| 57 |
description: |
| 58 |
- Password for the Storage Virtualize system. |
| 59 |
type: str |
| 60 |
log_path: |
| 61 |
description: |
| 62 |
- Path of debug log file. |
| 63 |
type: str |
| 64 |
notes: |
| 65 |
- This module supports logging in via partition IP. |
| 66 |
''' |
| 67 |
|
| 68 |
EXAMPLES = ''' |
| 69 |
- name: Run svcinfo CLI command using SSH client with password |
| 70 |
ibm.storage_virtualize.ibm_svcinfo_command: |
| 71 |
command: "svcinfo lsuser {{ user }}" |
| 72 |
clustername: "{{ clustername }}" |
| 73 |
username: "{{ username }}" |
| 74 |
password: "{{ password }}" |
| 75 |
log_path: /tmp/ansible.log |
| 76 |
- name: Run svcinfo CLI command using passwordless SSH Client |
| 77 |
ibm.storage_virtualize.ibm_svcinfo_command: |
| 78 |
command: "svcinfo lsuser" |
| 79 |
usesshkey: "yes" |
| 80 |
clustername: "{{ clustername }}" |
| 81 |
username: "{{ username }}" |
| 82 |
log_path: /tmp/ansible.log |
| 83 |
- name: Run sainfo CLI command |
| 84 |
ibm.storage_virtualize.ibm_svcinfo_command: |
| 85 |
command: "sainfo lsservicenodes" |
| 86 |
clustername: "{{ clustername }}" |
| 87 |
username: "{{ username }}" |
| 88 |
password: "{{ password }}" |
| 89 |
log_path: /tmp/ansible.log |
| 90 |
''' |
| 91 |
|
| 92 |
RETURN = '''#''' |
| 93 |
|
| 94 |
from traceback import format_exc |
| 95 |
from ansible.module_utils.basic import AnsibleModule |
| 96 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import svc_ssh_argument_spec, get_logger |
| 97 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_ssh import IBMSVCssh |
| 98 |
from ansible.module_utils._text import to_native |
| 99 |
|
| 100 |
|
| 101 |
class IBMSVCsshClient(object): |
| 102 |
def __init__( |
| 103 |
self, |
| 104 |
timeout=30, |
| 105 |
cmd_timeout=30.0): |
| 106 |
""" |
| 107 |
Constructor for SSH client class. |
| 108 |
""" |
| 109 |
|
| 110 |
argument_spec = svc_ssh_argument_spec() |
| 111 |
|
| 112 |
argument_spec.update( |
| 113 |
dict( |
| 114 |
password=dict(type='str', required=False, no_log=True), |
| 115 |
command=dict(type='str', required=False), |
| 116 |
usesshkey=dict(type='str', required=False, default='no', choices=['yes', 'no']), |
| 117 |
key_filename=dict(type='str', required=False) |
| 118 |
) |
| 119 |
) |
| 120 |
|
| 121 |
self.module = AnsibleModule(argument_spec=argument_spec, |
| 122 |
supports_check_mode=True) |
| 123 |
|
| 124 |
|
| 125 |
log_path = self.module.params['log_path'] |
| 126 |
log = get_logger(self.__class__.__name__, log_path) |
| 127 |
self.log = log.info |
| 128 |
|
| 129 |
|
| 130 |
self.command = self.module.params['command'] |
| 131 |
|
| 132 |
|
| 133 |
self.usesshkey = self.module.params['usesshkey'] |
| 134 |
self.key_filename = self.module.params['key_filename'] |
| 135 |
|
| 136 |
|
| 137 |
self.clustername = self.module.params['clustername'] |
| 138 |
self.username = self.module.params['username'] |
| 139 |
self.log_path = log_path |
| 140 |
|
| 141 |
|
| 142 |
self.domain = self.module.params.get('domain', '') |
| 143 |
self.password = self.module.params.get('password', '') |
| 144 |
|
| 145 |
|
| 146 |
if not self.command: |
| 147 |
self.module.fail_json(msg='Missing mandatory parameter: command') |
| 148 |
|
| 149 |
if not self.password: |
| 150 |
if self.usesshkey == 'yes': |
| 151 |
self.log("password is none and use ssh private key. Check for its path") |
| 152 |
if self.key_filename: |
| 153 |
self.look_for_keys = True |
| 154 |
else: |
| 155 |
self.log("key file_name is not provided, use default one, ~/.ssh/id_rsa.pub") |
| 156 |
self.look_for_keys = True |
| 157 |
else: |
| 158 |
self.module.fail_json(msg="You must pass in either password or key for ssh") |
| 159 |
else: |
| 160 |
self.look_for_keys = False |
| 161 |
|
| 162 |
|
| 163 |
self.ssh_client = IBMSVCssh( |
| 164 |
module=self.module, |
| 165 |
clustername=self.module.params['clustername'], |
| 166 |
domain=self.domain, |
| 167 |
username=self.module.params['username'], |
| 168 |
password=self.password, |
| 169 |
look_for_keys=self.look_for_keys, |
| 170 |
key_filename=self.key_filename, |
| 171 |
log_path=log_path |
| 172 |
) |
| 173 |
|
| 174 |
def modify_command(self, argument): |
| 175 |
index = None |
| 176 |
command = [item.strip() for item in argument.split()] |
| 177 |
if command: |
| 178 |
for n, word in enumerate(command): |
| 179 |
if word.startswith('ls') and 'svcinfo' in command[n - 1]: |
| 180 |
index = n |
| 181 |
break |
| 182 |
if index: |
| 183 |
command.insert(index + 1, '-json') |
| 184 |
return ' '.join(command) |
| 185 |
|
| 186 |
def send_svcinfo_command(self): |
| 187 |
info_output = "" |
| 188 |
message = "" |
| 189 |
failed = False |
| 190 |
|
| 191 |
if self.ssh_client.is_client_connected: |
| 192 |
if (self.command.find('|') != -1): |
| 193 |
failed = True |
| 194 |
message = "Pipe(|) is not supported in command." |
| 195 |
if (self.command.find('-filtervalue') != -1): |
| 196 |
failed = True |
| 197 |
message = "'filtervalue' is not supported in command." |
| 198 |
if not failed: |
| 199 |
new_command = self.modify_command(self.command) |
| 200 |
self.log("Executing CLI command: %s", new_command) |
| 201 |
stdin, stdout, stderr = self.ssh_client.client.exec_command(new_command) |
| 202 |
for line in stdout.readlines(): |
| 203 |
info_output += line |
| 204 |
self.log(info_output) |
| 205 |
rc = stdout.channel.recv_exit_status() |
| 206 |
if rc > 0: |
| 207 |
message = stderr.read() |
| 208 |
if len(message) > 0: |
| 209 |
message = message.decode('utf-8') |
| 210 |
self.log("Error in executing CLI command: %s", new_command) |
| 211 |
self.log("%s", message) |
| 212 |
else: |
| 213 |
message = "Unknown error" |
| 214 |
self.ssh_client._svc_disconnect() |
| 215 |
self.module.fail_json(msg=message, rc=rc, stdout=info_output) |
| 216 |
self.ssh_client._svc_disconnect() |
| 217 |
self.module.exit_json(msg=message, rc=rc, stdout=info_output, changed=False) |
| 218 |
else: |
| 219 |
message = "SSH client is not connected" |
| 220 |
self.ssh_client._svc_disconnect() |
| 221 |
self.module.fail_json(msg=message) |
| 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_svcinfo_command() |
| 232 |
except Exception as e: |
| 233 |
v.ssh_client._svc_disconnect() |
| 234 |
v.log("Exception in executing CLI 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() |