Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions ansible_bender/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import sys
import tempfile
from pathlib import Path
import uuid

import jsonschema
import yaml
Expand Down Expand Up @@ -173,6 +174,30 @@ def _get_path_our_site(self):
# hence, let's add the site ab is installed in to sys.path
return os.path.dirname(os.path.dirname(ansible_bender.__file__))

def _correct_host_entries(self, playbook_path, tmpDir):
""" Correct the host entries in the playbook and all imported playbooks """
tmp_pb_path = os.path.join(tmpDir, "ab_" + str(uuid.uuid4()) + ".yaml")

with open(playbook_path, "r") as fd_r:
pb_dict = yaml.safe_load(fd_r)

for idx, doc in enumerate(pb_dict):
imported_playbook = doc.get("import_playbook")
if imported_playbook:
import_base_path = os.path.dirname(playbook_path)
imported_playbook_path = os.path.join(import_base_path, imported_playbook)
logger.debug("Encountered import_playbook, correcting hosts entries in imported file: %s", imported_playbook_path)
doc["import_playbook"] = self._correct_host_entries(imported_playbook_path, tmpDir)
else:
host = doc["hosts"]
logger.debug("play[%s], host = %s", idx, host)
doc["hosts"] = self.builder.ansible_host

with open(tmp_pb_path, "w") as fd:
yaml.safe_dump(pb_dict, fd)

return tmp_pb_path

def build(self, db_path):
"""
run the playbook against the container
Expand Down Expand Up @@ -204,15 +229,7 @@ def build(self, db_path):
with open(a_cfg_path, "w") as fd:
self._create_ansible_cfg(fd)

tmp_pb_path = os.path.join(tmp, "p.yaml")
with open(self.pb, "r") as fd_r:
pb_dict = yaml.safe_load(fd_r)
for idx, doc in enumerate(pb_dict):
host = doc["hosts"]
logger.debug("play[%s], host = %s", idx, host)
doc["hosts"] = self.builder.ansible_host
with open(tmp_pb_path, "w") as fd:
yaml.safe_dump(pb_dict, fd)
tmp_pb_path = self._correct_host_entries(self.pb, tmp)
playbook_base = os.path.basename(self.pb).split(".", 1)[0]
timestamp = datetime.datetime.now().strftime(TIMESTAMP_FORMAT)
symlink_name = f".{playbook_base}-{timestamp}-{random_str()}.yaml"
Expand Down
6 changes: 6 additions & 0 deletions tests/data/import_playbook_basic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- import_playbook: imported_playbook.yaml
vars:
ansible_bender:
base_image: python:3.5-stretch
target_image:
name: test_img
13 changes: 13 additions & 0 deletions tests/data/import_playbook_recursive.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# doing a few different tasks including importing a playbook
- import_playbook: imported_playbook_upper.yaml
vars:
ansible_bender:
base_image: python:3.5-stretch
target_image:
name: test_img
- hosts: all
tasks:
- name: create a file
copy:
src: '{{ playbook_dir }}/a_bag_of_fun'
dest: /fun
4 changes: 4 additions & 0 deletions tests/data/imported_playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- hosts: all
tasks:
- debug:
msg: hi
9 changes: 9 additions & 0 deletions tests/data/imported_playbook_upper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- hosts: all
tasks:
- debug:
msg: hello there
- import_playbook: imported_playbook.yaml
- hosts: all
tasks:
- debug:
msg: more hello
16 changes: 15 additions & 1 deletion tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ansible_bender.utils import random_str, run_cmd
from tests.spellbook import (dont_cache_playbook_path, change_layering_playbook, data_dir,
dont_cache_playbook_path_pre, non_ex_pb, multiplay_path, role_pb_path, roles_dir)
from ..spellbook import small_basic_playbook_path
from ..spellbook import small_basic_playbook_path, import_playbook_basic, import_playbook_recursive


def test_build_db_metadata(application, build):
Expand Down Expand Up @@ -327,3 +327,17 @@ def test_cache_python_interpreter(application, build, image_name, interpreter):
build.python_interpreter = None
flexmock(application.db).should_call("record_python_interpreter").never()
application.build(build)


# test a few playbooks that uses import_playbook to verify that it works without exceptions/errors.
# Also checks that the correct number of layers are created as a minimum verification (based upon the instruction from all imported playbooks)
@pytest.mark.parametrize("playbook,num_layers", [
(import_playbook_basic, 2),
(import_playbook_recursive, 5)
])
def test_import_playbook_basic(application, build, playbook, num_layers):
build.playbook_path = playbook
application.build(build)

build = application.db.get_build(build.build_id)
assert len(build.layers) == num_layers
2 changes: 2 additions & 0 deletions tests/spellbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
role_pb_path = os.path.join(data_dir, "role.yaml")
playbook_with_unknown_keys = os.path.join(data_dir, "playbook_with_unknown_keys.yaml")
playbook_wrong_type = os.path.join(data_dir, "pb_wrong_type.yaml")
import_playbook_basic = os.path.join(data_dir, "import_playbook_basic.yaml")
import_playbook_recursive = os.path.join(data_dir, "import_playbook_recursive.yaml")

base_image = "docker.io/library/python:3-alpine"

Expand Down