Skip to content

Commit 104401b

Browse files
author
Dan Ellis
committed
Add support for showing ansible templates.
Add support for specifying absolute path to ansible inventory in config file.
1 parent b26de33 commit 104401b

File tree

8 files changed

+133
-80
lines changed

8 files changed

+133
-80
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pointing to your vault password file:
1212
[vault]
1313
password_file = ~/.vault
1414

15+
By default, inventory will be read from the path `inventory`, relative to the
16+
directory an Ansible Toolkit command is run from. To override:
17+
18+
[inventory]
19+
path = /home/foo/inventory
20+
1521
Usage
1622
-----
1723

@@ -26,3 +32,15 @@ when they are overridden by a subgroup.
2632
Group Variables (sub-group)
2733
- ['foo'] = 3
2834
+ ['foo'] = 5
35+
36+
### atk-show-template ###
37+
38+
You can display what the output of a template will be when applied against a
39+
host. For instance, if this were the template:
40+
41+
template_key = {{ template_var }}
42+
43+
And the value of `template_var` was set to `3` for `host`, the output would be:
44+
45+
$ atk-show-template host roles/foo/templates/template.j2
46+
template_key = 3

ansible_toolkit/__init__.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +0,0 @@
1-
#!/usr/bin/env python
2-
3-
from ansible.utils import combine_vars, template
4-
from ansible.inventory import Inventory
5-
from ansible.runner import Runner
6-
7-
from utils import green, red, get_vault_password
8-
9-
10-
def show_diff(old, new):
11-
for k, v in new.iteritems():
12-
if k in old.keys() and v == old[k]:
13-
continue
14-
if k in old.keys() and v != old[k]:
15-
red(" - ['{}'] = {}".format(k, old[k]))
16-
green(" + ['{}'] = {}".format(k, v))
17-
18-
19-
def get_inject_vars(self, host):
20-
21-
host_variables = self.inventory.get_variables(
22-
host, vault_password=self.vault_pass)
23-
ansible_host = self.inventory.get_host(host)
24-
25-
# Keep track of variables in the order they will be merged
26-
to_merge = [
27-
('Default Variables', self.default_vars),
28-
]
29-
30-
# Group variables
31-
groups = ansible_host.get_groups()
32-
for group in sorted(groups, key=lambda g: g.depth):
33-
to_merge.append(
34-
("Group Variables ({})".format(group.name), group.get_variables())
35-
)
36-
37-
combined_cache = self.get_combined_cache()
38-
39-
# use combined_cache and host_variables to template the module_vars
40-
# we update the inject variables with the data we're about to template
41-
# since some of the variables we'll be replacing may be contained there too
42-
module_vars_inject = combine_vars(
43-
host_variables, combined_cache.get(host, {}))
44-
module_vars_inject = combine_vars(
45-
self.module_vars, module_vars_inject)
46-
module_vars = template.template(
47-
self.basedir, self.module_vars, module_vars_inject)
48-
49-
inject = {}
50-
to_merge.extend([
51-
('Host Variables', ansible_host.vars),
52-
('Setup Cache', self.setup_cache.get(host, {})),
53-
('Play Variables', self.play_vars),
54-
('Play File Variables', self.play_file_vars),
55-
('Role Variables', self.role_vars),
56-
('Module Variables', module_vars),
57-
('Variables Cache', self.vars_cache.get(host, {})),
58-
('Role Parameters', self.role_params),
59-
('Extra Variables', self.extra_vars),
60-
])
61-
for name, value in to_merge:
62-
old_inject = inject
63-
inject = combine_vars(inject, value)
64-
print name
65-
show_diff(old_inject, inject)
66-
67-
return inject
68-
69-
70-
def show_vars(host):
71-
inventory = Inventory('inventory', vault_password=get_vault_password())
72-
Runner.get_inject_vars = get_inject_vars
73-
runner = Runner(inventory=inventory)
74-
runner.get_inject_vars(host)

ansible_toolkit/show_template.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ansible.runner import Runner
2+
from ansible.utils.template import template_from_file
3+
4+
from utils import get_inventory
5+
6+
7+
def show_template(host, path):
8+
inventory = get_inventory()
9+
runner = Runner(inventory=inventory)
10+
host_vars = runner.get_inject_vars(host)
11+
print template_from_file('.', path, host_vars)

