Skip to content

Commit

Permalink
Load molecule.yml through our own from_yaml loader
Browse files Browse the repository at this point in the history
This allows us to run the interpolator before parsing the molecule
file.

Fixes: #887
  • Loading branch information
retr0h committed Jul 7, 2017
1 parent 53b92a1 commit ab81970
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 21 deletions.
13 changes: 12 additions & 1 deletion molecule/provisioner/ansible/plugins/filters/molecule_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from molecule import config
import os

from molecule import interpolation
from molecule import util


def from_yaml(data):
i = interpolation.Interpolator(interpolation.TemplateWithDefaults,
os.environ)
interpolated_data = i.interpolate(data)

return util.safe_load(interpolated_data)


def to_yaml(data):
return str(util.safe_dump(data))

Expand All @@ -35,6 +45,7 @@ class FilterModule(object):

def filters(self):
return {
'molecule_from_yaml': from_yaml,
'molecule_to_yaml': to_yaml,
'molecule_header': header,
}
5 changes: 4 additions & 1 deletion test/functional/docker/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ def test_interpolation(scenario_to_test, with_scenario, scenario_name):
'all': True,
}
env = os.environ
env.update({'DRIVER_NAME': 'docker'})
env.update({
'DRIVER_NAME': 'docker',
'INSTANCE_NAME': 'instance',
})

cmd = sh.molecule.bake('test', **options)
pytest.helpers.run_command(cmd, env=env)
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/docker/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Create Dockerfiles from image names
template:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/docker/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
docker_container:
Expand Down
4 changes: 2 additions & 2 deletions test/resources/playbooks/ec2/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"

ssh_port: 22

Expand Down Expand Up @@ -77,4 +77,4 @@
host: "{{ item.address }}"
search_regex: SSH
delay: 10
with_items: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
4 changes: 2 additions & 2 deletions test/resources/playbooks/ec2/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- block:
- name: Populate instance config
set_fact:
instance_conf: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
skip_instances: False
rescue:
- name: Populate instance config when file missing
Expand Down
4 changes: 2 additions & 2 deletions test/resources/playbooks/gce/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"

ssh_port: 22
ssh_user: "{{ lookup('env', 'USER') }}"
Expand Down Expand Up @@ -58,4 +58,4 @@
host: "{{ item.address }}"
search_regex: SSH
delay: 10
with_items: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
2 changes: 1 addition & 1 deletion test/resources/playbooks/gce/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
gce:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/lxc/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Create molecule instance(s)
lxc_container:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/lxc/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
lxc_container:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/lxd/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Create molecule instance(s)
lxd_container:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/lxd/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
lxd_container:
Expand Down
4 changes: 2 additions & 2 deletions test/resources/playbooks/openstack/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"

ssh_port: 22

Expand Down Expand Up @@ -96,4 +96,4 @@
host: "{{ item.address }}"
search_regex: SSH
delay: 10
with_items: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
2 changes: 1 addition & 1 deletion test/resources/playbooks/openstack/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
os_server:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/vagrant/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Create molecule instance(s)
molecule_vagrant:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/playbooks/vagrant/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
molecule_vagrant:
Expand Down
2 changes: 1 addition & 1 deletion test/scenarios/interpolation/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ lint:
options:
config-file: ../../resources/.yamllint
platforms:
- name: instance
- name: $INSTANCE_NAME
image: centos:latest
provisioner:
name: ansible
Expand Down

0 comments on commit ab81970

Please sign in to comment.