Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Add Terraform outputs as extra variables #77

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/ati/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import json
import os

from ati import __name__, __version__
from ati import __name__, __version__
from ati.terraform import (
get_stage_root, iterhosts, iterresources,
query_host, query_hostfile, query_list, tfstates,
iter_states)
iter_states, terraform_outputs)

def get_args():
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -45,7 +45,7 @@ def get_args():
# extra aws args
parser.add_argument('--aws_name_key',
# also defaulted in ati.terraform.aws_host
default='tags.Name',
default='tags.Name',
action='store',
help='resouce attribute key to use as a name')
parser.add_argument('--aws_ssh_host_key',
Expand Down Expand Up @@ -73,9 +73,12 @@ def cli():
parser.exit()

if args.noterraform:
hosts = iterhosts(iterresources(tfstates(args.root)), args)
states = list(tfstates(args.root))
else:
hosts = iterhosts(iterresources(iter_states(args.root)), args)
states = list(iter_states(args.root))

outputs = terraform_outputs(states)
hosts = iterhosts(iterresources(states), args, outputs)

if args.list:
output = query_list(hosts)
Expand Down
26 changes: 21 additions & 5 deletions src/ati/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,29 @@ def iter_states(root=None):

def iterresources(sources):
for source in sources:
if type(source) in [unicode, str]:
if type(source) in [unicode, str]:
with open(source, 'r') as json_file:
state = json.load(json_file)
else:
state = source
else:
state = source
for module in state['modules']:
name = module['path'][-1]
for key, resource in list(module['resources'].items()):
yield name, key, resource


def terraform_outputs(sources):
for source in sources:
if type(source) in [unicode, str]:
with open(source, 'r') as json_file:
state = json.load(json_file)
else:
state = source
for module in state['modules']:
for key, value in module['outputs'].items():
yield key, value['value']


def get_stage_root(tf_dirname=None, root=None):
"""Look for a terraform root directory to match the inventory root directory.

Expand Down Expand Up @@ -101,7 +113,7 @@ def _clean_dc(dcname):
return re.sub('[^\w_\-]', '-', dcname)


def iterhosts(resources, args):
def iterhosts(resources, args, outputs):
'''yield host tuples of (name, attributes, groups)'''
for module_name, key, resource in resources:
resource_type, name = key.split('.', 1)
Expand All @@ -110,7 +122,7 @@ def iterhosts(resources, args):
except KeyError:
continue

yield parser(resource, module_name, args=args)
yield parser(resource, module_name, args=args, outputs=outputs)


def parses(prefix):
Expand Down Expand Up @@ -604,6 +616,10 @@ def aws_host(resource, module_name, **kwargs):
groups.append('role=' + attrs['role'])
groups.append('dc=' + attrs['consul_dc'])

# add outputs to attrs
for (k, v) in kwargs.get('outputs', []):
attrs[k] = v

return name, attrs, groups


Expand Down