88# OF ANY KIND, either express or implied. See the License for the specific language
99# governing permissions and limitations under the License.
1010
11- import collections
1211import os
13-
1412import yaml
1513
1614from ansible .module_utils .common .collections import ImmutableDict
1715from ansible .parsing .dataloader import DataLoader
18- from ansible .plugins .loader import PluginLoader
1916from ansible .template import Templar
20- from ansible .utils .vars import load_extra_vars
17+ from ansible .utils .vars import combine_vars
2118from ansible .vars .manager import VariableManager
2219from ops .cli import display
2320from ansible import constants as C
2421from ansible import context
2522import 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
2627
2728logger = logging .getLogger (__name__ )
2829
@@ -34,6 +35,38 @@ def get_cluster_name(cluster_config_path):
3435 '/' )[- 1 ].replace ('.yaml' , '' ).replace ('.yml' , '' )
3536
3637
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+
3770class ClusterConfig (object ):
3871 def __init__ (self , cluster_config_generator ,
3972 ops_config , cluster_config_path ):
@@ -119,7 +152,6 @@ def get(self):
119152 context_cliargs ['extra_vars' ] = tuple (extra_vars )
120153
121154 context .CLIARGS = ImmutableDict (context_cliargs )
122- setattr (load_extra_vars , 'extra_vars' , {})
123155 variable_manager ._extra_vars = load_extra_vars (
124156 loader = data_loader )
125157
@@ -159,7 +191,6 @@ def get(self):
159191 context_cliargs ['extra_vars' ] = tuple (extra_vars )
160192
161193 context .CLIARGS = ImmutableDict (context_cliargs )
162- setattr (load_extra_vars , 'extra_vars' , {})
163194 variable_manager ._extra_vars = load_extra_vars (
164195 loader = data_loader )
165196
0 commit comments