EE can't execute the fucking python script on its own..
This commit is contained in:
@@ -1,54 +1,81 @@
|
|||||||
---
|
---
|
||||||
plugin: linode.cloud.inventory
|
plugin: advanced_host_list
|
||||||
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
|
|
||||||
compose:
|
compose:
|
||||||
ansible_host: ipv4[0]
|
# This is a workaround - we'll use a playbook to generate the inventory instead
|
||||||
ansible_user: phlux
|
|
||||||
linode_id: id
|
|
||||||
linode_region: region
|
|
||||||
linode_type: type
|
|
||||||
linode_status: status
|
|
||||||
linode_tags: tags
|
|
||||||
|
|
||||||
# Add convenience variables for your tags
|
# Alternative: use the script plugin explicitly
|
||||||
is_k3s: "'k3s' in (tags | default([]))"
|
plugin: script
|
||||||
is_control_plane: "'control-plane' in (tags | default([]))"
|
script: |
|
||||||
is_worker_node: "'worker-node' in (tags | default([]))"
|
#!/usr/bin/env python3
|
||||||
is_debian: "'Debian' in (tags | default([]))"
|
import json, sys, os, urllib.request
|
||||||
is_ubuntu: "'Ubuntu' in (tags | default([]))"
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
# Optional: Only include running instances
|
def main():
|
||||||
filters:
|
parser = ArgumentParser()
|
||||||
- status == "running"
|
parser.add_argument('--list', action='store_true')
|
||||||
|
parser.add_argument('--host')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Cache settings
|
if args.host:
|
||||||
cache: true
|
print('{}')
|
||||||
cache_plugin: memory
|
return
|
||||||
cache_timeout: 300
|
|
||||||
|
token = os.environ.get('LINODE_API_TOKEN')
|
||||||
|
if not token:
|
||||||
|
print('{"_meta": {"hostvars": {}}}')
|
||||||
|
return
|
||||||
|
|
||||||
|
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": {}}}')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
Reference in New Issue
Block a user