--- # Copyright 2018, Rackspace US # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Test basic function of config_template - name: Template test INI template config_template: src: "{{ playbook_dir }}/templates/test.ini" dest: "/tmp/test.ini" config_overrides: "{{ test_config_ini_overrides }}" config_type: "ini" register: test_ini notify: test_ini check diff - name: Read test.ini slurp: src: /tmp/test.ini register: ini_file - debug: msg: "ini - {{ ini_file.content | b64decode }}" - name: Validate output assert: that: - "(lookup('ini', 'new_key section=DEFAULT file=/tmp/test.ini')) == 'new_value'" - "(lookup('ini', 'baz section=foo file=/tmp/test.ini')) == 'bar'" # Test basic function of config_template with content instead of src - name: Template test INI template config_template: content: "{{ lookup('file', playbook_dir + '/templates/test.ini') }}" dest: "/tmp/test_with_content.ini" config_overrides: "{{ test_config_ini_overrides }}" config_type: "ini" register: test_with_content_ini notify: test_with_content_ini check diff - name: Read test.ini slurp: src: /tmp/test_with_content.ini register: ini_file_with_content - debug: msg: "ini - {{ ini_file_with_content.content | b64decode }}" - name: Validate output assert: that: - "(lookup('ini', 'new_key section=DEFAULT file=/tmp/test_with_content.ini')) == 'new_value'" - "(lookup('ini', 'baz section=foo file=/tmp/test_with_content.ini')) == 'bar'" # Test list additions in config_template - name: Template test YML template config_template: src: "{{ playbook_dir }}/templates/test.yml" dest: "/tmp/test_extend.yml" config_overrides: "{{ test_config_yml_overrides }}" config_type: "yaml" list_extend: True register: test_extend_yml notify: test_extend_yml check diff - name: Read test_extend.yml slurp: src: /tmp/test_extend.yml register: extend_file - debug: msg: "extend - {{ extend_file.content | b64decode }}" - debug: msg: "extend.expected - {{ extend_file_expected.content | b64decode }}" - name: Compare files assert: that: - "(extend_file.content | b64decode) == (extend_file_expected.content | b64decode)" # Test list replacement in config_template - name: Template test YML template config_template: src: "{{ playbook_dir }}/templates/test.yml" dest: "/tmp/test_no_extend.yml" config_overrides: "{{ test_config_yml_overrides }}" config_type: "yaml" list_extend: False register: test_no_extend_yml notify: test_no_extend_yml check diff - name: Read test_no_extend.yml slurp: src: /tmp/test_no_extend.yml register: no_extend_file - debug: msg: "no_extend - {{ no_extend_file.content | b64decode }}" - debug: msg: "no_extend.expected - {{ no_extend_file_expected.content | b64decode }}" - name: Compare files assert: 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" register: test_hostvars_yml notify: test_hostvars_yml check diff - 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: dest: "/tmp/test_content_no_overrides.json" config_overrides: {} config_type: "json" content: "{{ lookup('file', playbook_dir ~ '/templates/test.json') | from_json }}" register: test_content_no_overrides_json notify: test_content_no_overrides_json check diff - name: Read test_content_no_overrides.json slurp: src: /tmp/test_content_no_overrides.json register: content_no_overrides_file - debug: msg: "content_no_overrides.json - {{ content_no_overrides_file.content | b64decode | from_json }}" - debug: msg: "content_no_overrides.json.expected - {{ content_no_overrides_file_expected.content | b64decode | from_json }}" # NOTE (alextricity25): The config_template module doesn't use ordered dicts when reading and writing json # data, so we can't guarantee that the string literal of both file's content will be the same. Instead, we compare # the content after transforming it into a dictionary. - name: Compare file content assert: that: - "(content_no_overrides_file.content | b64decode | from_json) == (content_no_overrides_file_expected.content | b64decode | from_json)" # Test the ignore_none_type attribute when set to False - name: Template test with ignore_none_type set to false config_template: src: "{{ playbook_dir }}/templates/test_ignore_none_type.ini" dest: "/tmp/test_ignore_none_type.ini" config_overrides: "{{ test_config_ini_overrides }}" config_type: "ini" ignore_none_type: False register: test_ignore_none_type_ini notify: test_ignore_none_type_ini check diff - name: Read test_ignore_none_type.ini slurp: src: /tmp/test_ignore_none_type.ini register: test_ignore_none_type - debug: msg: "test_ignore_none_type.ini - {{ test_ignore_none_type.content | b64decode }}" - name: Validate output has valueless options printed out assert: that: - "{{ test_ignore_none_type.content | b64decode | search('(?m)^india$') }}" - "{{ test_ignore_none_type.content | b64decode | search('(?m)^juliett kilo$') }}" # Test basic function of config_template - name: Template test INI comments config_template: src: "{{ playbook_dir }}/templates/test_with_comments.ini" dest: "/tmp/test_with_comments.ini" config_overrides: "{{ test_config_ini_overrides }}" config_type: "ini" tags: test register: test_with_comments_ini notify: test_with_comments_ini check diff - name: Read test.ini slurp: src: /tmp/test_with_comments.ini register: ini_file tags: test - debug: msg: "ini - {{ ini_file.content | b64decode }}" - name: Validate output tags: test assert: that: - "(lookup('ini', 'new_key section=DEFAULT file=/tmp/test_with_comments.ini')) == 'new_value'" - "(lookup('ini', 'baz section=foo file=/tmp/test_with_comments.ini')) == 'bar'" - "{{ ini_file.content | b64decode | search('#This is a comment')}}" - "{{ ini_file.content | b64decode | search('# A default section comment\n# broken into multiple lines\n\\[DEFAULT\\]')}}" - name: Template multiple times to assert no changes config_template: src: "{{ playbook_dir }}/templates/test_with_comments.ini" dest: "/tmp/test_with_comments.ini" config_type: "ini" config_overrides: "{{ item[1] }}" register: template_changed failed_when: template_changed is changed with_nested: - [ 0, 1, 2 ] - [ "{{ test_config_ini_overrides }}" ] - name: Put down default_section_expected file copy: src: "{{ playbook_dir }}/files/test_default_section.ini.expected" dest: "/tmp/test_default_section.ini" - name: Template using default_section config_template: src: "{{ playbook_dir }}/templates/test_default_section.ini" dest: "/tmp/test_default_section.ini" config_type: "ini" config_overrides: "{{ test_default_section_overrides }}" default_section: "global" register: template_changed failed_when: template_changed is changed - name: Write ini for testing diff output config_template: src: "{{ playbook_dir }}/templates/test_diff.ini" dest: "/tmp/test_diff.ini" config_type: "ini" config_overrides: {} - name: Test ini with additions and changed config_template: src: "{{ playbook_dir }}/templates/test_diff.ini" dest: "/tmp/test_diff.ini" config_type: "ini" config_overrides: "{{ test_diff_overrides }}" register: test_diff_ini notify: test_diff_ini check diff - name: Test ini with removes config_template: src: "{{ playbook_dir }}/templates/test_diff_remove.ini" dest: "/tmp/test_diff.ini" config_type: "ini" config_overrides: "{{ test_diff_overrides }}" register: test_diff_remove_ini notify: test_diff_remove_ini check diff