8
8
# OF ANY KIND, either express or implied. See the License for the specific language
9
9
# governing permissions and limitations under the License.
10
10
11
- import collections
12
11
import os
13
-
14
12
import yaml
15
13
16
14
from ansible .module_utils .common .collections import ImmutableDict
17
15
from ansible .parsing .dataloader import DataLoader
18
- from ansible .plugins .loader import PluginLoader
19
16
from ansible .template import Templar
20
- from ansible .utils .vars import load_extra_vars
17
+ from ansible .utils .vars import combine_vars
21
18
from ansible .vars .manager import VariableManager
22
19
from ops .cli import display
23
20
from ansible import constants as C
24
21
from ansible import context
25
22
import logging
23
+ from ansible .errors import AnsibleOptionsError
24
+ from ansible .module_utils ._text import to_text
25
+ from ansible .parsing .splitter import parse_kv
26
+ from collections .abc import MutableMapping
26
27
27
28
logger = logging .getLogger (__name__ )
28
29
@@ -34,6 +35,38 @@ def get_cluster_name(cluster_config_path):
34
35
'/' )[- 1 ].replace ('.yaml' , '' ).replace ('.yml' , '' )
35
36
36
37
38
+ def load_extra_vars (loader ):
39
+ """
40
+ Overriding Ansible function using version before slight var loading optimization
41
+ in order to avoid caching issues https://github.com/ansible/ansible/pull/78835/files
42
+ """
43
+
44
+ extra_vars = {}
45
+ for extra_vars_opt in context .CLIARGS .get ('extra_vars' , tuple ()):
46
+ data = None
47
+ extra_vars_opt = to_text (extra_vars_opt , errors = 'surrogate_or_strict' )
48
+ if extra_vars_opt is None or not extra_vars_opt :
49
+ continue
50
+
51
+ if extra_vars_opt .startswith (u"@" ):
52
+ # Argument is a YAML file (JSON is a subset of YAML)
53
+ data = loader .load_from_file (extra_vars_opt [1 :])
54
+ elif extra_vars_opt [0 ] in [u'/' , u'.' ]:
55
+ raise AnsibleOptionsError ("Please prepend extra_vars filename '%s' with '@'" % extra_vars_opt )
56
+ elif extra_vars_opt [0 ] in [u'[' , u'{' ]:
57
+ # Arguments as YAML
58
+ data = loader .load (extra_vars_opt )
59
+ else :
60
+ # Arguments as Key-value
61
+ data = parse_kv (extra_vars_opt )
62
+
63
+ if isinstance (data , MutableMapping ):
64
+ extra_vars = combine_vars (extra_vars , data )
65
+ else :
66
+ raise AnsibleOptionsError ("Invalid extra vars data supplied. '%s' could not be made into a dictionary" % extra_vars_opt )
67
+ return extra_vars
68
+
69
+
37
70
class ClusterConfig (object ):
38
71
def __init__ (self , cluster_config_generator ,
39
72
ops_config , cluster_config_path ):
@@ -119,7 +152,6 @@ def get(self):
119
152
context_cliargs ['extra_vars' ] = tuple (extra_vars )
120
153
121
154
context .CLIARGS = ImmutableDict (context_cliargs )
122
- setattr (load_extra_vars , 'extra_vars' , {})
123
155
variable_manager ._extra_vars = load_extra_vars (
124
156
loader = data_loader )
125
157
@@ -159,7 +191,6 @@ def get(self):
159
191
context_cliargs ['extra_vars' ] = tuple (extra_vars )
160
192
161
193
context .CLIARGS = ImmutableDict (context_cliargs )
162
- setattr (load_extra_vars , 'extra_vars' , {})
163
194
variable_manager ._extra_vars = load_extra_vars (
164
195
loader = data_loader )
165
196
0 commit comments