Skip to content

Instantly share code, notes, and snippets.

@haarcuba
Created June 5, 2016 14:37
Show Gist options
  • Save haarcuba/07251871560c729b9f88aa441529ca99 to your computer and use it in GitHub Desktop.
Save haarcuba/07251871560c729b9f88aa441529ca99 to your computer and use it in GitHub Desktop.
ansible inventory module that lists lxd containers
#!/usr/bin/env python3
import sys
import subprocess
import re
def containers():
lxcOutput = subprocess.check_output( ['lxc', 'list' ] )
ips = re.compile( '\d+\.\d+\.\d+\.\d+' ).findall( str( lxcOutput ) )
nodes = dict( hosts = ips, vars = { "ansible_user": "root" } )
inventory = dict( nodes = nodes )
return inventory
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument( '--list', action = 'store_true' )
parser.add_argument( '--host' )
arguments = parser.parse_args()
if arguments.list:
json.dump( containers(), sys.stdout )
if arguments.host:
json.dump( {}, sys.stdout )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment