diff --git a/README.rst b/README.rst index c15e6a7..eb797d4 100644 --- a/README.rst +++ b/README.rst @@ -26,6 +26,18 @@ This plugin requires `containers.podman` collection to be present: Please note that this driver is currently in its early stage of development. +Change podman executable +======================== + +To change the podman executable from the standard podman, export environment +variable ``MOLECULE_PODMAN_EXECUTABLE``. For instance, if you wish to run +molecule with ``podman-remote`` instead of ordinary ``podman``, the variable +can be exported as: + +.. code-block:: console + + $ export MOLECULE_PODMAN_EXECUTABLE=podman-remote + .. _get-involved: Get Involved diff --git a/src/molecule_podman/driver.py b/src/molecule_podman/driver.py index b1e3b85..a7ab208 100644 --- a/src/molecule_podman/driver.py +++ b/src/molecule_podman/driver.py @@ -21,10 +21,11 @@ from __future__ import absolute_import +import distutils.spawn import os from typing import Dict -from molecule import logger +from molecule import logger, util from molecule.api import Driver from molecule.util import lru_cache @@ -152,6 +153,14 @@ def __init__(self, config=None): """Construct Podman.""" super(Podman, self).__init__(config) self._name = "podman" + # To change the podman executable, set environment variable + # MOLECULE_PODMAN_EXECUTABLE + # An example could be MOLECULE_PODMAN_EXECUTABLE=podman-remote + self.podman_exec = os.environ.get("MOLECULE_PODMAN_EXECUTABLE", "podman") + self.podman_cmd = distutils.spawn.find_executable(self.podman_exec) + if not self.podman_cmd: + msg = f"command not found in PATH {self.podman_exec}" + util.sysexit_with_message(msg) @property def name(self): @@ -164,7 +173,7 @@ def name(self, value): @property def login_cmd_template(self): return ( - "podman exec " + f"{self.podman_cmd} exec " "-e COLUMNS={columns} " "-e LINES={lines} " "-e TERM=bash " @@ -184,7 +193,10 @@ def login_options(self, instance_name): return {"instance": instance_name} def ansible_connection_options(self, instance_name): - return {"ansible_connection": "podman"} + return { + "ansible_connection": "podman", + "ansible_podman_executable": f"{self.podman_exec}", + } @lru_cache() def sanity_checks(self): diff --git a/src/molecule_podman/playbooks/create.yml b/src/molecule_podman/playbooks/create.yml index 7f29b63..cbaeab5 100644 --- a/src/molecule_podman/playbooks/create.yml +++ b/src/molecule_podman/playbooks/create.yml @@ -5,10 +5,22 @@ gather_facts: false no_log: "{{ molecule_no_log }}" become: "{{ not (item.rootless|default(true)) }}" + vars: + podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}" tasks: + - name: get podman executable path + command: which {{ podman_exec }} + register: podman_path + environment: + PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin" + changed_when: false + - name: save path to executable as fact + set_fact: + podman_cmd: "{{ podman_path.stdout }}" + - name: Log into a container registry command: > - podman login + {{ podman_cmd }} login --username {{ item.registry.credentials.username }} --password {{ item.registry.credentials.password }} --tls-verify={{ item.tls_verify | default(lookup('env', 'DOCKER_TLS_VERIFY')) or false }} @@ -54,6 +66,7 @@ - name: Discover local Podman images containers.podman.podman_image_info: name: "molecule_local/{{ item.item.name }}" + executable: "{{ podman_exec }}" with_items: "{{ platforms.results }}" loop_control: label: "{{ item.item.name | default('None specified') }}" @@ -63,7 +76,7 @@ - name: Build an Ansible compatible image # noqa: no-handler command: > - podman build + {{ podman_cmd }} build -f {{ item.dest }} -t molecule_local/{{ item.item.image }} {% if item.item.buildargs is defined %}{% for i,k in item.item.buildargs.items() %}--build-arg={{ i }}={{ k }} {% endfor %}{% endif %} @@ -96,7 +109,7 @@ # https://github.com/ansible-community/molecule-podman/issues/22 - name: Remove possible pre-existing containers command: > - podman rm -f -i -v {% for key in molecule_yml.platforms %}{{ key.name }} {% endfor %} + {{ podman_cmd }} rm -f -i -v {% for key in molecule_yml.platforms %}{{ key.name }} {% endfor %} register: result changed_when: true failed_when: false @@ -104,6 +117,7 @@ - name: Discover local podman networks containers.podman.podman_network_info: name: "{{ item.network }}" + executable: "{{ podman_exec }}" loop: "{{ molecule_yml.platforms | flatten(levels=1) }}" loop_control: extended: true @@ -117,6 +131,7 @@ - name: Create podman network dedicated to this scenario containers.podman.podman_network: name: "{{ podman_network.results[0].ansible_loop.allitems[0].network }}" + executable: "{{ podman_exec }}" subnet: "{{ podman_network.results[0].ansible_loop.allitems[0].subnet }}" when: @@ -126,7 +141,7 @@ - name: Create molecule instance(s) command: > - podman + {{ podman_cmd }} {% if item.cgroup_manager is defined %}--cgroup-manager={{ item.cgroup_manager }}{% endif %} {% if item.storage_opt is defined %}--storage-opt={{ item.storage_opt }}{% endif %} {% if item.storage_driver is defined %}--storage-driver={{ item.storage_driver }}{% endif %} diff --git a/src/molecule_podman/playbooks/destroy.yml b/src/molecule_podman/playbooks/destroy.yml index 71389dd..1f3e1b7 100644 --- a/src/molecule_podman/playbooks/destroy.yml +++ b/src/molecule_podman/playbooks/destroy.yml @@ -5,9 +5,11 @@ gather_facts: false no_log: "{{ molecule_no_log }}" become: "{{ not (item.rootless|default(true)) }}" + vars: + podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}" tasks: - name: Destroy molecule instance(s) - shell: podman container exists {{ item.name }} && podman rm -f {{ item.name }} || true + shell: "{{ podman_exec }} container exists {{ item.name }} && {{ podman_exec }} rm -f {{ item.name }} || true" register: server with_items: "{{ molecule_yml.platforms }}" async: 7200