1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Clean up group name before creation to remove spaces etc .. (#22020)

This commit is contained in:
Damien 2017-03-01 03:20:28 -08:00 committed by John R Barker
parent 029ccf9aa0
commit 8032c9a715

View file

@ -51,9 +51,9 @@ Version: 0.2.0
""" """
import os import os
import argparse import argparse
import re
from ansible.compat.six.moves import configparser from ansible.compat.six.moves import configparser
import os
try: try:
from apstra.aosom.session import Session from apstra.aosom.session import Session
@ -485,6 +485,7 @@ class AosInventory(object):
except: except:
pass pass
def parse_cli_args(self): def parse_cli_args(self):
""" Command line argument processing """ """ Command line argument processing """
@ -503,13 +504,16 @@ class AosInventory(object):
def add_host_to_group(self, group, host): def add_host_to_group(self, group, host):
# Check if the group exist, if not initialize it # Cleanup group name first
if group not in self.inventory.keys(): clean_group = self.cleanup_group_name(group)
self.inventory[group] = {}
self.inventory[group]['hosts'] = []
self.inventory[group]['vars'] = {}
self.inventory[group]['hosts'].append(host) # Check if the group exist, if not initialize it
if clean_group not in self.inventory.keys():
self.inventory[clean_group] = {}
self.inventory[clean_group]['hosts'] = []
self.inventory[clean_group]['vars'] = {}
self.inventory[clean_group]['hosts'].append(host)
def add_var_to_host(self, host, var, value): def add_var_to_host(self, host, var, value):
@ -521,13 +525,16 @@ class AosInventory(object):
def add_var_to_group(self, group, var, value): def add_var_to_group(self, group, var, value):
# Check if the group exist, if not initialize it # Cleanup group name first
if group not in self.inventory.keys(): clean_group = self.cleanup_group_name(group)
self.inventory[group] = {}
self.inventory[group]['hosts'] = []
self.inventory[group]['vars'] = {}
self.inventory[group]['vars'][var] = value # Check if the group exist, if not initialize it
if clean_group not in self.inventory.keys():
self.inventory[clean_group] = {}
self.inventory[clean_group]['hosts'] = []
self.inventory[clean_group]['vars'] = {}
self.inventory[clean_group]['vars'][var] = value
def add_device_facts_to_var(self, device_name, device): def add_device_facts_to_var(self, device_name, device):
@ -545,6 +552,17 @@ class AosInventory(object):
elif key == 'hw_model': elif key == 'hw_model':
self.add_host_to_group(value, device_name) self.add_host_to_group(value, device_name)
def cleanup_group_name(self, group_name):
"""
Clean up group name by :
- Replacing all non-alphanumeric caracter by underscore
- Converting to lowercase
"""
rx = re.compile('\W+')
clean_group = rx.sub('_', group_name).lower()
return clean_group
# Run the script # Run the script
if __name__ == '__main__': if __name__ == '__main__':