mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 20:01:25 -07:00
Added the ability to filter gce grouped_instances by region/zone (#14138)
This commit is contained in:
parent
7aa39981c4
commit
bb8d1168ac
1 changed files with 18 additions and 2 deletions
|
@ -125,8 +125,10 @@ class GceInventory(object):
|
||||||
pretty=self.args.pretty))
|
pretty=self.args.pretty))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
zones = self.parse_env_zones()
|
||||||
|
|
||||||
# Otherwise, assume user wants all instances grouped
|
# Otherwise, assume user wants all instances grouped
|
||||||
print(self.json_format_dict(self.group_instances(),
|
print(self.json_format_dict(self.group_instances(zones),
|
||||||
pretty=self.args.pretty))
|
pretty=self.args.pretty))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
@ -233,6 +235,14 @@ class GceInventory(object):
|
||||||
)
|
)
|
||||||
return gce
|
return gce
|
||||||
|
|
||||||
|
def parse_env_zones(self):
|
||||||
|
'''returns a list of comma seperated zones parsed from the GCE_ZONE environment variable.
|
||||||
|
If provided, this will be used to filter the results of the grouped_instances call'''
|
||||||
|
import csv
|
||||||
|
reader = csv.reader([os.environ.get('GCE_ZONE',"")], skipinitialspace=True)
|
||||||
|
zones = [r for r in reader]
|
||||||
|
return [z for z in zones[0]]
|
||||||
|
|
||||||
def parse_cli_args(self):
|
def parse_cli_args(self):
|
||||||
''' Command line argument processing '''
|
''' Command line argument processing '''
|
||||||
|
|
||||||
|
@ -289,7 +299,7 @@ class GceInventory(object):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def group_instances(self):
|
def group_instances(self, zones=None):
|
||||||
'''Group all instances'''
|
'''Group all instances'''
|
||||||
groups = {}
|
groups = {}
|
||||||
meta = {}
|
meta = {}
|
||||||
|
@ -312,6 +322,12 @@ class GceInventory(object):
|
||||||
meta["hostvars"][name] = self.node_to_dict(node)
|
meta["hostvars"][name] = self.node_to_dict(node)
|
||||||
|
|
||||||
zone = node.extra['zone'].name
|
zone = node.extra['zone'].name
|
||||||
|
|
||||||
|
# To avoid making multiple requests per zone
|
||||||
|
# we list all nodes and then filter the results
|
||||||
|
if zones and zone not in zones:
|
||||||
|
continue
|
||||||
|
|
||||||
if groups.has_key(zone): groups[zone].append(name)
|
if groups.has_key(zone): groups[zone].append(name)
|
||||||
else: groups[zone] = [name]
|
else: groups[zone] = [name]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue