Add server-side artifact fetching to proxmox_template module (#9113)
* Add server-side artifact fetching to proxmox_template module * Update docs, format per feedback. * Formatting plugins/modules/proxmox_template.py Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --------- Co-authored-by: spencer <Spencer> Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>pull/9170/head
parent
4b0d5cb8cf
commit
8078a08f72
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- proxmox_template - add server side artifact fetching support (https://github.com/ansible-collections/community.general/pull/9113).
|
|
@ -30,8 +30,14 @@ options:
|
||||||
src:
|
src:
|
||||||
description:
|
description:
|
||||||
- Path to uploaded file.
|
- Path to uploaded file.
|
||||||
- Required only for O(state=present).
|
- Exactly one of O(src) or O(url) is required for O(state=present).
|
||||||
type: path
|
type: path
|
||||||
|
url:
|
||||||
|
description:
|
||||||
|
- URL to file to download
|
||||||
|
- Exactly one of O(src) or O(url) is required for O(state=present).
|
||||||
|
type: str
|
||||||
|
version_added: 10.1.0
|
||||||
template:
|
template:
|
||||||
description:
|
description:
|
||||||
- The template name.
|
- The template name.
|
||||||
|
@ -85,6 +91,14 @@ EXAMPLES = '''
|
||||||
api_host: node1
|
api_host: node1
|
||||||
src: ~/ubuntu-14.04-x86_64.tar.gz
|
src: ~/ubuntu-14.04-x86_64.tar.gz
|
||||||
|
|
||||||
|
- name: Pull new openvz template with minimal options
|
||||||
|
community.general.proxmox_template:
|
||||||
|
node: uk-mc02
|
||||||
|
api_user: root@pam
|
||||||
|
api_password: 1q2w3e
|
||||||
|
api_host: node1
|
||||||
|
url: https://ubuntu-mirror/ubuntu-14.04-x86_64.tar.gz
|
||||||
|
|
||||||
- name: >
|
- name: >
|
||||||
Upload new openvz template with minimal options use environment
|
Upload new openvz template with minimal options use environment
|
||||||
PROXMOX_PASSWORD variable(you should export it before)
|
PROXMOX_PASSWORD variable(you should export it before)
|
||||||
|
@ -105,6 +119,17 @@ EXAMPLES = '''
|
||||||
src: ~/ubuntu-14.04-x86_64.tar.gz
|
src: ~/ubuntu-14.04-x86_64.tar.gz
|
||||||
force: true
|
force: true
|
||||||
|
|
||||||
|
- name: Pull new openvz template with all options and force overwrite
|
||||||
|
community.general.proxmox_template:
|
||||||
|
node: uk-mc02
|
||||||
|
api_user: root@pam
|
||||||
|
api_password: 1q2w3e
|
||||||
|
api_host: node1
|
||||||
|
storage: local
|
||||||
|
content_type: vztmpl
|
||||||
|
url: https://ubuntu-mirror/ubuntu-14.04-x86_64.tar.gz
|
||||||
|
force: true
|
||||||
|
|
||||||
- name: Delete template with minimal options
|
- name: Delete template with minimal options
|
||||||
community.general.proxmox_template:
|
community.general.proxmox_template:
|
||||||
node: uk-mc02
|
node: uk-mc02
|
||||||
|
@ -132,6 +157,7 @@ import traceback
|
||||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||||
from ansible_collections.community.general.plugins.module_utils.proxmox import (proxmox_auth_argument_spec, ProxmoxAnsible)
|
from ansible_collections.community.general.plugins.module_utils.proxmox import (proxmox_auth_argument_spec, ProxmoxAnsible)
|
||||||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
||||||
|
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||||
|
|
||||||
REQUESTS_TOOLBELT_ERR = None
|
REQUESTS_TOOLBELT_ERR = None
|
||||||
try:
|
try:
|
||||||
|
@ -179,6 +205,17 @@ class ProxmoxTemplateAnsible(ProxmoxAnsible):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.module.fail_json(msg="Uploading template %s failed with error: %s" % (realpath, e))
|
self.module.fail_json(msg="Uploading template %s failed with error: %s" % (realpath, e))
|
||||||
|
|
||||||
|
def fetch_template(self, node, storage, content_type, url, timeout):
|
||||||
|
"""Fetch a template from a web url source using the proxmox download-url endpoint
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
taskid = self.proxmox_api.nodes(node).storage(storage)("download-url").post(
|
||||||
|
url=url, content=content_type, filename=os.path.basename(url)
|
||||||
|
)
|
||||||
|
return self.task_status(node, taskid, timeout)
|
||||||
|
except Exception as e:
|
||||||
|
self.module.fail_json(msg="Fetching template from url %s failed with error: %s" % (url, e))
|
||||||
|
|
||||||
def download_template(self, node, storage, template, timeout):
|
def download_template(self, node, storage, template, timeout):
|
||||||
try:
|
try:
|
||||||
taskid = self.proxmox_api.nodes(node).aplinfo.post(storage=storage, template=template)
|
taskid = self.proxmox_api.nodes(node).aplinfo.post(storage=storage, template=template)
|
||||||
|
@ -205,6 +242,7 @@ def main():
|
||||||
template_args = dict(
|
template_args = dict(
|
||||||
node=dict(),
|
node=dict(),
|
||||||
src=dict(type='path'),
|
src=dict(type='path'),
|
||||||
|
url=dict(),
|
||||||
template=dict(),
|
template=dict(),
|
||||||
content_type=dict(default='vztmpl', choices=['vztmpl', 'iso']),
|
content_type=dict(default='vztmpl', choices=['vztmpl', 'iso']),
|
||||||
storage=dict(default='local'),
|
storage=dict(default='local'),
|
||||||
|
@ -218,7 +256,8 @@ def main():
|
||||||
argument_spec=module_args,
|
argument_spec=module_args,
|
||||||
required_together=[('api_token_id', 'api_token_secret')],
|
required_together=[('api_token_id', 'api_token_secret')],
|
||||||
required_one_of=[('api_password', 'api_token_id')],
|
required_one_of=[('api_password', 'api_token_id')],
|
||||||
required_if=[('state', 'absent', ['template'])]
|
required_if=[('state', 'absent', ['template'])],
|
||||||
|
mutually_exclusive=[("src", "url")],
|
||||||
)
|
)
|
||||||
|
|
||||||
proxmox = ProxmoxTemplateAnsible(module)
|
proxmox = ProxmoxTemplateAnsible(module)
|
||||||
|
@ -231,9 +270,10 @@ def main():
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
content_type = module.params['content_type']
|
content_type = module.params['content_type']
|
||||||
src = module.params['src']
|
src = module.params['src']
|
||||||
|
url = module.params['url']
|
||||||
|
|
||||||
# download appliance template
|
# download appliance template
|
||||||
if content_type == 'vztmpl' and not src:
|
if content_type == 'vztmpl' and not (src or url) :
|
||||||
template = module.params['template']
|
template = module.params['template']
|
||||||
|
|
||||||
if not template:
|
if not template:
|
||||||
|
@ -245,16 +285,27 @@ def main():
|
||||||
if proxmox.download_template(node, storage, template, timeout):
|
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))
|
module.exit_json(changed=True, msg='template with volid=%s:%s/%s downloaded' % (storage, content_type, template))
|
||||||
|
|
||||||
|
if not src and not url:
|
||||||
|
module.fail_json(msg='src or url param for uploading template file is mandatory')
|
||||||
|
elif not url:
|
||||||
template = os.path.basename(src)
|
template = os.path.basename(src)
|
||||||
if proxmox.has_template(node, storage, content_type, template) and not module.params['force']:
|
if proxmox.has_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))
|
module.exit_json(changed=False, msg='template with volid=%s:%s/%s 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)):
|
elif not (os.path.exists(src) and os.path.isfile(src)):
|
||||||
module.fail_json(msg='template file on path %s not exists' % src)
|
module.fail_json(msg='template file on path %s not exists' % src)
|
||||||
|
|
||||||
if proxmox.upload_template(node, storage, content_type, src, timeout):
|
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))
|
module.exit_json(changed=True, msg='template with volid=%s:%s/%s uploaded' % (storage, content_type, template))
|
||||||
|
elif not src:
|
||||||
|
template = os.path.basename(urlparse(url).path)
|
||||||
|
if proxmox.has_template(node, storage, content_type, template):
|
||||||
|
if not module.params['force']:
|
||||||
|
module.exit_json(changed=False, msg='template with volid=%s:%s/%s already exists' % (storage, content_type, template))
|
||||||
|
elif not proxmox.delete_template(node, storage, content_type, template, timeout):
|
||||||
|
module.fail_json(changed=False, msg='failed to delete template with volid=%s:%s/%s' % (storage, content_type, template))
|
||||||
|
|
||||||
|
if proxmox.fetch_template(node, storage, content_type, url, timeout):
|
||||||
|
module.exit_json(changed=True, msg='template with volid=%s:%s/%s uploaded' % (storage, content_type, template))
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue