
It is sometimes useful to tell ConfigTemplateParser to write out options that are valueless and not suffixed with '=' or ':', such as when overriding a my.cnf. This commit adds the 'ignore_none_type' attribute to the config_template module. If this attribute is set to false, then valueless options will be written out to the resultant INI file as-is, without the '=' or ':' suffix. Change-Id: I5c88b2019c01b44193a5d0df9299ecce6de52f01 Partial-Bug: #1693234
168 lines
6.4 KiB
YAML
168 lines
6.4 KiB
YAML
---
|
|
# Copyright 2016, Comcast Corp.
|
|
#
|
|
# 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.
|
|
|
|
- name: Test config_template
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: no
|
|
tasks:
|
|
# 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"
|
|
|
|
- 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"
|
|
|
|
- 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
|
|
|
|
- name: Read test_extend.yml
|
|
slurp:
|
|
src: /tmp/test_extend.yml
|
|
register: extend_file
|
|
- name: Read expected test_extend.yml
|
|
slurp:
|
|
src: "{{ playbook_dir }}/files/test_extend.yml.expected"
|
|
register: extend_file_expected
|
|
- 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
|
|
- name: Read test_no_extend.yml
|
|
slurp:
|
|
src: /tmp/test_no_extend.yml
|
|
register: no_extend_file
|
|
- name: Read expected test_no_extend.yml
|
|
slurp:
|
|
src: "{{ playbook_dir }}/files/test_no_extend.yml.expected"
|
|
register: no_extend_file_expected
|
|
- 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 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 }}"
|
|
- name: Read test_content_no_overrides.json
|
|
slurp:
|
|
src: /tmp/test_content_no_overrides.json
|
|
register: content_no_overrides_file
|
|
- name: Read expected test_content_no_overrides.json
|
|
slurp:
|
|
src: "{{ playbook_dir }}/files/test_content_no_overrides.json.expected"
|
|
register: content_no_overrides_file_expected
|
|
- 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
|
|
- 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$') }}"
|
|
|
|
vars:
|
|
test_config_ini_overrides:
|
|
DEFAULT:
|
|
new_key: "new_value"
|
|
foo:
|
|
baz: "bar"
|
|
test_config_yml_overrides:
|
|
list_one:
|
|
- four
|