-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for showing ansible templates.
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
Showing
8 changed files
with
133 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters