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

Fixes podman always remove existing containers #187

Merged
merged 14 commits into from
Dec 2, 2023
Merged
128 changes: 59 additions & 69 deletions src/molecule_plugins/podman/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@
vars:
podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}"
tasks:
- name: Get podman executable path # noqa: command-instead-of-shell
ansible.builtin.shell: "command -v {{ podman_exec }}"
register: _podman_path
changed_when: false

- name: Register podman executable path
ansible.builtin.set_fact:
podman_cmd: "{{ _podman_path.stdout }}"

- name: Log into a container registry
ansible.builtin.command: >
{{ 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 }}
{% if lookup('env', 'DOCKER_CERT_PATH') %}
--cert-dir {{ item.cert_path | default(lookup('env', 'DOCKER_CERT_PATH') + '/cert.pem') }}{% endif %}
{{ item.registry.url }}
with_items: "{{ molecule_yml.platforms }}"
containers.podman.podman_login:
certdir: >-
{% if lookup('env', 'DOCKER_CERT_PATH') %}
{{ item.cert_path | default(lookup('env', 'DOCKER_CERT_PATH') + '/cert.pem') }}
{% endif %}
executable: "{{ podman_exec }}"
username: "{{ item.registry.credentials.username }}"
password: "{{ item.registry.credentials.password }}"
registry: "{{ item.registry.url }}"
tlsverify: "{{ item.registry.tls_verify | default(lookup('env', 'DOCKER_TLS_VERIFY')) or false }}"
loop: "{{ molecule_yml.platforms }}"
loop_control:
label: >-
"{{ item.name }} registry username:
Expand Down Expand Up @@ -77,14 +70,16 @@
register: podman_images

