Commit 2d38b6ff authored by Sven-Hendrik Haase's avatar Sven-Hendrik Haase
Browse files

Use custom hcloud dynamic inventory script

This has multiple benefits:
- We don't have to wait until Ansible 2.8 (which will be when the official hcloud dynamic inventory provider will be included)
- We can expand the script to group servers by labels if required
- We can use the vault to automatically get the hetzner cloud token which is not possible otherwise
parent b6d766e9
inventory = hosts,inventory_hcloud.yml
inventory = hosts,
library = library
remote_tmp = $HOME/.ansible/tmp
remote_user = root
#!/usr/bin/env python
# Dynamic inventory script for getting infrastructure information from hcloud
import argparse
import json
import yaml
import sys
from hcloud import Client
from ansible import constants as C
from ansible.parsing.vault import VaultLib
from ansible.cli import CLI
from ansible.parsing.dataloader import DataLoader
def parse_args():
parser = argparse.ArgumentParser(description="Hcloud dynamic inventory script")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--list', action='store_true')
return parser.parse_args()
def list_running_hosts(client):
return [ for server in client.servers.get_all()]
def get_host_details(client, host):
server = client.servers.get_by_name(host)
return {'ansible_ssh_host': server.public_net.ipv4.ip,
'ansible_ssh_port': 22,
'ansible_ssh_user': "root"}
def main():
args = parse_args()
loader = DataLoader()
vault_secret = CLI.setup_vault_secrets(
vault = VaultLib(vault_secret)
decrypted = vault.decrypt(open('misc/vault_hetzner.yml').read())
loaded = yaml.load(decrypted)
client = Client(token=loaded["hetzner_cloud_api_key"])
if args.list:
hosts = list_running_hosts(client=client)
json.dump({'hcloud': hosts}, sys.stdout)
details = get_host_details(client,
json.dump(details, sys.stdout)
if __name__ == '__main__':
......@@ -48,9 +48,3 @@
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment