From 81296caf1849bddcac720c8e399925a12bc9020c Mon Sep 17 00:00:00 2001 From: grcancelliere Date: Sun, 9 Jul 2023 09:12:16 +0200 Subject: [PATCH] podman: set ansible_async_dir only when $HOME is redefined - src/molecule_plugins/podman/playbooks: reworked the ansible_async_dir logic: - use ansible.builtin.getent to get the passwd information for $USER - use the ansible.builtin.config lookup to get the async_dir information - set ansible_async_dir by rewriting the home folder only if the two home paths are different - tox.ini: add USER to the pass_env list, since the new ansible_async_dir logic relies on looking up the equivalent of getent passwd $USER --- .../podman/playbooks/create.yml | 17 +++++++++++++++-- .../podman/playbooks/destroy.yml | 17 +++++++++++++++-- tox.ini | 1 + 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/molecule_plugins/podman/playbooks/create.yml b/src/molecule_plugins/podman/playbooks/create.yml index 0d81b88c..e761048f 100644 --- a/src/molecule_plugins/podman/playbooks/create.yml +++ b/src/molecule_plugins/podman/playbooks/create.yml @@ -17,10 +17,23 @@ ansible.builtin.set_fact: podman_cmd: "{{ _podman_path.stdout }}" + - name: Get passwd entries for USER env + ansible.builtin.getent: + database: passwd + key: "{{ lookup('env', 'USER') }}" + + - name: Get shell async_dir + ansible.builtin.set_fact: + _shell_async_dir: >- + {{ lookup('ansible.builtin.config', 'async_dir', plugin_type='shell', plugin_name='sh') + | regex_replace('^~', ansible_facts.getent_passwd[lookup('env', 'USER')][4]) }} + - name: Set async_dir for HOME env ansible.builtin.set_fact: - ansible_async_dir: "{{ lookup('env', 'HOME') }}/.ansible_async/" - when: (lookup('env', 'HOME')) + ansible_async_dir: >- + {{ _shell_async_dir + | regex_replace('^' + ansible_facts.getent_passwd[lookup('env', 'USER')][4], lookup('env', 'HOME')) }} + when: lookup('env', 'HOME') != ansible_facts.getent_passwd[lookup('env', 'USER')][4] - name: Log into a container registry ansible.builtin.command: > diff --git a/src/molecule_plugins/podman/playbooks/destroy.yml b/src/molecule_plugins/podman/playbooks/destroy.yml index 711e6d18..26199935 100644 --- a/src/molecule_plugins/podman/playbooks/destroy.yml +++ b/src/molecule_plugins/podman/playbooks/destroy.yml @@ -8,10 +8,23 @@ vars: podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}" tasks: + - name: Get passwd entries for USER env + ansible.builtin.getent: + database: passwd + key: "{{ lookup('env', 'USER') }}" + + - name: Get shell async_dir + ansible.builtin.set_fact: + _shell_async_dir: >- + {{ lookup('ansible.builtin.config', 'async_dir', plugin_type='shell', plugin_name='sh') + | regex_replace('^~', ansible_facts.getent_passwd[lookup('env', 'USER')][4]) }} + - name: Set async_dir for HOME env ansible.builtin.set_fact: - ansible_async_dir: "{{ lookup('env', 'HOME') }}/.ansible_async/" - when: (lookup('env', 'HOME')) + ansible_async_dir: >- + {{ _shell_async_dir + | regex_replace('^' + ansible_facts.getent_passwd[lookup('env', 'USER')][4], lookup('env', 'HOME')) }} + when: lookup('env', 'HOME') != ansible_facts.getent_passwd[lookup('env', 'USER')][4] - name: Destroy molecule instance(s) ansible.builtin.shell: "{{ podman_exec }} container exists {{ item.name }} && {{ podman_exec }} rm -f {{ item.name }} || true" diff --git a/tox.ini b/tox.ini index fa395466..3c1f675f 100644 --- a/tox.ini +++ b/tox.ini @@ -57,6 +57,7 @@ passenv = SSL_CERT_FILE TOXENV TWINE_* + USER allowlist_externals = bash twine