Skip to content
This repository was archived by the owner on Jan 1, 2024. It is now read-only.

Commit e9007a0

Browse files
authored
Fix facts setting in hostvars fact (#278)
Before this patch, complex variables (lists, dictionaries) were not fully expanded on old Ansible versions (2.8 or earlier). As a result, it was impossible to use other variables (e.g. `{{ common_memtx }}` value) in dictionaries and lists in `hosts.yml`. We are now using the standard `set_fact` mechanism, which solves this problem.
1 parent 4dbe3dc commit e9007a0

20 files changed

+201
-136
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Please update `ansible-galaxy install` command in
1010
README.md to use the newest tag with new release
1111
-->
1212

13+
### Fixed
14+
15+
- Fix facts setting in `hostvars` fact
16+
1317
## [1.8.1] - 2021-03-31
1418

1519
### Fixed

doc/scenario.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ Some of useful facts are established during preparation, so you can use them at
281281
for example, in `delegate_to`;
282282
- `scenario_steps` - description of scenario steps. Each step is a dictionary with fields:
283283
- `name` - name of step;
284-
- `path` - path to YAML file of step;
285-
- `role_vars` - dictionary with all role variables.
284+
- `path` - path to YAML file of step.
286285

287286
## Role Steps Description
288287

library/cartridge_connect_to_membership.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ def connect_to_membership(params):
2727
changed = False
2828

2929
for instance_name, instance_vars in hostvars.items():
30-
if 'role_vars' in instance_vars:
31-
instance_vars = instance_vars['role_vars']
32-
3330
if helpers.is_expelled(instance_vars) or helpers.is_stateboard(instance_vars):
3431
continue
3532

library/cartridge_edit_topology.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def get_configured_replicasets(hostvars, play_hosts):
116116
replicasets = {}
117117

118118
for instance_name in play_hosts:
119-
instance_vars = hostvars[instance_name]['role_vars']
119+
instance_vars = hostvars[instance_name]
120120

121121
if helpers.is_expelled(instance_vars) or helpers.is_stateboard(instance_vars):
122122
continue
@@ -144,7 +144,7 @@ def get_instances_to_configure(hostvars, play_hosts):
144144
instances = {}
145145

146146
for instance_name in play_hosts:
147-
instance_vars = hostvars[instance_name]['role_vars']
147+
instance_vars = hostvars[instance_name]
148148

149149
if helpers.is_stateboard(instance_vars):
150150
continue

library/cartridge_get_control_instance.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def get_control_instance(params):
105105

106106
if not control_instance_candidates:
107107
for instance_name in play_hosts:
108-
instance_vars = hostvars[instance_name]['role_vars']
108+
instance_vars = hostvars[instance_name]
109109

110110
if helpers.is_expelled(instance_vars) or helpers.is_stateboard(instance_vars):
111111
continue
@@ -118,8 +118,6 @@ def get_control_instance(params):
118118
return helpers.ModuleRes(failed=True, msg=errmsg)
119119

120120
advertise_uris = [
121-
hostvars[instance_name]['role_vars']['config']['advertise_uri']
122-
if 'role_vars' in hostvars[instance_name] else
123121
hostvars[instance_name]['config']['advertise_uri']
124122
for instance_name in control_instance_candidates
125123
]
@@ -135,8 +133,6 @@ def get_control_instance(params):
135133
# instance_vars['instance_info'], but if control instance is not
136134
# in play_hosts, instance_info isn't computed for it
137135
instance_vars = hostvars[control_instance_name]
138-
if 'role_vars' in instance_vars:
139-
instance_vars = instance_vars['role_vars']
140136
run_dir = instance_vars.get('cartridge_run_dir', helpers.DEFAULT_RUN_DIR)
141137
control_instance_console_sock = helpers.get_instance_console_sock(
142138
run_dir, app_name, control_instance_name,

library/cartridge_get_not_expelled_instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def get_one_not_expelled_instance(params):
1515
not_expelled_instance_name = None
1616

1717
for instance_name in play_hosts:
18-
instance_vars = hostvars[instance_name]['role_vars']
18+
instance_vars = hostvars[instance_name]
1919
if helpers.is_expelled(instance_vars) or helpers.is_stateboard(instance_vars):
2020
continue
2121

library/cartridge_get_single_instances_for_each_machine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_one_not_expelled_instance_for_machine(params):
2525
for instance_name in play_hosts:
2626
instance_vars = hostvars[instance_name]
2727

28-
if helpers.is_expelled(instance_vars['role_vars']):
28+
if helpers.is_expelled(instance_vars):
2929
continue
3030

3131
machine_hostname = get_machine_hostname(instance_vars, instance_name)

library/cartridge_validate_config.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,8 @@ def check_schema(schema, conf, path=''):
104104
return errmsg
105105
return None
106106
elif isinstance(schema, type):
107-
if schema in [float, int]:
108-
if not (isinstance(conf, float) or isinstance(conf, int)):
109-
return '{} must be {}'.format(path, schema)
110-
else:
111-
if not isinstance(conf, schema):
112-
return '{} must be {}'.format(path, schema)
107+
if not isinstance(conf, schema):
108+
return '{} must be {}'.format(path, schema)
113109
return None
114110
else:
115111
return 'Wrong type'
@@ -217,13 +213,13 @@ def check_cluster_cookie_symbols(cluster_cookie):
217213
return None
218214

219215

220-
def check_required_params(role_vars, host):
216+
def check_required_params(instance_vars, host):
221217
for p in INSTANCE_REQUIRED_PARAMS:
222-
if role_vars.get(p) is None:
218+
if instance_vars.get(p) is None:
223219
errmsg = '"{}" must be specified (missed for "{}")'.format(p, host)
224220
return errmsg
225221

226-
errmsg = check_cluster_cookie_symbols(role_vars['cartridge_cluster_cookie'])
222+
errmsg = check_cluster_cookie_symbols(instance_vars['cartridge_cluster_cookie'])
227223
if errmsg is not None:
228224
return errmsg
229225

@@ -251,29 +247,29 @@ def check_instance_config(config, host):
251247
return None
252248

253249

254-
def check_params_the_same_for_all_hosts(role_vars, found_common_params):
250+
def check_params_the_same_for_all_hosts(instance_vars, found_common_params):
255251
for p in PARAMS_THE_SAME_FOR_ALL_HOSTS:
256252
if found_common_params.get(p) is not None:
257-
if role_vars.get(p) != found_common_params.get(p):
253+
if instance_vars.get(p) != found_common_params.get(p):
258254
errmsg = '"{}" must be the same for all hosts'.format(p)
259255
return errmsg
260-
elif role_vars.get(p) is not None:
261-
found_common_params[p] = role_vars.get(p)
256+
elif instance_vars.get(p) is not None:
257+
found_common_params[p] = instance_vars.get(p)
262258

263259
return None
264260

265261

266-
def check_replicaset(role_vars, found_replicasets):
267-
if 'replicaset_alias' not in role_vars:
262+
def check_replicaset(instance_vars, found_replicasets):
263+
if 'replicaset_alias' not in instance_vars:
268264
return None
269265

270-
replicaset_alias = role_vars['replicaset_alias']
266+
replicaset_alias = instance_vars['replicaset_alias']
271267

272-
replicaset = {p: role_vars.get(p) for p in REPLICASET_PARAMS}
268+
replicaset = {p: instance_vars.get(p) for p in REPLICASET_PARAMS}
273269

274270
if replicaset_alias not in found_replicasets:
275271
for p in REPLICASET_REQUIRED_PARAMS:
276-
if p not in role_vars:
272+
if p not in instance_vars:
277273
errmsg = 'Parameter "{}" is required for all replicasets (missed for "{}")'.format(
278274
p, replicaset_alias
279275
)
@@ -483,53 +479,53 @@ def validate_config(params):
483479
warnings = []
484480

485481
for host in params['hosts']:
486-
role_vars = params['hostvars'][host]['role_vars']
482+
instance_vars = params['hostvars'][host]
487483

488484
# Validate types
489-
errmsg = validate_types(role_vars)
485+
errmsg = validate_types(instance_vars)
490486
if errmsg is not None:
491487
return helpers.ModuleRes(failed=True, msg=errmsg)
492488

493-
if role_vars.get('stateboard') is True:
494-
errmsg = check_stateboard(role_vars)
489+
if instance_vars.get('stateboard') is True:
490+
errmsg = check_stateboard(instance_vars)
495491
if errmsg is not None:
496492
return helpers.ModuleRes(failed=True, msg=errmsg)
497493
continue
498494

499495
# All required params should be specified
500-
errmsg = check_required_params(role_vars, host)
496+
errmsg = check_required_params(instance_vars, host)
501497
if errmsg is not None:
502498
return helpers.ModuleRes(failed=True, msg=errmsg)
503499

504500
# Instance config
505-
errmsg = check_instance_config(role_vars['config'], host)
501+
errmsg = check_instance_config(instance_vars['config'], host)
506502
if errmsg is not None:
507503
return helpers.ModuleRes(failed=True, msg=errmsg)
508504

509505
# Params common for all instances
510-
errmsg = check_params_the_same_for_all_hosts(role_vars, found_common_params)
506+
errmsg = check_params_the_same_for_all_hosts(instance_vars, found_common_params)
511507
if errmsg is not None:
512508
return helpers.ModuleRes(failed=True, msg=errmsg)
513509

514510
# Cartridge defaults
515-
if 'cartridge_defaults' in role_vars:
516-
if 'cluster_cookie' in role_vars['cartridge_defaults']:
511+
if 'cartridge_defaults' in instance_vars:
512+
if 'cluster_cookie' in instance_vars['cartridge_defaults']:
517513
errmsg = 'Cluster cookie must be specified in "cartridge_cluster_cookie", not in "cartridge_defaults"'
518514
return helpers.ModuleRes(failed=True, msg=errmsg)
519515

520516
# Instance state
521-
if role_vars.get('expelled') is True and role_vars.get('restarted') is True:
517+
if instance_vars.get('expelled') is True and instance_vars.get('restarted') is True:
522518
errmsg = 'Flags "expelled" and "restarted" cannot be set at the same time'
523519
return helpers.ModuleRes(failed=True, msg=errmsg)
524520

525521
# Replicasets
526-
errmsg = check_replicaset(role_vars, found_replicasets)
522+
errmsg = check_replicaset(instance_vars, found_replicasets)
527523
if errmsg is not None:
528524
return helpers.ModuleRes(failed=True, msg=errmsg)
529525

530526
# Dist retention
531-
if 'cartridge_keep_num_latest_dists' in role_vars:
532-
keep_num_latest_dists = role_vars['cartridge_keep_num_latest_dists']
527+
if 'cartridge_keep_num_latest_dists' in instance_vars:
528+
keep_num_latest_dists = instance_vars['cartridge_keep_num_latest_dists']
533529
if keep_num_latest_dists <= 0:
534530
errmsg = '"cartridge_keep_num_latest_dists" should be greater than 0'
535531
return helpers.ModuleRes(failed=True, msg=errmsg)

molecule/common/tests/test_cluster.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def test_services_status_and_config(host):
147147
instance_vars = variable_manager.get_vars(host=instance)
148148

149149
instance_conf = instance_vars['config']
150+
if instance_conf.get('memtx_memory') == '{{ common_memtx_memory }}':
151+
instance_conf['memtx_memory'] = 268436000
152+
150153
instance_name = instance_vars['inventory_hostname']
151154

152155
conf_dir = instance_vars.get('cartridge_conf_dir', '/etc/tarantool/conf.d')

molecule/default/hosts.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ all:
1212
# common cartridge opts
1313
cartridge_app_name: ['incorrect type to check playbook vars']
1414
cartridge_cluster_cookie: secret-cookie
15+
16+
common_memtx_memory: 268436000
1517
cartridge_defaults:
1618
some_option: 'default value'
1719

@@ -82,7 +84,7 @@ all:
8284
config:
8385
advertise_uri: 'vm2:3201'
8486
http_port: 8201
85-
memtx_memory: 268436000
87+
memtx_memory: '{{ common_memtx_memory }}'
8688
instance_start_timeout: 120
8789
instance_discover_buckets_timeout: 120
8890

0 commit comments

Comments
 (0)