- name: Build an Ansible compatible image # noqa: no-handler
ansible.builtin.command: >
{{ 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 %}
{% if item.item.pull is defined %}--pull={{ item.item.pull }}{% endif %}
{{ molecule_scenario_directory + '/' + (item.item.dockerfile | default('Dockerfile.j2')) | dirname }}
with_items: "{{ platforms.results }}"
containers.podman.podman_image:
build:
extra_args: >-
{% if item.item.buildargs is defined %}{% for i, k in item.item.buildargs.items() %}--build-arg={{ i }}={{ k }}{% endfor %}{% endif %}
{% if item.item.pull is defined %}--pull={{ item.item.pull }}{% endif %}
file: "{{ item.dest }}"
name: "molecule_local/{{ item.item.image }}"
path: "{{ molecule_scenario_directory }}"
pull: "{{ item.item.pull | default(omit) }}"
loop: "{{ platforms.results }}"
loop_control:
label: "{{ item.item.image | default('None specified') }}"
when:
Expand All @@ -101,7 +96,7 @@
ansible.builtin.set_fact:
command_directives_dict: >-
{{ command_directives_dict | default({}) |
combine({ item.name: item.command | default('bash -c "while true; do sleep 10000; done"') })
combine({item.name: item.command | default(["bash", "-c", "while true; do sleep 10000; done"])})
}}
with_items: "{{ molecule_yml.platforms }}"
loop_control:
Expand All @@ -110,14 +105,6 @@
{{ item.command | default('None specified') }}"
when: item.override_command | default(true)

# https://github.com/ansible-community/molecule-podman/issues/22
- name: Remove possible pre-existing containers
ansible.builtin.command: >
{{ podman_cmd }} rm -f -i -v {% for key in molecule_yml.platforms %}{{ key.name }} {% endfor %}
register: result
changed_when: true
failed_when: false

- name: Discover local podman networks
containers.podman.podman_network_info:
name: "{{ item.network }}"
Expand Down Expand Up @@ -145,46 +132,49 @@
- "podman_network.results[0].ansible_loop.allitems[0].network not in ['bridge', 'none', 'host', 'ns', 'private', 'slirp4netns']"

- name: Create molecule instance(s)
ansible.builtin.command: >
{{ 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 %}
run
-d
--name "{{ item.name }}"
{% if item.pid_mode is defined %}--pid={{ item.pid_mode }}{% endif %}
{% if item.privileged is defined %}--privileged={{ item.privileged }}{% endif %}
{% if item.detach is defined %}--detach{% endif %}
{% if item.security_opts is defined %}{% for i in item.security_opts %}--security-opt {{ i }} {% endfor %}{% endif %}
{% if item.devices is defined %}{% for i in item.devices %}--device {{ i }} {% endfor %}{% endif %}
{% if item.volumes is defined %}{% for i in item.volumes %}--volume {{ i }} {% endfor %}{% endif %}
{% if item.tmpfs is defined %}{% for i in item.tmpfs %}--tmpfs={{ i }} {% endfor %}{% endif %}
{% if item.capabilities is defined %}{% for i in item.capabilities %}--cap-add={{ i }} {% endfor %}{% endif %}
{% if item.exposed_ports is defined %}--expose="{{ item.exposed_ports | join(',') }}"{% endif %}
{% if item.published_ports is defined %}{% for i in item.published_ports %}--publish={{ i }} {% endfor %}{% endif %}
{% if item.ulimits is defined %}{% for i in item.ulimits %}--ulimit={{ i }} {% endfor %}{% endif %}
{% if item.dns_servers is defined %}--dns="{{ item.dns_servers | join(',') }}"{% endif %}
{% if item.env is defined %}{% for i, k in item.env.items() %}--env={{ i }}={{ k }} {% endfor %}{% endif %}
{% if item.restart_policy is defined %}
--restart={{ item.restart_policy }}{% if item.restart_retries is defined %}:{{ item.restart_retries }}{% endif %}{% endif %}
{% if item.tty is defined %}--tty={{ item.tty }}{% endif %}
{% if item.network is defined %}--network={{ item.network }}{% endif %}
{% if item.ip is defined %}--ip={{ item.ip }}{% endif %}
{% if item.etc_hosts is defined %}
{% for i, k in item.etc_hosts.items() %}{% if i != item.name %}--add-host {{ i }}:{{ k }} {% endif %}{% endfor %}{% endif %}
{% if item.hostname is defined %}--hostname={{ item.hostname }}{% elif item.name is defined %}--hostname={{ item.name }}{% endif %}
{% if item.systemd is defined %}--systemd={{ item.systemd | string | lower }}{% endif %}
{{ item.extra_opts | default([]) | join(' ') }}
{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}
{{ (command_directives_dict | default({}))[item.name] | default('') }}
containers.podman.podman_container:
name: "{{ item.name }}"
cap_add: "{{ item.capabilities | default(omit) }}"
# it's necessary to exclude empty args as ansible modules run_command method doesn't handle this well
cmd_args: "{{ molecule_podman_args | reject('equalto', '') | list + item.extra_opts | default([]) }}"
command: "{{ (command_directives_dict | default({}))[item.name] | default('') }}"
detach: "{{ item.detach | default(omit) }}"
device: "{{ item.devices | default(omit) }}"
dns: "{{ item.dns_servers | default(omit) }}"
env: "{{ item.env | default(omit) }}"
etc_hosts: "{{ item.etc_hosts | default(omit) }}"
executable: "{{ podman_exec }}"
expose: "{{ item.exposed_ports | default(omit) }}"
hostname: "{{ item.hostname | default(omit) }}"
image: "{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}"
ip: "{{ item.ip | default(omit) }}"
network: "{{ item.network | default(omit) }}"
pid: "{{ item.pid_mode | default(omit) }}"
privileged: "{{ item.privileged | default(omit) }}"
publish: "{{ item.published_ports | default(omit) }}"
security_opt: "{{ item.security_opts | default(omit) }}"
systemd: "{{ item.systemd | default(omit) }}"
tmpfs: "{{ item.tmpfs | default(omit) }}"
tty: "{{ item.tty | default(omit) }}"
ulimits: "{{ item.ulimits | default(omit) }}"
volume: "{{ item.volumes | default(omit) }}"
state: started
register: server
changed_when: false
zhan9san marked this conversation as resolved.
Show resolved Hide resolved
with_items: "{{ molecule_yml.platforms }}"
loop_control:
label: "{{ item.name }}"
async: 7200
poll: 0
changed_when: true
vars:
molecule_podman_args:
- "{% if item.cgroup_manager is defined %}--cgroup-manager={{ item.cgroup_manager }}{% endif %}"
- >-
{% if item.restart_policy is defined %}
--restart={{ item.restart_policy }}{% if item.restart_retries is defined %}:{{ item.restart_retries }}{% endif %}
{% 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 %}"

- name: Wait for instance(s) creation to complete
become: "{{ not item.item.rootless | default(omit) }}"
Expand Down