Skip to content

Commit

Permalink
Final pass provisioner
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Nov 7, 2024
1 parent be2711f commit 65f4a2e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def write(self) -> None: # noqa: D102
@property
def ansible_collections_path(
self,
) -> Literal["ANSIBLE_COLLECTIONS_PATH", "ANSIBLE_COLLECTIONS_PATHS"]:
) -> str:
"""Return collection path variable for current version of Ansible."""
# https://github.com/ansible/ansible/pull/70007
if self.runtime.version >= Version("2.10.0.dev0"):
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/provisioner/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# D104 # noqa: D104, ERA001
# noqa: D104
49 changes: 28 additions & 21 deletions src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=too-many-lines
# Copyright (c) 2015-2018 Cisco Systems, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -529,10 +530,10 @@ def default_env(self) -> dict[str, str]:
list(map(util.abs_path, os.environ["ANSIBLE_ROLES_PATH"].split(":"))),
)

env = util.merge_dicts(
os.environ,
env = util.merge_dicts( # type: ignore[type-var]
dict(os.environ),
{
"ANSIBLE_CONFIG": self._config.provisioner.config_file,
"ANSIBLE_CONFIG": self.config_file,
"ANSIBLE_ROLES_PATH": ":".join(roles_path_list),
self._config.ansible_collections_path: ":".join(collections_path_list),
"ANSIBLE_LIBRARY": ":".join(self._get_modules_directories()),
Expand All @@ -541,7 +542,7 @@ def default_env(self) -> dict[str, str]:
),
},
)
env = util.merge_dicts(env, self._config.env)
env = util.merge_dicts(env, self._config.env) # type: ignore[type-var]

return env # noqa: RET504

Expand Down Expand Up @@ -736,9 +737,10 @@ def connection_options(

def check(self) -> None:
"""Execute ``ansible-playbook`` against the converge playbook with the ``--check`` flag."""
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("check", value=True)
pb.execute()
if self.playbooks.converge:
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("check", value=True)
pb.execute()

def converge(self, playbook: str = "", **kwargs: object) -> str:
"""Execute ``ansible-playbook`` against the converge playbook. unless specified otherwise.
Expand All @@ -756,50 +758,55 @@ def converge(self, playbook: str = "", **kwargs: object) -> str:

def destroy(self) -> None:
"""Execute ``ansible-playbook`` against the destroy playbook and returns None."""
pb = self._get_ansible_playbook(self.playbooks.destroy)
pb.execute()
if self.playbooks.destroy:
pb = self._get_ansible_playbook(self.playbooks.destroy)
pb.execute()

def side_effect(self, action_args: list[str] | None = None) -> None:
"""Execute ``ansible-playbook`` against the side_effect playbook.
Args:
action_args: Arguments to pass to the side_effect playbook.
"""
playbooks = []
if action_args:
playbooks = [
self._get_ansible_playbook(self._config.provisioner.abs_path(playbook))
for playbook in action_args
self._get_ansible_playbook(self.abs_path(playbook)) for playbook in action_args
]
else:
elif self.playbooks.side_effect:
playbooks = [self._get_ansible_playbook(self.playbooks.side_effect)]
for pb in playbooks:
pb.execute()

def create(self) -> None:
"""Execute ``ansible-playbook`` against the create playbook and returns None."""
pb = self._get_ansible_playbook(self.playbooks.create)
pb.execute()
if self.playbooks.create:
pb = self._get_ansible_playbook(self.playbooks.create)
pb.execute()

def prepare(self) -> None:
"""Execute ``ansible-playbook`` against the prepare playbook and returns None."""
pb = self._get_ansible_playbook(self.playbooks.prepare)
pb.execute()
if self.playbooks.prepare:
pb = self._get_ansible_playbook(self.playbooks.prepare)
pb.execute()

def syntax(self) -> None:
"""Execute `ansible-playbook` against the converge playbook with the -syntax-check flag."""
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("syntax-check", True) # noqa: FBT003
pb.execute()
if self.playbooks.converge:
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("syntax-check", value=True)
pb.execute()

def verify(self, action_args: list[str] | None = None) -> None:
"""Execute ``ansible-playbook`` against the verify playbook.
Args:
action_args: Arguments to pass on to the verify playbook.
"""
playbooks = []
if action_args:
playbooks = [self._config.provisioner.abs_path(playbook) for playbook in action_args]
else:
playbooks = [self.abs_path(playbook) for playbook in action_args]
elif self.playbooks.verify:
playbooks = [self.playbooks.verify]
if not playbooks:
LOG.warning("Skipping, verify playbook not configured.")
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
self._ansible_command: list[str] = []
self._playbook = playbook
self._config = config
self._cli: dict[str, str] = {}
self._cli: dict[str, str | bool] = {}
self._env: dict[str, str] = {}
if verify:
self._env = util.merge_dicts(
Expand Down Expand Up @@ -144,7 +144,7 @@ def execute(self, action_args: list[str] | None = None) -> str: # noqa: ARG002

return result.stdout

def add_cli_arg(self, name: str, value: str) -> None:
def add_cli_arg(self, name: str, value: str | bool) -> None:
"""Add argument to CLI passed to ansible-playbook.
Args:
Expand Down
6 changes: 5 additions & 1 deletion src/molecule/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@ class InventoryData(TypedDict):
links: dict[str, str]


class PlatformData(TypedDict):
class PlatformData(TypedDict, total=False):
"""Platform data for a Molecule run.
Attributes:
name: Name of the platform.
groups: Optional list of groups.
children: Optional list of child groups.
"""

name: str
groups: list[str]
children: list[str]


class PlaybookData(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def os_walk(
yield str(filename)


def render_template(template: str, **kwargs: str) -> str:
def render_template(template: str, **kwargs: dict[str, str]) -> str:
"""Render a jinaj2 template.
Args:
Expand Down

0 comments on commit 65f4a2e

Please sign in to comment.