diff --git a/changelogs/fragments/63166-add-extra-args-executalbe-podman-connection.yaml b/changelogs/fragments/63166-add-extra-args-executalbe-podman-connection.yaml new file mode 100644 index 00000000000..8fd91f78329 --- /dev/null +++ b/changelogs/fragments/63166-add-extra-args-executalbe-podman-connection.yaml @@ -0,0 +1,2 @@ +minor_changes: + - podman connection - allow to add extra arguments to podman command and to configure the executable. diff --git a/lib/ansible/plugins/connection/podman.py b/lib/ansible/plugins/connection/podman.py index f22e437a547..cc166d0b163 100644 --- a/lib/ansible/plugins/connection/podman.py +++ b/lib/ansible/plugins/connection/podman.py @@ -10,6 +10,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import distutils.spawn import shlex import shutil import subprocess @@ -48,6 +49,25 @@ DOCUMENTATION = """ - name: ANSIBLE_REMOTE_USER vars: - name: ansible_user + podman_extra_args: + description: + - Extra arguments to pass to the podman command line. + default: '' + ini: + - section: defaults + key: podman_extra_args + vars: + - name: ansible_podman_extra_args + env: + - name: ANSIBLE_PODMAN_EXTRA_ARGS + podman_executable: + description: + - Executable for podman command. + default: podman + vars: + - name: ansible_podman_executable + env: + - name: ANSIBLE_PODMAN_EXECUTABLE """ @@ -79,7 +99,17 @@ class Connection(ConnectionBase): :param in_data: data passed to podman's stdin :return: return code, stdout, stderr """ - local_cmd = ['podman', cmd] + podman_exec = self.get_option('podman_executable') + podman_cmd = distutils.spawn.find_executable(podman_exec) + if not podman_cmd: + raise AnsibleError("%s command not found in PATH" % podman_exec) + local_cmd = [podman_cmd] + if self.get_option('podman_extra_args'): + local_cmd += shlex.split( + to_native( + self.get_option('podman_extra_args'), + errors='surrogate_or_strict')) + local_cmd.append(cmd) if use_container_id: local_cmd.append(self._container_id) if cmd_args: diff --git a/test/integration/targets/connection_podman/runme.sh b/test/integration/targets/connection_podman/runme.sh index d12223e412d..36dfefc4b4a 100755 --- a/test/integration/targets/connection_podman/runme.sh +++ b/test/integration/targets/connection_podman/runme.sh @@ -5,3 +5,10 @@ set -eux ./posix.sh "$@" ANSIBLE_REMOTE_TMP="/tmp" ANSIBLE_REMOTE_USER="1000" ./posix.sh "$@" +ANSIBLE_PODMAN_EXECUTABLE=fakepodman ./posix.sh "$@" 2>&1 | grep "fakepodman command not found in PATH" + +ANSIBLE_PODMAN_EXECUTABLE=fakepodman ./posix.sh "$@" && { + echo "Playbook with fakepodman should fail!" + exit 1 +} +ANSIBLE_VERBOSITY=4 ANSIBLE_PODMAN_EXTRA_ARGS=" --log-level debug " ./posix.sh "$@" | grep "level=debug msg="