Let's just create a role that populates the Linode inventory..
This commit is contained in:
@@ -1,7 +0,0 @@
|
|||||||
plugin: linode.cloud.linode
|
|
||||||
api_token: '{{ lookup("env", "ANSIBLE_PASSWORD") }}'
|
|
||||||
regions:
|
|
||||||
- us-east
|
|
||||||
instance_tags:
|
|
||||||
- production
|
|
||||||
- k3s
|
|
16
playbooks/inventory/linode_inventory.yml
Normal file
16
playbooks/inventory/linode_inventory.yml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
---
|
||||||
|
- name: Build Inventory from Linode
|
||||||
|
hosts: localhost
|
||||||
|
gather_facts: false
|
||||||
|
|
||||||
|
roles:
|
||||||
|
- populate_inventory
|
||||||
|
|
||||||
|
- name: Test connection to Linode VMs
|
||||||
|
hosts: all
|
||||||
|
gather_facts: false
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
- name: Ping each Linode
|
||||||
|
ansible.builtin.ping
|
||||||
|
|
47
roles/inventory/linode_inventory/files/linode_inventory.py
Executable file
47
roles/inventory/linode_inventory/files/linode_inventory.py
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
def main():
|
||||||
|
linode_token = os.getenv("LINODE_API_TOKEN") or os.getenv("ANSIBLE_PASSWORD")
|
||||||
|
if not linode_token:
|
||||||
|
print("Error: LINODE_API_TOKEN or ANSIBLE_PASSWORD environment variable not set.", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
headers = {"Authorization": f"Bearer {linode_token}"}
|
||||||
|
response = requests.get("https://api.linode.com/v4/linode/instances", headers=headers)
|
||||||
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
print(f"Error fetching Linode instances: {response.status_code}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
linodes = response.json()["data"]
|
||||||
|
|
||||||
|
inventory = {
|
||||||
|
"all": {
|
||||||
|
"hosts": [],
|
||||||
|
"vars": {}
|
||||||
|
},
|
||||||
|
"_meta": {
|
||||||
|
"hostvars": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for linode in linodes:
|
||||||
|
hostname = linode["label"]
|
||||||
|
ipv4 = linode["ipv4"]
|
||||||
|
|
||||||
|
if hostname and ipv4:
|
||||||
|
public_ip = ipv4[0]
|
||||||
|
inventory["all"]["hosts"].append(hostname)
|
||||||
|
inventory["_meta"]["hostvars"][hostname] = {
|
||||||
|
"ansible_host": public_ip
|
||||||
|
}
|
||||||
|
|
||||||
|
print(json.dumps(inventory, indent=2))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
17
roles/inventory/linode_inventory/tasks/main.yml
Normal file
17
roles/inventory/linode_inventory/tasks/main.yml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
- name: Run Linode dynamic inventory script
|
||||||
|
ansible.builtin.command: "{{ playbook_dir }}/roles/inventory/linode_inventory/files/linode_inventory.py"
|
||||||
|
register: linode_inventory_raw
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Parse JSON inventory output
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
linode_inventory: "{{ linode_inventory_raw.stdout | from_json }}"
|
||||||
|
|
||||||
|
- name: Add Linode hosts dynamically
|
||||||
|
ansible.builtin.add_host:
|
||||||
|
name: "{{ item.key }}"
|
||||||
|
ansible_host: "{{ item.value.ansible_host }}"
|
||||||
|
groups: all
|
||||||
|
loop: "{{ linode_inventory['_meta']['hostvars'] | dict2items }}"
|
||||||
|
|
Reference in New Issue
Block a user