Created
September 10, 2024 16:07
-
-
Save kubilus1/13a695d15deb81c254fd97a9f98f4e7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
try: | |
import boto3 | |
import botocore | |
HAS_BOTO3=True | |
except ImportError: | |
pass # will be captured by imported HAS_BOTO3 | |
from ansible.plugins.inventory import BaseInventoryPlugin | |
ANSIBLE_METADATA = { | |
'metadata_version': '1.0.0', | |
'status': [], | |
'supported_by': '' | |
} | |
DOCUMENTATION = ''' | |
--- | |
module: aws_eks | |
plugin_type: inventory | |
short_description: AWS EKS clusters | |
version_added: "2.9.0" | |
description: "Refer to AWS EKS clusters as inventory" | |
options: | |
author: Matt Kubilus | |
''' | |
class InventoryModule(BaseInventoryPlugin): | |
NAME = 'aws_eks' # used internally by Ansible, it should match the file name but not required#!/usr/bin/env python3 | |
def __init__(self): | |
super(BaseInventoryPlugin, self).__init__() | |
def verify_file(self, path): | |
if super(InventoryModule, self).verify_file(path): | |
if path.endswith(('aws_eks.yml', 'aws_eks.yaml')): | |
return True | |
return False | |
def _get_boto3_client(self): | |
client = boto3.client('eks') | |
return client | |
def _get_all(self): | |
client = self._get_boto3_client() | |
clusters = [] | |
nextToken = '' | |
while True: | |
resp = client.list_clusters(nextToken=nextToken) | |
clusters.extend(resp.get('clusters')) | |
nextToken = resp.get('nextToken') | |
if not nextToken: | |
break | |
return clusters | |
def _add_to_group(self, host, group, parent=None): | |
group = re.sub('[^0-9a-zA-Z_]+', '_', group) | |
if group not in self.inventory.groups: | |
self.inventory.add_group(group) | |
if parent: | |
self.inventory.add_child(parent, group) | |
self.inventory.add_host(host, group=group) | |
def parse(self, inventory, loader, path, cache=True): | |
super(InventoryModule, self).parse(inventory, loader, path, cache) | |
config = self._read_config_data(path) | |
if not HAS_BOTO3: | |
raise AnsibleError('The EKS dynamic inventory plugin requires boto3 and botocore.') | |
config_data = self._read_config_data(path) | |
client = self._get_boto3_client() | |
clusters = self._get_all() | |
for cluster in clusters: | |
self.inventory.add_host(cluster) | |
self._add_to_group(cluster, "aws_eks", "all") | |
data = client.describe_cluster(name=cluster) | |
cluster_data = data.get('cluster', {}) | |
status = cluster_data.get('status') | |
self._add_to_group(cluster, "status_%s" % status, 'all') | |
vpc_id = cluster_data.get('resourcesVpcConfig',{}).get('vpcId') | |
if vpc_id: | |
self._add_to_group(cluster, vpc_id, 'all') | |
version = cluster_data.get('version') | |
self._add_to_group(cluster, "version_%s" % version, 'all') | |
tags = cluster_data.get('tags') | |
for k,v in tags.items(): | |
self._add_to_group(cluster, "tag_%s_%s" % (k,v), 'all') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment