2015-12-02 14:03:34 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-08-19 16:18:48 +00:00
|
|
|
# Copyright: (c) 2015-16, Ritesh Khadgaray <khadgaray () gmail.com>
|
2017-07-29 15:05:38 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-12-02 14:03:34 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2015-12-02 14:03:34 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: vmware_vm_shell
|
2017-08-19 16:18:48 +00:00
|
|
|
short_description: Run commands in a VMware guest operating system
|
2015-12-02 14:03:34 +00:00
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- Module allows user to run common system administration commands in the guest operating system.
|
|
|
|
version_added: "2.1"
|
|
|
|
author:
|
|
|
|
- Ritesh Khadgaray (@ritzk)
|
|
|
|
- Abhijeet Kasurde (@akasurde)
|
2015-12-02 14:03:34 +00:00
|
|
|
notes:
|
|
|
|
- Tested on vSphere 5.5
|
|
|
|
- Only the first match against vm_id is used, even if there are multiple matches
|
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- PyVmomi
|
|
|
|
options:
|
|
|
|
datacenter:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The datacenter hosting the virtual machine.
|
|
|
|
- If set, it will help to speed up virtual machine search.
|
2016-04-23 06:19:17 +00:00
|
|
|
cluster:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The cluster hosting the virtual machine.
|
|
|
|
- If set, it will help to speed up virtual machine search.
|
2017-07-24 16:51:26 +00:00
|
|
|
folder:
|
|
|
|
description:
|
|
|
|
- Destination folder, absolute or relative path to find an existing guest or create the new guest.
|
|
|
|
- The folder should include the datacenter. ESX's datacenter is ha-datacenter
|
|
|
|
- 'Examples:'
|
|
|
|
- ' folder: /ha-datacenter/vm'
|
|
|
|
- ' folder: ha-datacenter/vm'
|
|
|
|
- ' folder: /datacenter1/vm'
|
|
|
|
- ' folder: datacenter1/vm'
|
|
|
|
- ' folder: /datacenter1/vm/folder1'
|
|
|
|
- ' folder: datacenter1/vm/folder1'
|
|
|
|
- ' folder: /folder1/datacenter1/vm'
|
|
|
|
- ' folder: folder1/datacenter1/vm'
|
|
|
|
- ' folder: /folder1/datacenter1/vm/folder2'
|
|
|
|
- ' folder: vm/folder2'
|
|
|
|
- ' folder: folder2'
|
|
|
|
default: /vm
|
|
|
|
version_added: "2.4"
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_id:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- Name of the virtual machine to work with.
|
2015-12-02 14:03:34 +00:00
|
|
|
required: True
|
|
|
|
vm_id_type:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The VMware identification method by which the virtual machine will be identified.
|
2015-12-11 14:57:10 +00:00
|
|
|
default: vm_name
|
2015-12-02 14:03:34 +00:00
|
|
|
choices:
|
|
|
|
- 'uuid'
|
|
|
|
- 'dns_name'
|
|
|
|
- 'inventory_path'
|
2015-12-11 14:57:10 +00:00
|
|
|
- 'vm_name'
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_username:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The user to login-in to the virtual machine.
|
|
|
|
required: True
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_password:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The password used to login-in to the virtual machine.
|
|
|
|
required: True
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_shell:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The absolute path to the program to start.
|
|
|
|
- On Linux, shell is executed via bash.
|
2015-12-02 14:03:34 +00:00
|
|
|
required: True
|
|
|
|
vm_shell_args:
|
|
|
|
description:
|
2016-04-23 06:27:21 +00:00
|
|
|
- The argument to the program.
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_shell_env:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- Comma separated list of environment variable, specified in the guest OS notation.
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_shell_cwd:
|
|
|
|
description:
|
2017-08-19 16:18:48 +00:00
|
|
|
- The current working directory of the application from which it will be run.
|
2016-01-12 17:00:22 +00:00
|
|
|
extends_documentation_fragment: vmware.documentation
|
2015-12-02 14:03:34 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2017-08-19 16:18:48 +00:00
|
|
|
- name: Run command inside a vm
|
|
|
|
vmware_vm_shell:
|
2017-07-24 16:51:26 +00:00
|
|
|
hostname: myVSphere
|
|
|
|
username: myUsername
|
|
|
|
password: mySecret
|
|
|
|
datacenter: myDatacenter
|
|
|
|
folder: /vm
|
|
|
|
vm_id: NameOfVM
|
|
|
|
vm_username: root
|
|
|
|
vm_password: superSecret
|
|
|
|
vm_shell: /bin/echo
|
|
|
|
vm_shell_args: " $var >> myFile "
|
|
|
|
vm_shell_env:
|
|
|
|
- "PATH=/bin"
|
|
|
|
- "VAR=test"
|
|
|
|
vm_shell_cwd: "/tmp"
|
2017-08-19 16:18:48 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
register: shell_command_output
|
2015-12-02 14:03:34 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
from pyVmomi import vim, vmodl
|
|
|
|
except ImportError:
|
2017-08-19 16:18:48 +00:00
|
|
|
pass
|
2017-07-29 15:05:38 +00:00
|
|
|
|
2017-07-24 16:51:26 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-07-29 15:05:38 +00:00
|
|
|
from ansible.module_utils.vmware import (connect_to_api, find_cluster_by_name, find_datacenter_by_name,
|
2017-08-19 16:18:48 +00:00
|
|
|
find_vm_by_id, HAS_PYVMOMI, vmware_argument_spec)
|
2017-07-24 16:51:26 +00:00
|
|
|
|
2015-12-02 14:03:34 +00:00
|
|
|
|
|
|
|
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/execute_program_in_vm.py
|
2017-08-19 16:18:48 +00:00
|
|
|
def execute_command(content, vm, params):
|
|
|
|
vm_username = params['vm_username']
|
|
|
|
vm_password = params['vm_password']
|
|
|
|
program_path = params['vm_shell']
|
|
|
|
args = params['vm_shell_args']
|
|
|
|
env = params['vm_shell_env']
|
|
|
|
cwd = params['vm_shell_cwd']
|
2015-12-02 14:03:34 +00:00
|
|
|
|
|
|
|
creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
|
|
|
|
cmdspec = vim.vm.guest.ProcessManager.ProgramSpec(arguments=args, envVariables=env, programPath=program_path, workingDirectory=cwd)
|
|
|
|
cmdpid = content.guestOperationsManager.processManager.StartProgramInGuest(vm=vm, auth=creds, spec=cmdspec)
|
|
|
|
|
|
|
|
return cmdpid
|
|
|
|
|
|
|
|
|
2017-07-24 16:51:26 +00:00
|
|
|
def main():
|
2015-12-02 14:03:34 +00:00
|
|
|
argument_spec = vmware_argument_spec()
|
2017-08-19 16:18:48 +00:00
|
|
|
argument_spec.update(dict(datacenter=dict(type='str'),
|
|
|
|
cluster=dict(type='str'),
|
2017-07-24 16:51:26 +00:00
|
|
|
folder=dict(type='str', default='/vm'),
|
2017-08-19 16:18:48 +00:00
|
|
|
vm_id=dict(type='str', required=True),
|
2015-12-09 21:30:31 +00:00
|
|
|
vm_id_type=dict(default='vm_name', type='str', choices=['inventory_path', 'uuid', 'dns_name', 'vm_name']),
|
2017-08-19 16:18:48 +00:00
|
|
|
vm_username=dict(type='str', required=True),
|
|
|
|
vm_password=dict(type='str', no_log=True, required=True),
|
|
|
|
vm_shell=dict(type='str', required=True),
|
2015-12-02 14:03:34 +00:00
|
|
|
vm_shell_args=dict(default=" ", type='str'),
|
2017-08-19 16:18:48 +00:00
|
|
|
vm_shell_env=dict(type='list'),
|
|
|
|
vm_shell_cwd=dict(type='str')))
|
2015-12-02 14:03:34 +00:00
|
|
|
|
2017-07-24 16:51:26 +00:00
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=False,
|
|
|
|
required_if=[['vm_id_type', 'inventory_path', ['folder']]],
|
|
|
|
)
|
2015-12-02 14:03:34 +00:00
|
|
|
|
|
|
|
if not HAS_PYVMOMI:
|
2016-04-23 06:19:17 +00:00
|
|
|
module.fail_json(changed=False, msg='pyvmomi is required for this module')
|
|
|
|
|
2017-08-19 16:18:48 +00:00
|
|
|
datacenter_name = module.params['datacenter']
|
|
|
|
cluster_name = module.params['cluster']
|
|
|
|
folder = module.params['folder']
|
|
|
|
content = connect_to_api(module)
|
|
|
|
|
|
|
|
datacenter = None
|
|
|
|
if datacenter_name:
|
|
|
|
datacenter = find_datacenter_by_name(content, datacenter_name)
|
|
|
|
if not datacenter:
|
|
|
|
module.fail_json(changed=False, msg="Unable to find %(datacenter)s datacenter" % module.params)
|
2015-12-02 14:03:34 +00:00
|
|
|
|
2017-08-19 16:18:48 +00:00
|
|
|
cluster = None
|
|
|
|
if cluster_name:
|
|
|
|
cluster = find_cluster_by_name(content, cluster_name, datacenter)
|
|
|
|
if not cluster:
|
|
|
|
module.fail_json(changed=False, msg="Unable to find %(cluster)s cluster" % module.params)
|
|
|
|
|
|
|
|
if module.params['vm_id_type'] == 'inventory_path':
|
|
|
|
vm = find_vm_by_id(content, vm_id=module.params['vm_id'], vm_id_type="inventory_path", folder=folder)
|
|
|
|
else:
|
|
|
|
vm = find_vm_by_id(content, vm_id=module.params['vm_id'], vm_id_type=module.params['vm_id_type'], datacenter=datacenter, cluster=cluster)
|
|
|
|
|
|
|
|
if not vm:
|
|
|
|
module.fail_json(msg='Unable to find virtual machine.')
|
|
|
|
|
|
|
|
try:
|
|
|
|
msg = execute_command(content, vm, module.params)
|
2016-04-23 06:19:17 +00:00
|
|
|
module.exit_json(changed=True, uuid=vm.summary.config.uuid, msg=msg)
|
2015-12-02 14:03:34 +00:00
|
|
|
except vmodl.RuntimeFault as runtime_fault:
|
2016-04-23 06:19:17 +00:00
|
|
|
module.fail_json(changed=False, msg=runtime_fault.msg)
|
2015-12-02 14:03:34 +00:00
|
|
|
except vmodl.MethodFault as method_fault:
|
2016-04-23 06:19:17 +00:00
|
|
|
module.fail_json(changed=False, msg=method_fault.msg)
|
2015-12-02 14:03:34 +00:00
|
|
|
except Exception as e:
|
2016-04-23 06:19:17 +00:00
|
|
|
module.fail_json(changed=False, msg=str(e))
|
2015-12-02 14:03:34 +00:00
|
|
|
|
2017-07-29 15:05:38 +00:00
|
|
|
|
2015-12-02 14:03:34 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|