Skip to content

Commit

Permalink
Merge pull request #51 from apigban/create-groups-based-on-proxmox_tags
Browse files Browse the repository at this point in the history
Added feature to create groups based on tags
  • Loading branch information
xezpeleta authored Jun 21, 2023
2 parents 9253b5b + aacf7b1 commit 719103e
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions proxmox.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# Copyright (C) 2014 Mathieu GAUTHIER-LAFAYE <gauthierl@lapth.cnrs.fr>
#
Expand Down Expand Up @@ -415,16 +415,36 @@ def main_list(options, config_path):
}
results[osid]['hosts'] += [vm]

# Create group 'based on proxmox_tags'
# so you can: --limit 'worker,external-datastore'
try:
tags = results['_meta']['hostvars'][vm]['proxmox_tags']
vm_name = results['_meta']['hostvars'][vm]['proxmox_name']
tag_list = split_tags(tags)
for i in range(len(tag_list)):
if tag_list[i] not in results:
results[tag_list[i]] = {
'hosts': []
}
results[tag_list[i]]['hosts'] += [vm]
except KeyError:
pass

results['_meta']['hostvars'][vm].update(metadata)

# pools
for pool in proxmox_api.pools().get_names():
results[pool] = {
'hosts': proxmox_api.pool(pool).get_members_name(),
}

return results

def split_tags(proxmox_tags: str) -> list[str]:
"""
Splits proxmox_tags delimited by comma and returns a list of the tags.
"""
tags = proxmox_tags.split(';')
return tags

def main_host(options, config_path):
proxmox_api = ProxmoxAPI(options, config_path)
Expand Down Expand Up @@ -479,7 +499,7 @@ def main():
indent = None
if options.pretty:
indent = 2

#TODO
print((json.dumps(data, indent=indent)))


Expand Down

0 comments on commit 719103e

Please sign in to comment.