|
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) |
0 commit comments