Use pyyaml to output group vars

Because the ansible group vars can be more complex structures than
strings and ints, we should use yaml to output the groupvars files. This
will allow list or dict values to be correctly represented in the
ansible vars files.

Change-Id: I79b3e6a5e7acb5f95d65034a2908a005d9a343db
Closes-Bug: #1801162
This commit is contained in:
Alex Schultz 2018-11-01 15:42:11 -06:00
parent 1ee3c59dc7
commit 279543d7c9
5 changed files with 12 additions and 17 deletions

View File

@ -1,3 +0,0 @@
{% for k,v in role_group_vars.items() %}
{{ k }}: {{ v }}
{% endfor %}

View File

@ -1,2 +1,2 @@
max_fail_percentage: 15
any_errors_fatal: yes
any_errors_fatal: True

View File

@ -1,2 +1,2 @@
max_fail_percentage: 15
any_errors_fatal: yes
any_errors_fatal: True

View File

@ -279,10 +279,10 @@ class TestConfig(base.TestCase):
{'output_key': 'RoleGroupVars',
'output_value': {
'Controller': {
'any_errors_fatal': 'yes',
'any_errors_fatal': True,
'max_fail_percentage': 15},
'Compute': {
'any_errors_fatal': 'yes',
'any_errors_fatal': True,
'max_fail_percentage': 15},
}}]
deployment_data, configs = \
@ -615,10 +615,10 @@ class TestConfig(base.TestCase):
{'output_key': 'RoleGroupVars',
'output_value': {
'Controller': {
'any_errors_fatal': 'yes',
'any_errors_fatal': True,
'max_fail_percentage': 15},
'Compute': {
'any_errors_fatal': 'yes',
'any_errors_fatal': True,
'max_fail_percentage': 15},
}}]
deployment_data, configs = \

View File

@ -441,14 +441,12 @@ class Config(object):
# Render group_vars
for role in set(server_roles.values()):
group_var_role_path = os.path.join(group_vars_dir, role)
group_var_role_template = env.get_template('group_var_role.j2')
with open(group_var_role_path, 'w') as f:
template_data = group_var_role_template.render(
role=role,
role_group_vars=role_group_vars[role])
self.validate_config(template_data, group_var_role_path)
f.write(template_data)
# NOTE(aschultz): we just use yaml.safe_dump for the vars because
# the vars should already bein a hash for for ansible.
# See LP#1801162 for previous issues around using jinja for this
with open(group_var_role_path, 'w') as group_vars_file:
yaml.safe_dump(role_group_vars[role], group_vars_file,
default_flow_style=False)
# Render host_vars
for server, deployments in server_deployment_names.items():