molecule-podman icon indicating copy to clipboard operation
molecule-podman copied to clipboard

Ansible rebuilds images after molecule destroy when pre_build_image: false

Open nahsi opened this issue 4 years ago • 2 comments

When I run molecule create with pre_build_image: false ansible recreates image everytime. I'm not sure if this is a bug or expected behavior?

After molecule create

ls ~/.cache/molecule/gentoo-portage/default/
ansible.cfg                  Dockerfile_gentoo_portage  molecule.yml
inventory                     state.yml

After molecule destroy Dockerfile is gone

ls ~/.cache/molecule/gentoo-portage/default/
ansible.cfg  inventory  state.yml

Next time I run molecule create Dockerfiles would be created again triggering Build an Ansible compatible image with platforms.changed

My molecule.yml file

driver:
  name: podman
platforms:
  - name: gentoo-portage
    image: gentoo-portage
    dockerfile: Containerfile
    pre_build_image: false
    cmd: /sbin/init
 provisioner:
  name: ansible
verifier:
  name: ansible

nahsi avatar Dec 09 '20 15:12 nahsi

This is the expected behavior. While Build an Ansible compatible image is rebuilding each time, molecule does not prune the image, only the instance. This means on successive builds where no changes are made, we're taking advantage of the image layers already existing on the host, so the build is almost instantaneous:

$ podman system prune -af
$ time molecule create

real	1m2.137s
user	0m42.328s
sys	0m7.961s
$ molecule destroy
$ time molecule create

real	0m3.748s
user	0m3.192s
sys	0m0.636s

In theory the Ansible task could better describe this, but I personally believe this to be expected behavior of minimal note and is specific to molecule itself, not molecule-podman. I've experienced the exact behavior when using Docker, so it is not isolated to Podman as a driver. Improving the explanation around pre_build_image in the documentation might help to clarify this for future users, though I think this behavior is hinted at in some capacity, if not expressly stated somewhere I tend skim over.

Hope this is (mostly) correct and helps to clarify! Happy hacking :smile:

jcmdln avatar Feb 17 '21 05:02 jcmdln

Ah, looks like there's an open issue to track a way of determining if containers should be rebuilt by checking whether the configuration was changed: https://github.com/ansible-community/molecule/issues/2175

I think this ticket might be a more suitable location for describing desired behavior, as it looks like this has been an open idea for some time.

jcmdln avatar Feb 17 '21 18:02 jcmdln