feat: enhance ensure_ansible_collections function to check and install missing Ansible collections
Some checks failed
Nix Format Check / check-format (push) Failing after 38s
Some checks failed
Nix Format Check / check-format (push) Failing after 38s
This commit is contained in:
parent
6cb059ebce
commit
9bc865e151
@ -28,15 +28,47 @@ def ensure_ansible_collections():
|
|||||||
]
|
]
|
||||||
|
|
||||||
printfe("cyan", "Checking for required Ansible collections...")
|
printfe("cyan", "Checking for required Ansible collections...")
|
||||||
status, output = run_command(["ansible-galaxy", "collection", "list"], shell=False)
|
|
||||||
|
|
||||||
if not status:
|
# Get default collections paths from Ansible config
|
||||||
printfe("red", "Failed to list Ansible collections")
|
collections_paths = []
|
||||||
return False
|
try:
|
||||||
|
status, output = run_command(["ansible-config", "dump", "COLLECTIONS_PATHS"], shell=False)
|
||||||
|
if status and output.strip():
|
||||||
|
# Extract paths from output which is in format "COLLECTIONS_PATHS(default) = ['/path1', '/path2']"
|
||||||
|
import re
|
||||||
|
paths_match = re.search(r'\[(.*)\]', output)
|
||||||
|
if paths_match:
|
||||||
|
paths_str = paths_match.group(1)
|
||||||
|
# Split by comma, strip quotes and whitespace
|
||||||
|
collections_paths = [p.strip().strip("'\"") for p in paths_str.split(',')]
|
||||||
|
except Exception as e:
|
||||||
|
printfe("yellow", f"Failed to get collections paths: {e}")
|
||||||
|
|
||||||
# Check each required collection and install if missing
|
# Add default paths if we couldn't get them from config
|
||||||
|
if not collections_paths:
|
||||||
|
collections_paths = [
|
||||||
|
os.path.expanduser("~/.ansible/collections"),
|
||||||
|
"/usr/share/ansible/collections"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Check if each required collection is installed
|
||||||
|
missing_collections = []
|
||||||
for collection in required_collections:
|
for collection in required_collections:
|
||||||
if collection not in output:
|
collection_found = False
|
||||||
|
namespace, name = collection.split('.')
|
||||||
|
|
||||||
|
for path in collections_paths:
|
||||||
|
collection_path = os.path.join(path, "ansible_collections", namespace, name)
|
||||||
|
if os.path.exists(collection_path):
|
||||||
|
collection_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not collection_found:
|
||||||
|
missing_collections.append(collection)
|
||||||
|
|
||||||
|
# Install missing collections
|
||||||
|
if missing_collections:
|
||||||
|
for collection in missing_collections:
|
||||||
printfe("yellow", f"Installing {collection} collection...")
|
printfe("yellow", f"Installing {collection} collection...")
|
||||||
status, install_output = run_command(["ansible-galaxy", "collection", "install", collection], shell=False)
|
status, install_output = run_command(["ansible-galaxy", "collection", "install", collection], shell=False)
|
||||||
if not status:
|
if not status:
|
||||||
@ -44,6 +76,8 @@ def ensure_ansible_collections():
|
|||||||
printfe("yellow", f"Continuing anyway, but playbook might fail if it requires {collection}")
|
printfe("yellow", f"Continuing anyway, but playbook might fail if it requires {collection}")
|
||||||
else:
|
else:
|
||||||
printfe("green", f"Successfully installed {collection} collection")
|
printfe("green", f"Successfully installed {collection} collection")
|
||||||
|
else:
|
||||||
|
printfe("green", "All required collections are already installed.")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user