ansible_toolkit/show_vars.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from ansible.runner import Runner
2+
from ansible.utils import combine_vars, template
3+
4+
from utils import green, red, get_vault_password, get_inventory
5+
6+
7+
def show_diff(old, new):
8+
for k, v in new.iteritems():
9+
if k in old.keys() and v == old[k]:
10+
continue
11+
if k in old.keys() and v != old[k]:
12+
red(" - ['{}'] = {}".format(k, old[k]))
13+
green(" + ['{}'] = {}".format(k, v))
14+
15+
16+
def get_inject_vars(self, host):
17+
18+
host_variables = self.inventory.get_variables(
19+
host, vault_password=self.vault_pass)
20+
ansible_host = self.inventory.get_host(host)
21+
22+
# Keep track of variables in the order they will be merged
23+
to_merge = [
24+
('Default Variables', self.default_vars),
25+
]
26+
27+
# Group variables
28+
groups = ansible_host.get_groups()
29+
for group in sorted(groups, key=lambda g: g.depth):
30+
to_merge.append(
31+
("Group Variables ({})".format(group.name), group.get_variables())
32+
)
33+
34+
combined_cache = self.get_combined_cache()
35+
36+
# use combined_cache and host_variables to template the module_vars
37+
# we update the inject variables with the data we're about to template
38+
# since some of the variables we'll be replacing may be contained there too
39+
module_vars_inject = combine_vars(
40+
host_variables, combined_cache.get(host, {}))
41+
module_vars_inject = combine_vars(
42+
self.module_vars, module_vars_inject)
43+
module_vars = template.template(
44+
self.basedir, self.module_vars, module_vars_inject)
45+
46+
inject = {}
47+
to_merge.extend([
48+
('Host Variables', ansible_host.vars),
49+
('Setup Cache', self.setup_cache.get(host, {})),
50+
('Play Variables', self.play_vars),
51+
('Play File Variables', self.play_file_vars),
52+
('Role Variables', self.role_vars),
53+
('Module Variables', module_vars),
54+
('Variables Cache', self.vars_cache.get(host, {})),
55+
('Role Parameters', self.role_params),
56+
('Extra Variables', self.extra_vars),
57+
])
58+
for name, value in to_merge:
59+
old_inject = inject
60+
inject = combine_vars(inject, value)
61+
print name
62+
show_diff(old_inject, inject)
63+
64+
return inject
65+
66+
67+
def show_vars(host):
68+
inventory = get_inventory()
69+
Runner.get_inject_vars = get_inject_vars
70+
runner = Runner(inventory=inventory)
71+
runner.get_inject_vars(host)

ansible_toolkit/utils.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import ConfigParser
22
import os
33

4+
from ansible.inventory import Inventory
5+
6+
7+
config = ConfigParser.ConfigParser()
8+
9+
config.read([os.path.expanduser('~/.atk')])
10+
411

512
# Terminal Colors
613

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

2027
# Vault Password
2128

22-
config = ConfigParser.ConfigParser()
23-
24-
2529
def get_vault_password():
26-
config.read(['site.cfg', os.path.expanduser('~/.atk')])
2730
try:
2831
password_file = config.get('vault', 'password_file')
2932
with open(password_file, 'rb') as f:
3033
return f.read()
3134
except ConfigParser.NoSectionError:
3235
return None
36+
37+
38+
# Inventory
39+
40+
def get_inventory():
41+
try:
42+
inventory_path = config.get('inventory', 'path')
43+
except ConfigParser.NoSectionError:
44+
inventory_path = 'inventory'
45+
return Inventory(inventory_path, vault_password=get_vault_password())

bin/atk-show-template

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
5+
from ansible_toolkit.show_template import show_template
6+
7+
8+
if __name__ == '__main__':
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument('host')
11+
parser.add_argument('path')
12+
args = parser.parse_args()
13+
show_template(args.host, args.path)

bin/atk-show-vars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import argparse
44
import sys
55

6-
from ansible_toolkit import show_vars
6+
from ansible_toolkit.show_vars import show_vars
77

88

99
if __name__ == '__main__':

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

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

0 commit comments

Comments
 (0)