Skip to content

Instantly share code, notes, and snippets.

@opsroller
Forked from haarcuba/lxd.py
Created March 16, 2018 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opsroller/391dc1a5849471edbc4f0c97dd54677d to your computer and use it in GitHub Desktop.
Save opsroller/391dc1a5849471edbc4f0c97dd54677d 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