Is there a tool to convert an ansible-playbook to a Dockerfile?

Viewed 703

I have an ansible-playbook with 1000+ lines that builds my VM and I want to build a docker image with the same project-structure, dependencies, users, permissions etc.

so for example this code will be converted to "RUN pip3 install requests jsonschema ..."

  - name: Install pip3 dependencies
    pip:
      executable: pip3
      name:
        - requests
        - jsonschema
        ...
2 Answers

Not to convert into a Dockerfile bit to spit out a container image on the other side of the build pipeline, you could use ansible-bender.

At first it seems a bit odd to have a 1000+ lines ansible playbook. It should be more split up into roles, configurations and so forth, so you can better maintain and control your ansible playbook's target state.

Secondly, I doubt, that there's an automatism to solve your problem. What I can think of is something like running the playbook against your image or even, within your image and than "docker saving" it.

This could be the option to move faster towards your goal having a docker image with the same state your VM has, whenn you run ansible-playbook on it.

While you choose a simple docker base image to cover your ansible dependencies, you may extract specific playbook logic and put it into a RUN expression in your dockerfile, so, slicing the elephant your playbook seems to be into smaller portions that are covered by your ansible scripting.

I'd go for running ansible against a docker container, later saving the container state and releasing the image.

Related