EE can't execute the fucking python script on its own..
This commit is contained in:
@@ -1,54 +1,81 @@
|
||||
---
|
||||
plugin: linode.cloud.inventory
|
||||
access_token: "{{ lookup('env', 'LINODE_API_TOKEN') }}"
|
||||
|
||||
# Use Linode labels as hostnames
|
||||
strict: false
|
||||
|
||||
# Create groups
|
||||
keyed_groups:
|
||||
# Group by region: region_us_southeast
|
||||
- key: region
|
||||
prefix: region
|
||||
separator: "_"
|
||||
|
||||
# Group by instance type: type_g6_standard_6
|
||||
- key: type
|
||||
prefix: type
|
||||
separator: "_"
|
||||
|
||||
# Group by status: status_running
|
||||
- key: status
|
||||
prefix: status
|
||||
separator: "_"
|
||||
|
||||
# Group by tags: tag_k3s, tag_debian, etc.
|
||||
- key: tags
|
||||
prefix: tag
|
||||
separator: "_"
|
||||
|
||||
# Set host variables
|
||||
plugin: advanced_host_list
|
||||
compose:
|
||||
ansible_host: ipv4[0]
|
||||
ansible_user: phlux
|
||||
linode_id: id
|
||||
linode_region: region
|
||||
linode_type: type
|
||||
linode_status: status
|
||||
linode_tags: tags
|
||||
# This is a workaround - we'll use a playbook to generate the inventory instead
|
||||
|
||||
# Alternative: use the script plugin explicitly
|
||||
plugin: script
|
||||
script: |
|
||||
#!/usr/bin/env python3
|
||||
import json, sys, os, urllib.request
|
||||
from argparse import ArgumentParser
|
||||
|
||||
# Add convenience variables for your tags
|
||||
is_k3s: "'k3s' in (tags | default([]))"
|
||||
is_control_plane: "'control-plane' in (tags | default([]))"
|
||||
is_worker_node: "'worker-node' in (tags | default([]))"
|
||||
is_debian: "'Debian' in (tags | default([]))"
|
||||
is_ubuntu: "'Ubuntu' in (tags | default([]))"
|
||||
def main():
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('--list', action='store_true')
|
||||
parser.add_argument('--host')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.host:
|
||||
print('{}')
|
||||
return
|
||||
|
||||
token = os.environ.get('LINODE_API_TOKEN')
|
||||
if not token:
|
||||
print('{"_meta": {"hostvars": {}}}')
|
||||
return
|
||||
|
||||
# Optional: Only include running instances
|
||||
filters:
|
||||
- status == "running"
|
||||
try:
|
||||
req = urllib.request.Request('https://api.linode.com/v4/linode/instances')
|
||||
req.add_header('Authorization', f'Bearer {token}')
|
||||
|
||||
with urllib.request.urlopen(req, timeout=30) as response:
|
||||
data = json.loads(response.read().decode('utf-8'))
|
||||
|
||||
inventory = {"_meta": {"hostvars": {}}, "all": {"children": []}}
|
||||
groups = {}
|
||||
|
||||
for instance in data.get('data', []):
|
||||
if instance['status'] != 'running':
|
||||
continue
|
||||
|
||||
hostname = instance['label']
|
||||
ip = instance['ipv4'][0] if instance.get('ipv4') else None
|
||||
if not ip:
|
||||
continue
|
||||
|
||||
tags = instance.get('tags', [])
|
||||
inventory['_meta']['hostvars'][hostname] = {
|
||||
'ansible_host': ip,
|
||||
'ansible_user': 'phlux',
|
||||
'linode_region': instance['region'],
|
||||
'linode_type': instance['type'],
|
||||
'linode_tags': tags,
|
||||
'is_k3s': 'k3s' in tags,
|
||||
'is_control_plane': 'control-plane' in tags,
|
||||
'is_worker_node': 'worker-node' in tags
|
||||
}
|
||||
|
||||
for tag in tags:
|
||||
group = f"tag_{tag.replace('-', '_')}"
|
||||
if group not in groups:
|
||||
groups[group] = []
|
||||
groups[group].append(hostname)
|
||||
|
||||
region_group = f"region_{instance['region'].replace('-', '_')}"
|
||||
if region_group not in groups:
|
||||
groups[region_group] = []
|
||||
groups[region_group].append(hostname)
|
||||
|
||||
for group, hosts in groups.items():
|
||||
inventory[group] = {"hosts": hosts}
|
||||
inventory['all']['children'].append(group)
|
||||
|
||||
print(json.dumps(inventory, indent=2))
|
||||
|
||||
except Exception as e:
|
||||
print(f'{{"error": "{e}"}}', file=sys.stderr)
|
||||
print('{"_meta": {"hostvars": {}}}')
|
||||
|
||||
# Cache settings
|
||||
cache: true
|
||||
cache_plugin: memory
|
||||
cache_timeout: 300
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Reference in New Issue
Block a user