40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
import os
|
|
import requests
|
|
|
|
def main():
|
|
module = AnsibleModule(argument_spec={})
|
|
|
|
token = os.getenv("LINODE_TOKEN")
|
|
if not token:
|
|
module.fail_json(msg="LINODE_TOKEN is not set")
|
|
|
|
response = requests.get("https://api.linode.com/v4/linode/instances", headers={
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json"
|
|
})
|
|
|
|
if response.status_code != 200:
|
|
module.fail_json(msg=f"Linode API error: {response.status_code}", details=response.text)
|
|
|
|
linodes = response.json().get("data", [])
|
|
results = {
|
|
"changed": False,
|
|
"linodes": [
|
|
{
|
|
"name": l["label"],
|
|
"ip": l["ipv4"][0] if l["ipv4"] else None,
|
|
"region": l["region"],
|
|
"tags": l["tags"],
|
|
"type": l["type"]
|
|
} for l in linodes
|
|
]
|
|
}
|
|
|
|
module.exit_json(**results)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|