Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial import_playbook support #268

Merged
merged 2 commits into from
Apr 1, 2022

Conversation

themkat
Copy link
Contributor

@themkat themkat commented Mar 30, 2022

Basics

Ignores adjusting hosts for import_playbooks statements, as those should not have a host entry. Instead we have to read the imported playbook and correct the possible hosts entries there (this goes on recursively by need, so can support a very big import_playbook-depth). The hosts-corrected imported playbook path/name will need to be used in place of the old one in the playbook that imports it. I decided on a uuid based filename for the temp files, because of the low likelihood of "name-crashes". Currently the temp-file was named p.yaml, and if we have several ones that would off course not work. This PR implements those fixes.

tl;dr: fixes #109

About the testing

I initially planned to do everything in tests_core.py, but after some thinking decided to create e2e'ish test in tests_api.py instead. This is because the tests in core would have needed either a lot of setup (duplication of api.py-code maybe), or mocking (in which I think it wouldn't be as useful). Hopefully this is not an issue, as I think the tests I made still verifies the functionality in an ok way.

The tests tests the example from #109, as well as a recursive one (import_playbook inside an imported playbook). The last one might be overkills. I have also tested manually, and it seems to work as expected in the tests I've done so far 🙂

Copy link
Collaborator

@TomasTomecek TomasTomecek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, very nice work!

just a few nits and we're good to merge

@@ -173,6 +174,29 @@ 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, str(uuid.uuid4()) + ".yaml")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love using the uuid here! could we please prefix the uuid string with e.g. ab_ so that people know the file is coming from ansible-bender?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2abc606 🙂

Comment on lines 187 to 189
import_base_path = os.path.dirname(playbook_path)
imported_playbook_path = os.path.join(import_base_path, imported_playbook)
doc["import_playbook"] = self._correct_host_entries(imported_playbook_path, tmpDir)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would some logger.debug make sense here as well? especially when it's called recursively

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a simple one in 2abc606 🙂 Does it look okay?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, looks fine!

@TomasTomecek
Copy link
Collaborator

merging, thank you so much for taking time to improve bender!

I'll try to fix some of the test runs in a separate PR so we can get green CI

@TomasTomecek TomasTomecek merged commit 4d022e3 into ansible-community:master Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support import_playbook
2 participants