From 6d496d6c5bdeb47be0e56bf06676755da2db2aea Mon Sep 17 00:00:00 2001 From: Logan V Date: Tue, 1 Aug 2017 11:36:46 -0500 Subject: [PATCH] Fix dumping hostvars to yaml from overrides Using AnsibleDumper adds support for dumping the internal ansible data types that are used in hostvars. Fixes-Bug: #1707265 Change-Id: Ie59bbef31306cd3e99aa827db875922e17084e83 --- action/_v2_config_template.py | 3 ++- tests/test-config_template.yml | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/action/_v2_config_template.py b/action/_v2_config_template.py index 0cc6198..f4c8b7e 100644 --- a/action/_v2_config_template.py +++ b/action/_v2_config_template.py @@ -38,6 +38,7 @@ from ansible.plugins.action import ActionBase from ansible.module_utils._text import to_bytes, to_text from ansible import constants as C from ansible import errors +from ansible.parsing.yaml.dumper import AnsibleDumper CONFIG_TYPES = { @@ -73,7 +74,7 @@ def _convert_2_string(item): return str(item) -class IDumper(yaml.SafeDumper): +class IDumper(AnsibleDumper): def increase_indent(self, flow=False, indentless=False): return super(IDumper, self).increase_indent(flow, False) diff --git a/tests/test-config_template.yml b/tests/test-config_template.yml index 450b3d6..9575765 100644 --- a/tests/test-config_template.yml +++ b/tests/test-config_template.yml @@ -16,7 +16,7 @@ - name: Test config_template hosts: localhost connection: local - gather_facts: no + gather_facts: yes tasks: # Test basic function of config_template - name: Template test INI template @@ -109,6 +109,27 @@ that: - "(no_extend_file.content | b64decode) == (no_extend_file_expected.content | b64decode)" + # Test dumping hostvars using config overrides + - name: Template test YML template with hostvars override + config_template: + src: "{{ playbook_dir }}/templates/test.yml" + dest: "/tmp/test_hostvars.yml" + config_overrides: "{{ test_config_yml_hostvars_overrides }}" + config_type: "yaml" + - name: Read test_hostvars.yml + slurp: + src: /tmp/test_hostvars.yml + register: hostvars_file + - debug: + msg: "hostvars - {{ (hostvars_file.content | b64decode | from_yaml).test_hostvar }}" + - debug: + msg: "hostvars.expected - {{ test_config_yml_hostvars_overrides.test_hostvar }}" + - name: Compare files + assert: + that: + - "((hostvars_file.content | b64decode | from_yaml).test_hostvar) == (test_config_yml_hostvars_overrides.test_hostvar)" + + # Test content attribute with a dictionary input and config_type equal to 'json' - name: Template test JSON template with content attribute config_template: @@ -165,3 +186,5 @@ test_config_yml_overrides: list_one: - four + test_config_yml_hostvars_overrides: + test_hostvar: "{{ ansible_default_ipv4.address }}"