diff options
author | Peter Schiffer <pschiffe@redhat.com> | 2018-03-14 19:09:06 +0100 |
---|---|---|
committer | ansibot <ansibot@users.noreply.github.com> | 2018-03-14 14:09:06 -0400 |
commit | 8c992ccbc7a60ad7d6e8ac4b6130ec01c8075303 (patch) | |
tree | 131df7885ba2feefd9393da89e592bc2829d0b33 /contrib/inventory/gce.py | |
parent | dd0140dfed55fe8e30699c5f976181f25513f005 (diff) | |
download | ansible-8c992ccbc7a60ad7d6e8ac4b6130ec01c8075303.tar.gz |
Allow filtering of instances by tags in GCE dynamic inventory (#35770)
* Allow filtering of instances by tags in GCE dynamic inventory
* Optimize instance tags configuration
* Fix for PEP8
Diffstat (limited to 'contrib/inventory/gce.py')
-rwxr-xr-x | contrib/inventory/gce.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index 9a0cef0b59..d0cc6b1553 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -222,6 +222,7 @@ class GceInventory(object): 'gce_project_id': '', 'gce_zone': '', 'libcloud_secrets': '', + 'instance_tags': '', 'inventory_ip_type': '', 'cache_path': '~/.ansible/tmp', 'cache_max_age': '300' @@ -247,6 +248,16 @@ class GceInventory(object): if states: self.instance_states = states.split(',') + # Set the instance_tags filter, env var overrides config from file + # and cli param overrides all + if self.args.instance_tags: + self.instance_tags = self.args.instance_tags + else: + self.instance_tags = os.environ.get( + 'GCE_INSTANCE_TAGS', config.get('gce', 'instance_tags')) + if self.instance_tags: + self.instance_tags = self.instance_tags.split(',') + # Caching cache_path = config.get('cache', 'cache_path') cache_max_age = config.getint('cache', 'cache_max_age') @@ -338,6 +349,8 @@ class GceInventory(object): help='List instances (default: True)') parser.add_argument('--host', action='store', help='Get all information about an instance') + parser.add_argument('--instance-tags', action='store', + help='Only include instances with this tags, separated by comma') parser.add_argument('--pretty', action='store_true', default=False, help='Pretty format (default: False)') parser.add_argument( @@ -430,6 +443,18 @@ class GceInventory(object): if self.instance_states and not node.extra['status'] in self.instance_states: continue + # This check filters on the desired instance tags defined in the + # config file with the instance_tags config option, env var GCE_INSTANCE_TAGS, + # or as the cli param --instance-tags. + # + # If the instance_tags list is _empty_ then _ALL_ instances are returned. + # + # If the instance_tags list is _populated_ then check the current + # instance tags against the instance_tags list. If the instance has + # at least one tag from the instance_tags list, it is returned. + if self.instance_tags and not set(self.instance_tags) & set(node.extra['tags']): + continue + name = node.name meta["hostvars"][name] = self.node_to_dict(node) |