Skip to content

Commit 3af933d

Browse files
committed
Move common parts to module_utils
As we need `parse_config` also for further lookup functions we moved it to a `modules_utils` file.
1 parent 1200857 commit 3af933d

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

plugins/module_utils/hieradata.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) 2020 Christian Meißner
2+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
4+
from __future__ import absolute_import, division, print_function
5+
__metaclass__ = type
6+
7+
import yaml
8+
9+
from jinja2 import Template
10+
from yaml.loader import SafeLoader
11+
12+
13+
def parse_config(entity, config):
14+
"""Loads hieradata.yml and parse its content
15+
16+
:param entity: the entity for what the configuration will be parsed
17+
:type entity: str
18+
:param parse: the type of entity we want to parse the configuration, defaults to "both"
19+
:type parse: str, optional
20+
:return: list of paths which reflects the hierarchy
21+
:rtype: list
22+
"""
23+
with open(config) as fd:
24+
fd_data = yaml.load(fd, Loader=SafeLoader)
25+
26+
hiera_vars = {}
27+
for k, v in fd_data['hiera_vars'].items():
28+
t = Template(v)
29+
hiera_vars[k] = t.render(entity=entity)
30+
31+
hierarchy = []
32+
for i, entry in enumerate(fd_data['hierarchy']):
33+
t = Template(entry)
34+
hierarchy.insert(i, t.render(hiera_vars))
35+
36+
return hierarchy

plugins/vars/hieradata.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,13 @@
111111
'''
112112

113113
import os
114-
import yaml
115-
116-
from jinja2 import Template
117-
from yaml.loader import SafeLoader
118114

119115
from ansible.errors import AnsibleParserError
120116
from ansible.module_utils._text import to_bytes, to_native, to_text
121117
from ansible.plugins.vars import BaseVarsPlugin
122118
from ansible.inventory.host import Host
123119
from ansible_collections.codeaffen.hieradata.plugins.module_utils.vars import combine_vars
120+
from ansible_collections.codeaffen.hieradata.plugins.module_utils.hieradata import parse_config
124121

125122
FOUND = {}
126123

@@ -185,29 +182,3 @@ def get_vars(self, loader, path, entities, cache=True):
185182
raise AnsibleParserError(to_native(e))
186183

187184
return hieradata
188-
189-
190-
def parse_config(entity, config):
191-
"""Loads hieradata.yml and parse its content
192-
193-
:param entity: the entity for what the configuration will be parsed
194-
:type entity: str
195-
:param parse: the type of entity we want to parse the configuration, defaults to "both"
196-
:type parse: str, optional
197-
:return: list of paths which reflects the hierarchy
198-
:rtype: list
199-
"""
200-
with open(config) as fd:
201-
fd_data = yaml.load(fd, Loader=SafeLoader)
202-
203-
hiera_vars = {}
204-
for k, v in fd_data['hiera_vars'].items():
205-
t = Template(v)
206-
hiera_vars[k] = t.render(entity=entity)
207-
208-
hierarchy = []
209-
for i, entry in enumerate(fd_data['hierarchy']):
210-
t = Template(entry)
211-
hierarchy.insert(i, t.render(hiera_vars))
212-
213-
return hierarchy

0 commit comments

Comments
 (0)