diff --git a/molecule/constants.py b/molecule/constants.py index e35fe4abe..1ca2f1040 100644 --- a/molecule/constants.py +++ b/molecule/constants.py @@ -3,3 +3,5 @@ RC_SUCCESS = 0 RC_TIMEOUT = 3 + +MOLECULE_HEADER = "# Molecule managed" diff --git a/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml b/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml index d2ac73f38..b1efcfca6 100644 --- a/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml +++ b/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml @@ -28,6 +28,9 @@ - name: Dump instance config copy: - content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + content: | + # Molecule managed + + {{ instance_conf | to_json | from_json | to_yaml }} dest: "{{ molecule_instance_config }}" {%- endraw %} diff --git a/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/destroy.yml b/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/destroy.yml index 0f4467cb4..064546874 100644 --- a/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/destroy.yml +++ b/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/destroy.yml @@ -16,7 +16,10 @@ - name: Dump instance config copy: - content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + content: | + # Molecule managed + + {{ instance_conf | to_json | from_json | to_yaml }} dest: "{{ molecule_instance_config }}" when: server.changed | default(false) | bool {%- endraw %} diff --git a/molecule/provisioner/ansible.py b/molecule/provisioner/ansible.py index babc0b05a..c4dbc914d 100644 --- a/molecule/provisioner/ansible.py +++ b/molecule/provisioner/ansible.py @@ -596,7 +596,7 @@ def inventory(self): "molecule_file": "{{ lookup('env', 'MOLECULE_FILE') }}", "molecule_ephemeral_directory": "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}", "molecule_scenario_directory": "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}", - "molecule_yml": "{{ lookup('file', molecule_file) | molecule_from_yaml }}", + "molecule_yml": "{{ lookup('file', molecule_file) | from_yaml }}", "molecule_instance_config": "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}", "molecule_no_log": "{{ lookup('env', 'MOLECULE_NO_LOG') or not " "molecule_yml.provisioner.log|default(False) | bool }}", diff --git a/molecule/provisioner/ansible/plugins/filter/molecule_core.py b/molecule/provisioner/ansible/plugins/filter/molecule_core.py deleted file mode 100644 index c02c2323f..000000000 --- a/molecule/provisioner/ansible/plugins/filter/molecule_core.py +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright (c) 2015-2018 Cisco Systems, Inc. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -"""Provisioner Ansible Plugins.""" - -import os - -from molecule import config, interpolation, util - - -def from_yaml(data): - """ - Interpolate the provided data and return a dict. - - Currently, this is used to reinterpolate the `molecule.yml` inside an - Ansible playbook. If there were any interpolation errors, they would - have been found and raised earlier. - - :return: dict - """ - molecule_env_file = os.environ["MOLECULE_ENV_FILE"] - - env = os.environ.copy() - env = config.set_env_from_file(env, molecule_env_file) - - i = interpolation.Interpolator(interpolation.TemplateWithDefaults, env) - interpolated_data = i.interpolate(data) - - return util.safe_load(interpolated_data) - - -def to_yaml(data): - """Format data as YAML.""" - return str(util.safe_dump(data)) - - -def header(content): - """Return heaader to be added.""" - return util.molecule_prepender(content) - - -def get_docker_networks(data, state, labels={}): - """Get list of docker networks.""" - network_list = [] - network_names = [] - for platform in data: - if "docker_networks" in platform: - for docker_network in platform["docker_networks"]: - if "labels" not in docker_network: - docker_network["labels"] = {} - for key in labels: - docker_network["labels"][key] = labels[key] - - docker_network["state"] = state - - if "name" in docker_network: - network_list.append(docker_network) - network_names.append(docker_network["name"]) - - # If a network name is defined for a platform but is not defined in - # docker_networks, add it to the network list. - if "networks" in platform: - for network in platform["networks"]: - if "name" in network: - name = network["name"] - if name not in network_names: - network_list.append( - {"name": name, "labels": labels, "state": state} - ) - return network_list - - -class FilterModule(object): - """Core Molecule filter plugins.""" - - def filters(self): - return { - "molecule_from_yaml": from_yaml, - "molecule_to_yaml": to_yaml, - "molecule_header": header, - "molecule_get_docker_networks": get_docker_networks, - } diff --git a/molecule/test/unit/test_util.py b/molecule/test/unit/test_util.py index fa3daf86d..462ed4571 100644 --- a/molecule/test/unit/test_util.py +++ b/molecule/test/unit/test_util.py @@ -29,6 +29,7 @@ import sh from molecule import util +from molecule.constants import MOLECULE_HEADER colorama.init(autoreset=True) @@ -233,7 +234,7 @@ def test_write_file(temp_dir): def molecule_prepender(content): - x = "# Molecule managed\n\nfoo bar" + x = f"{MOLECULE_HEADER}\nfoo bar" assert x == util.file_prepender("foo bar") diff --git a/molecule/util.py b/molecule/util.py index 7821de90b..f8c4341ff 100644 --- a/molecule/util.py +++ b/molecule/util.py @@ -33,6 +33,7 @@ import jinja2 import yaml +from molecule.constants import MOLECULE_HEADER from molecule.logger import get_logger LOG = get_logger(__name__) @@ -164,7 +165,7 @@ def write_file(filename, content): def molecule_prepender(content): """Return molecule identification header.""" - return "# Molecule managed\n\n" + content + return MOLECULE_HEADER + "\n\n" + content def file_prepender(filename):