2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: proxmox_template
|
2022-11-09 13:57:41 +01:00
|
|
|
short_description: Management of OS templates in Proxmox VE cluster
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- allows you to upload/delete templates in Proxmox VE cluster
|
Add attributes to atomic, memset, one, oneview, packet, proxmox, and xenserver modules (#5958)
Add attributes to atomic, memset, one, oneview, packet, proxmox, and xenserver modules.
2023-02-20 17:27:38 +01:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2024-05-12 10:03:06 +02:00
|
|
|
action_group:
|
|
|
|
version_added: 9.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
node:
|
|
|
|
description:
|
2020-12-02 16:08:07 +01:00
|
|
|
- Proxmox VE node on which to operate.
|
2020-09-01 13:44:04 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
src:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- Path to uploaded file.
|
2023-06-15 15:48:17 +02:00
|
|
|
- Required only for O(state=present).
|
2020-09-01 13:44:04 +02:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
template:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- The template name.
|
2023-06-15 15:48:17 +02:00
|
|
|
- Required for O(state=absent) to delete a template.
|
|
|
|
- Required for O(state=present) to download an appliance container template (pveam).
|
2020-09-01 13:44:04 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
content_type:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- Content type.
|
2023-06-15 15:48:17 +02:00
|
|
|
- Required only for O(state=present).
|
2020-09-01 13:44:04 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 'vztmpl'
|
|
|
|
choices: ['vztmpl', 'iso']
|
|
|
|
storage:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- Target storage.
|
2020-09-01 13:44:04 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 'local'
|
|
|
|
timeout:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- Timeout for operations.
|
2020-09-01 13:44:04 +02:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 30
|
|
|
|
force:
|
|
|
|
description:
|
2023-06-15 15:48:17 +02:00
|
|
|
- It can only be used with O(state=present), existing template will be overwritten.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 20:16:25 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- Indicate desired state of the template.
|
2020-09-01 13:44:04 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: present
|
|
|
|
notes:
|
2023-07-02 21:48:26 +02:00
|
|
|
- Requires C(proxmoxer) and C(requests) modules on host. Those modules can be installed with M(ansible.builtin.pip).
|
|
|
|
- C(proxmoxer) >= 1.2.0 requires C(requests_toolbelt) to upload files larger than 256 MB.
|
2020-03-09 10:11:07 +01:00
|
|
|
author: Sergei Antipov (@UnderGreen)
|
Add attributes to atomic, memset, one, oneview, packet, proxmox, and xenserver modules (#5958)
Add attributes to atomic, memset, one, oneview, packet, proxmox, and xenserver modules.
2023-02-20 17:27:38 +01:00
|
|
|
extends_documentation_fragment:
|
2024-05-12 10:03:06 +02:00
|
|
|
- community.general.proxmox.actiongroup_proxmox
|
Add attributes to atomic, memset, one, oneview, packet, proxmox, and xenserver modules (#5958)
Add attributes to atomic, memset, one, oneview, packet, proxmox, and xenserver modules.
2023-02-20 17:27:38 +01:00
|
|
|
- community.general.proxmox.documentation
|
|
|
|
- community.general.attributes
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Upload new openvz template with minimal options
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.proxmox_template:
|
2020-03-09 10:11:07 +01:00
|
|
|
node: uk-mc02
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
src: ~/ubuntu-14.04-x86_64.tar.gz
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: >
|
|
|
|
Upload new openvz template with minimal options use environment
|
|
|
|
PROXMOX_PASSWORD variable(you should export it before)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.proxmox_template:
|
2020-03-09 10:11:07 +01:00
|
|
|
node: uk-mc02
|
|
|
|
api_user: root@pam
|
|
|
|
api_host: node1
|
|
|
|
src: ~/ubuntu-14.04-x86_64.tar.gz
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Upload new openvz template with all options and force overwrite
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.proxmox_template:
|
2020-03-09 10:11:07 +01:00
|
|
|
node: uk-mc02
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
storage: local
|
|
|
|
content_type: vztmpl
|
|
|
|
src: ~/ubuntu-14.04-x86_64.tar.gz
|
2022-08-24 20:16:25 +02:00
|
|
|
force: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Delete template with minimal options
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.proxmox_template:
|
2020-03-09 10:11:07 +01:00
|
|
|
node: uk-mc02
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
template: ubuntu-14.04-x86_64.tar.gz
|
|
|
|
state: absent
|
2020-11-04 23:16:07 +01:00
|
|
|
|
|
|
|
- name: Download proxmox appliance container template
|
|
|
|
community.general.proxmox_template:
|
|
|
|
node: uk-mc02
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
storage: local
|
|
|
|
content_type: vztmpl
|
|
|
|
template: ubuntu-20.04-standard_20.04-1_amd64.tar.gz
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
2023-07-02 21:48:26 +02:00
|
|
|
import traceback
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-07-02 21:48:26 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
2022-02-07 06:21:24 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.proxmox import (proxmox_auth_argument_spec, ProxmoxAnsible)
|
2023-07-02 21:48:26 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
|
|
|
|
|
|
|
REQUESTS_TOOLBELT_ERR = None
|
|
|
|
try:
|
|
|
|
# requests_toolbelt is used internally by proxmoxer module
|
|
|
|
import requests_toolbelt # noqa: F401, pylint: disable=unused-import
|
|
|
|
HAS_REQUESTS_TOOLBELT = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_REQUESTS_TOOLBELT = False
|
|
|
|
REQUESTS_TOOLBELT_ERR = traceback.format_exc()
|
2022-02-07 06:21:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ProxmoxTemplateAnsible(ProxmoxAnsible):
|
|
|
|
def get_template(self, node, storage, content_type, template):
|
2023-07-02 21:48:26 +02:00
|
|
|
try:
|
|
|
|
return [True for tmpl in self.proxmox_api.nodes(node).storage(storage).content.get()
|
|
|
|
if tmpl['volid'] == '%s:%s/%s' % (storage, content_type, template)]
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg="Failed to retrieve template '%s:%s/%s': %s" % (storage, content_type, template, e))
|
2022-02-07 06:21:24 +01:00
|
|
|
|
|
|
|
def task_status(self, node, taskid, timeout):
|
|
|
|
"""
|
|
|
|
Check the task status and wait until the task is completed or the timeout is reached.
|
|
|
|
"""
|
|
|
|
while timeout:
|
2022-09-28 22:48:11 +02:00
|
|
|
if self.api_task_ok(node, taskid):
|
2022-02-07 06:21:24 +01:00
|
|
|
return True
|
|
|
|
timeout = timeout - 1
|
|
|
|
if timeout == 0:
|
|
|
|
self.module.fail_json(msg='Reached timeout while waiting for uploading/downloading template. Last line in task before timeout: %s' %
|
|
|
|
self.proxmox_api.node(node).tasks(taskid).log.get()[:1])
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def upload_template(self, node, storage, content_type, realpath, timeout):
|
2023-07-02 21:48:26 +02:00
|
|
|
stats = os.stat(realpath)
|
|
|
|
if (LooseVersion(self.proxmoxer_version) >= LooseVersion('1.2.0') and
|
|
|
|
stats.st_size > 268435456 and not HAS_REQUESTS_TOOLBELT):
|
|
|
|
self.module.fail_json(msg="'requests_toolbelt' module is required to upload files larger than 256MB",
|
|
|
|
exception=missing_required_lib('requests_toolbelt'))
|
|
|
|
|
|
|
|
try:
|
|
|
|
taskid = self.proxmox_api.nodes(node).storage(storage).upload.post(content=content_type, filename=open(realpath, 'rb'))
|
|
|
|
return self.task_status(node, taskid, timeout)
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg="Uploading template %s failed with error: %s" % (realpath, e))
|
2022-02-07 06:21:24 +01:00
|
|
|
|
|
|
|
def download_template(self, node, storage, template, timeout):
|
2023-07-02 21:48:26 +02:00
|
|
|
try:
|
|
|
|
taskid = self.proxmox_api.nodes(node).aplinfo.post(storage=storage, template=template)
|
|
|
|
return self.task_status(node, taskid, timeout)
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg="Downloading template %s failed with error: %s" % (template, e))
|
2022-02-07 06:21:24 +01:00
|
|
|
|
|
|
|
def delete_template(self, node, storage, content_type, template, timeout):
|
|
|
|
volid = '%s:%s/%s' % (storage, content_type, template)
|
|
|
|
self.proxmox_api.nodes(node).storage(storage).content.delete(volid)
|
|
|
|
while timeout:
|
|
|
|
if not self.get_template(node, storage, content_type, template):
|
|
|
|
return True
|
|
|
|
timeout = timeout - 1
|
|
|
|
if timeout == 0:
|
|
|
|
self.module.fail_json(msg='Reached timeout while waiting for deleting template.')
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
return False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-02-07 06:21:24 +01:00
|
|
|
module_args = proxmox_auth_argument_spec()
|
|
|
|
template_args = dict(
|
|
|
|
node=dict(),
|
|
|
|
src=dict(type='path'),
|
|
|
|
template=dict(),
|
|
|
|
content_type=dict(default='vztmpl', choices=['vztmpl', 'iso']),
|
|
|
|
storage=dict(default='local'),
|
|
|
|
timeout=dict(type='int', default=30),
|
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
)
|
|
|
|
module_args.update(template_args)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
module = AnsibleModule(
|
2022-02-07 06:21:24 +01:00
|
|
|
argument_spec=module_args,
|
2021-02-12 13:01:52 +01:00
|
|
|
required_together=[('api_token_id', 'api_token_secret')],
|
|
|
|
required_one_of=[('api_password', 'api_token_id')],
|
|
|
|
required_if=[('state', 'absent', ['template'])]
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
proxmox = ProxmoxTemplateAnsible(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
node = module.params['node']
|
|
|
|
storage = module.params['storage']
|
|
|
|
timeout = module.params['timeout']
|
|
|
|
|
|
|
|
if state == 'present':
|
2023-07-02 21:48:26 +02:00
|
|
|
content_type = module.params['content_type']
|
|
|
|
src = module.params['src']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-07-02 21:48:26 +02:00
|
|
|
# download appliance template
|
|
|
|
if content_type == 'vztmpl' and not src:
|
|
|
|
template = module.params['template']
|
2020-11-04 23:16:07 +01:00
|
|
|
|
2023-07-02 21:48:26 +02:00
|
|
|
if not template:
|
|
|
|
module.fail_json(msg='template param for downloading appliance template is mandatory')
|
2020-11-04 23:16:07 +01:00
|
|
|
|
2023-07-02 21:48:26 +02:00
|
|
|
if proxmox.get_template(node, storage, content_type, template) and not module.params['force']:
|
|
|
|
module.exit_json(changed=False, msg='template with volid=%s:%s/%s already exists' % (storage, content_type, template))
|
2020-11-04 23:16:07 +01:00
|
|
|
|
2023-07-02 21:48:26 +02:00
|
|
|
if proxmox.download_template(node, storage, template, timeout):
|
|
|
|
module.exit_json(changed=True, msg='template with volid=%s:%s/%s downloaded' % (storage, content_type, template))
|
2020-11-04 23:16:07 +01:00
|
|
|
|
2023-07-02 21:48:26 +02:00
|
|
|
template = os.path.basename(src)
|
|
|
|
if proxmox.get_template(node, storage, content_type, template) and not module.params['force']:
|
|
|
|
module.exit_json(changed=False, msg='template with volid=%s:%s/%s is already exists' % (storage, content_type, template))
|
|
|
|
elif not src:
|
|
|
|
module.fail_json(msg='src param to uploading template file is mandatory')
|
|
|
|
elif not (os.path.exists(src) and os.path.isfile(src)):
|
|
|
|
module.fail_json(msg='template file on path %s not exists' % src)
|
|
|
|
|
|
|
|
if proxmox.upload_template(node, storage, content_type, src, timeout):
|
|
|
|
module.exit_json(changed=True, msg='template with volid=%s:%s/%s uploaded' % (storage, content_type, template))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
try:
|
|
|
|
content_type = module.params['content_type']
|
|
|
|
template = module.params['template']
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if not proxmox.get_template(node, storage, content_type, template):
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=False, msg='template with volid=%s:%s/%s is already deleted' % (storage, content_type, template))
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if proxmox.delete_template(node, storage, content_type, template, timeout):
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True, msg='template with volid=%s:%s/%s deleted' % (storage, content_type, template))
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="deleting of template %s failed with exception: %s" % (template, e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|