Skip to content

Commit 7a0e414

Browse files
authored
Merge pull request #189 from futuriste/AddProxmoxPool
Add Proxmox pools management
2 parents 8fbe4fc + 04b8a08 commit 7a0e414

File tree

5 files changed

+188
-1
lines changed

5 files changed

+188
-1
lines changed

defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pve_manage_hosts_enabled: yes
3838
pve_datacenter_cfg: {}
3939
pve_cluster_ha_groups: []
4040
# additional roles for your cluster (f.e. for monitoring)
41+
pve_pools: []
4142
pve_roles: []
4243
pve_groups: []
4344
pve_users: []

library/proxmox_pool.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
ANSIBLE_METADATA = {
5+
'metadata_version': '1.0',
6+
'status': ['stableinterface'],
7+
'supported_by': 'futuriste'
8+
}
9+
10+
DOCUMENTATION = '''
11+
---
12+
module: proxmox_pool
13+
14+
short_description: Manages pools in Proxmox
15+
16+
options:
17+
name:
18+
required: true
19+
aliases: [ "pool", "poolid" ]
20+
description:
21+
- Name of the PVE pool to manage.
22+
state:
23+
required: false
24+
default: "present"
25+
choices: [ "present", "absent" ]
26+
description:
27+
- Specifies whether the pool should exist or not.
28+
comment:
29+
required: false
30+
description:
31+
- Optionally sets the pool's comment in PVE.
32+
33+
author:
34+
- Guiffo Joel (@futuriste)
35+
'''
36+
37+
EXAMPLES = '''
38+
- name: Create Administrators pool
39+
proxmox_pool:
40+
name: Administrators
41+
- name: Create Dev Users pool's
42+
proxmox_pool:
43+
name: pool_dev
44+
comment: Dev Users allowed to access on this pool.
45+
'''
46+
47+
RETURN = '''
48+
updated_fields:
49+
description: Fields that were modified for an existing pool
50+
type: list
51+
pool:
52+
description: Information about the pool fetched from PVE after this task completed.
53+
type: json
54+
'''
55+
56+
from ansible.module_utils.basic import AnsibleModule
57+
from ansible.module_utils._text import to_text
58+
from ansible.module_utils.pvesh import ProxmoxShellError
59+
import ansible.module_utils.pvesh as pvesh
60+
61+
class ProxmoxPool(object):
62+
def __init__(self, module):
63+
self.module = module
64+
self.name = module.params['name']
65+
self.state = module.params['state']
66+
self.comment = module.params['comment']
67+
68+
def lookup(self):
69+
try:
70+
return pvesh.get("pools/{}".format(self.name))
71+
except ProxmoxShellError as e:
72+
self.module.fail_json(msg=e.message, status_code=e.status_code, **result)
73+
74+
def remove_pool(self):
75+
try:
76+
pvesh.delete("pools/{}".format(self.name))
77+
return (True, None)
78+
except ProxmoxShellError as e:
79+
return (False, e.message)
80+
81+
def create_pool(self):
82+
new_pool = {}
83+
if self.comment is not None:
84+
new_pool['comment'] = self.comment
85+
86+
try:
87+
pvesh.create("pools", poolid=self.name, **new_pool)
88+
return (True, None)
89+
except ProxmoxShellError as e:
90+
return (False, e.message)
91+
92+
def modify_pool(self):
93+
lookup = self.lookup()
94+
staged_pool = {}
95+
96+
if self.comment is not None:
97+
staged_pool['comment'] = self.comment
98+
99+
updated_fields = []
100+
error = None
101+
102+
for key in staged_pool:
103+
staged_value = to_text(staged_pool[key]) if isinstance(staged_pool[key], str) else staged_pool[key]
104+
if key not in lookup or staged_value != lookup[key]:
105+
updated_fields.append(key)
106+
107+
if self.module.check_mode:
108+
self.module.exit_json(changed=bool(updated_fields), expected_changes=updated_fields)
109+
110+
if not updated_fields:
111+
# No changes necessary
112+
return (updated_fields, error)
113+
114+
try:
115+
pvesh.set("pools/{}".format(self.name), **staged_pool)
116+
except ProxmoxShellError as e:
117+
error = e.message
118+
119+
return (updated_fields, error)
120+
121+
def main():
122+
# Refer to https://pve.proxmox.com/pve-docs/api-viewer/index.html
123+
module = AnsibleModule(
124+
argument_spec = dict(
125+
name=dict(type='str', required=True, aliases=['pool', 'poolid']),
126+
state=dict(default='present', choices=['present', 'absent'], type='str'),
127+
comment=dict(default=None, type='str'),
128+
),
129+
supports_check_mode=True
130+
)
131+
132+
pool = ProxmoxPool(module)
133+
134+
changed = False
135+
error = None
136+
result = {}
137+
result['name'] = pool.name
138+
result['state'] = pool.state
139+
140+
if pool.state == 'absent':
141+
if pool.lookup() is not None:
142+
if module.check_mode:
143+
module.exit_json(changed=True)
144+
145+
(changed, error) = pool.remove_pool()
146+
147+
if error is not None:
148+
module.fail_json(name=pool.name, msg=error)
149+
elif pool.state == 'present':
150+
if not pool.lookup():
151+
if module.check_mode:
152+
module.exit_json(changed=True)
153+
154+
(changed, error) = pool.create_pool()
155+
else:
156+
# modify pool (note: this function is check mode aware)
157+
(updated_fields, error) = pool.modify_pool()
158+
159+
if updated_fields:
160+
changed = True
161+
result['updated_fields'] = updated_fields
162+
163+
if error is not None:
164+
module.fail_json(name=pool.name, msg=error)
165+
166+
lookup = pool.lookup()
167+
if lookup is not None:
168+
result['pool'] = lookup
169+
170+
result['changed'] = changed
171+
172+
module.exit_json(**result)
173+
174+
if __name__ == '__main__':
175+
main()

module_utils/pvesh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def run_command(handler, resource, **params):
5757
if handler == "get":
5858
if any(re.match(pattern, stderr[0]) for pattern in [
5959
"^no such user \('.{3,64}?'\)$",
60-
"^(group|role) '[A-Za-z0-9\.\-_]+' does not exist$",
60+
"^(group|role|pool) '[A-Za-z0-9\.\-_]+' does not exist$",
6161
"^domain '[A-Za-z][A-Za-z0-9\.\-_]+' does not exist$"]):
6262
return {u"status": 404, u"message": stderr[0]}
6363

tasks/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@
202202
- "pve_ceph_enabled | bool"
203203
- "inventory_hostname in groups[pve_ceph_nodes]"
204204

205+
- name: Configure Proxmox pools
206+
proxmox_pool:
207+
name: "{{ item.name }}"
208+
state: "{{ item.state | default('present') }}"
209+
comment: "{{ item.comment | default(omit) }}"
210+
with_items: "{{ pve_pools }}"
211+
when: "not pve_cluster_enabled or (pve_cluster_enabled | bool and inventory_hostname == groups[pve_group][0])"
212+
205213
- name: Configure Proxmox roles
206214
proxmox_role:
207215
name: "{{ item.name }}"

tests/vagrant/group_vars/all

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pve_zfs_zed_email: root@localhost
1212
pve_cluster_enabled: yes
1313
pve_datacenter_cfg:
1414
console: xtermjs
15+
pve_pools:
16+
- name: customer01
17+
comment: Pool for customer01
1518
pve_groups:
1619
- name: Admins
1720
comment: Administrators of this PVE cluster

0 commit comments

Comments
 (0)