Skip to content

Commit

Permalink
Add support for showing ansible templates.
Browse files Browse the repository at this point in the history
Add support for specifying absolute path to ansible inventory in
config file.
  • Loading branch information
Dan Ellis committed Jun 2, 2015
1 parent b26de33 commit 104401b
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 80 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pointing to your vault password file:
[vault]
password_file = ~/.vault

By default, inventory will be read from the path `inventory`, relative to the
directory an Ansible Toolkit command is run from. To override:

[inventory]
path = /home/foo/inventory

Usage
-----

Expand All @@ -26,3 +32,15 @@ when they are overridden by a subgroup.
Group Variables (sub-group)
- ['foo'] = 3
+ ['foo'] = 5

### atk-show-template ###

You can display what the output of a template will be when applied against a
host. For instance, if this were the template:

template_key = {{ template_var }}

And the value of `template_var` was set to `3` for `host`, the output would be:

$ atk-show-template host roles/foo/templates/template.j2
template_key = 3
74 changes: 0 additions & 74 deletions ansible_toolkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,74 +0,0 @@
#!/usr/bin/env python

from ansible.utils import combine_vars, template
from ansible.inventory import Inventory
from ansible.runner import Runner

from utils import green, red, get_vault_password


def show_diff(old, new):
for k, v in new.iteritems():
if k in old.keys() and v == old[k]:
continue
if k in old.keys() and v != old[k]:
red(" - ['{}'] = {}".format(k, old[k]))
green(" + ['{}'] = {}".format(k, v))


def get_inject_vars(self, host):

host_variables = self.inventory.get_variables(
host, vault_password=self.vault_pass)
ansible_host = self.inventory.get_host(host)

# Keep track of variables in the order they will be merged
to_merge = [
('Default Variables', self.default_vars),
]

# Group variables
groups = ansible_host.get_groups()
for group in sorted(groups, key=lambda g: g.depth):
to_merge.append(
("Group Variables ({})".format(group.name), group.get_variables())
)

combined_cache = self.get_combined_cache()

# use combined_cache and host_variables to template the module_vars
# we update the inject variables with the data we're about to template
# since some of the variables we'll be replacing may be contained there too
module_vars_inject = combine_vars(
host_variables, combined_cache.get(host, {}))
module_vars_inject = combine_vars(
self.module_vars, module_vars_inject)
module_vars = template.template(
self.basedir, self.module_vars, module_vars_inject)

inject = {}
to_merge.extend([
('Host Variables', ansible_host.vars),
('Setup Cache', self.setup_cache.get(host, {})),
('Play Variables', self.play_vars),
('Play File Variables', self.play_file_vars),
('Role Variables', self.role_vars),
('Module Variables', module_vars),
('Variables Cache', self.vars_cache.get(host, {})),
('Role Parameters', self.role_params),
('Extra Variables', self.extra_vars),
])
for name, value in to_merge:
old_inject = inject
inject = combine_vars(inject, value)
print name
show_diff(old_inject, inject)

return inject


def show_vars(host):
inventory = Inventory('inventory', vault_password=get_vault_password())
Runner.get_inject_vars = get_inject_vars
runner = Runner(inventory=inventory)
runner.get_inject_vars(host)
11 changes: 11 additions & 0 deletions ansible_toolkit/show_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ansible.runner import Runner
from ansible.utils.template import template_from_file

from utils import get_inventory


def show_template(host, path):
inventory = get_inventory()
runner = Runner(inventory=inventory)
host_vars = runner.get_inject_vars(host)
print template_from_file('.', path, host_vars)
71 changes: 71 additions & 0 deletions ansible_toolkit/show_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from ansible.runner import Runner
from ansible.utils import combine_vars, template

from utils import green, red, get_vault_password, get_inventory


def show_diff(old, new):
for k, v in new.iteritems():
if k in old.keys() and v == old[k]:
continue
if k in old.keys() and v != old[k]:
red(" - ['{}'] = {}".format(k, old[k]))
green(" + ['{}'] = {}".format(k, v))


def get_inject_vars(self, host):

host_variables = self.inventory.get_variables(
host, vault_password=self.vault_pass)
ansible_host = self.inventory.get_host(host)

# Keep track of variables in the order they will be merged
to_merge = [
('Default Variables', self.default_vars),
]

# Group variables
groups = ansible_host.get_groups()
for group in sorted(groups, key=lambda g: g.depth):
to_merge.append(
("Group Variables ({})".format(group.name), group.get_variables())
)

combined_cache = self.get_combined_cache()

# use combined_cache and host_variables to template the module_vars
# we update the inject variables with the data we're about to template
# since some of the variables we'll be replacing may be contained there too
module_vars_inject = combine_vars(
host_variables, combined_cache.get(host, {}))
module_vars_inject = combine_vars(
self.module_vars, module_vars_inject)
module_vars = template.template(
self.basedir, self.module_vars, module_vars_inject)

inject = {}
to_merge.extend([
('Host Variables', ansible_host.vars),
('Setup Cache', self.setup_cache.get(host, {})),
('Play Variables', self.play_vars),
('Play File Variables', self.play_file_vars),
('Role Variables', self.role_vars),
('Module Variables', module_vars),
('Variables Cache', self.vars_cache.get(host, {})),
('Role Parameters', self.role_params),
('Extra Variables', self.extra_vars),
])
for name, value in to_merge:
old_inject = inject
inject = combine_vars(inject, value)
print name
show_diff(old_inject, inject)

return inject


def show_vars(host):
inventory = get_inventory()
Runner.get_inject_vars = get_inject_vars
runner = Runner(inventory=inventory)
runner.get_inject_vars(host)
21 changes: 17 additions & 4 deletions ansible_toolkit/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import ConfigParser
import os

from ansible.inventory import Inventory


config = ConfigParser.ConfigParser()

config.read([os.path.expanduser('~/.atk')])


# Terminal Colors

Expand All @@ -19,14 +26,20 @@ def red(text):

# Vault Password

config = ConfigParser.ConfigParser()


def get_vault_password():
config.read(['site.cfg', os.path.expanduser('~/.atk')])
try:
password_file = config.get('vault', 'password_file')
with open(password_file, 'rb') as f:
return f.read()
except ConfigParser.NoSectionError:
return None


# Inventory

def get_inventory():
try:
inventory_path = config.get('inventory', 'path')
except ConfigParser.NoSectionError:
inventory_path = 'inventory'
return Inventory(inventory_path, vault_password=get_vault_password())
13 changes: 13 additions & 0 deletions bin/atk-show-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python

import argparse

from ansible_toolkit.show_template import show_template


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('host')
parser.add_argument('path')
args = parser.parse_args()
show_template(args.host, args.path)
2 changes: 1 addition & 1 deletion bin/atk-show-vars
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
import sys

from ansible_toolkit import show_vars
from ansible_toolkit.show_vars import show_vars


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


setup(name='ansible-toolkit',
version='1.0',
version='1.1',
description='The missing Ansible tools',
url='http://github.com/dellis23/ansible-toolkit',
author='Daniel Ellis',
Expand All @@ -12,4 +12,5 @@
packages=['ansible_toolkit'],
scripts=[
'bin/atk-show-vars',
'bin/atk-show-template',
])

0 comments on commit 104401b

Please sign in to comment.