Answers: <p>If it's within scope, I would use <a href="https://packer.io" rel="nofollow noreferrer">Packer</a> to build the artifacts.</p>
  
  <ul>
  <li>Write a <a href="https://www.packer.io/docs/templates/index.html" rel="nofollow noreferrer">Packer template</a> using the builders that you want:
  
  <ul>
  <li><a href="https://www.packer.io/docs/builders/vagrant.html" rel="nofollow noreferrer">Vagrant builder</a> for the Vagrant image</li>
  <li><a href="https://www.packer.io/docs/builders/docker.html" rel="nofollow noreferrer">Docker builder</a> for the Docker image.</li>
  </ul></li>
  <li>Use the same Ansible playbook as you already have to provision each of the builders.</li>
  </ul>
  
  <p>Note that you need to make sure that the Ansible playbook will work in both types of virtualised environments.</p>
  
  <p>This has the benefit of producing arbitrary types of artifacts, based off of a single input. You can use a single workflow for both the Vagrant and the Docker images, and they will be built off of the same state of the codebase (<em>i.e.</em> git hash).</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-12T16:50:18.560Z
Answers: After I created my image and used Ansible for the provisioning, I'd like to deploy it to AWS. For Docker that would be ECR.
  
  Can I only do this by directly using an AMI Builder? Or can I keep the Docker builder and just deploy to AWS instead of .e.g exporting it to a file. 
  I am trying to see the whole picture from pulling and building the image, provisioning it to deploying and starting it.
  Would be nice if you could give me some advice for that too :)
Answers: Kyu96
Answers: CC BY-SA 4.0
Answers: 2019-11-22T16:46:21.253Z
Answers: 2019-11-22T16:46:21.253Z
Answers: Bruce Becker
Body: <p>So I am working on a research project and I am trying to automatically generate specific environments on different virtualization techniques.  </p>
  
  <p>So, first I made a Vagrantfile that pulls an Ubuntu image and then runs an Ansible  playbook. I have a playbook that defines everything for my environment.
  Which means it copies files, installs and configures everything.
  For VirtualBox this is no problem, - it spins up my environment in VirtualBox.</p>
  
  <p>Now I want to do this exact same thing, but on Docker. Normally one would write a docker file to define the environment. What I want to do is re-use the Ansible script to define the environment (despite being a bit counter-intuitive). While there seems to be some support for Docker in Vagrant - what is the best way to achieve what I want. I want to create a Docker Container - exactly how specified in the Ansible playbook. (Basically re-using the playbook I also used to setup VirtualBox). The output should then be a docker image that I can just start with docker run.</p>
  
  <p>Whats the best way to do this? What do I have to look out for?  </p>
  
  <p>My current idea is the following:</p>
  
  <pre><code>1) Setup a VM with Vagrant.
  2) Install Ansible and Docker in that VM.
  3) Create a new Docker Image with Ansible. It just defines the Container OS and maybe opens a port so Ansible can SSH into the container?
  4) Run the playbook in the container to configure everything.
  5) Package the entire thing as a new image
  6) Transfer packaged docker image to the host machine
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-11-22T16:34:56.703Z
Comments: I would suggest using the same Ansible playbook, but using Packer as a provisioner. Is this acceptable?
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2019-11-23T01:14:02.107Z
Comments: The best way to do something bad is to not do it. Docker is not a hypervisor, it doesn't solve the isolation problem like VMs do. It sounds like you either need VMs (this can be fully automated as well, mind you) or you need to use Docker for solving other problems.
Comments: Birb
Tags: <docker><ansible><vagrant><environments>
Title: Build Docker Images with Ansible + Vagrant
UserDisplayName: Kyu96
ThreadText: # Build Docker Images with Ansible + Vagrant
  
  (Asked by: username_0 on 2019-11-22)
  
  So I am working on a research project and I am trying to automatically generate specific environments on different virtualization techniques.
  
  So, first I made a Vagrantfile that pulls an Ubuntu image and then runs an Ansible playbook. I have a playbook that defines everything for my environment.
  Which means it copies files, installs and configures everything.
  For VirtualBox this is no problem, - it spins up my environment in VirtualBox.
  
  Now I want to do this exact same thing, but on Docker. Normally one would write a docker file to define the environment. What I want to do is re-use the Ansible script to define the environment (despite being a bit counter-intuitive). While there seems to be some support for Docker in Vagrant - what is the best way to achieve what I want. I want to create a Docker Container - exactly how specified in the Ansible playbook. (Basically re-using the playbook I also used to setup VirtualBox). The output should then be a docker image that I can just start with docker run.
  
  Whats the best way to do this? What do I have to look out for?
  
  My current idea is the following:
  
  ```
  1) Setup a VM with Vagrant.
  2) Install Ansible and Docker in that VM.
  3) Create a new Docker Image with Ansible. It just defines the Container OS and maybe opens a port so Ansible can SSH into the container?
  4) Run the playbook in the container to configure everything.
  5) Package the entire thing as a new image
  6) Transfer packaged docker image to the host machine
  
  ```
  
  **Tags:** docker, ansible, vagrant, environments
  
  > **Comments:**
  > **username_1**: I would suggest using the same Ansible playbook, but using Packer as a provisioner. Is this acceptable?  
  > **username_2**: The best way to do something bad is to not do it. Docker is not a hypervisor, it doesn't solve the isolation problem like VMs do. It sounds like you either need VMs (this can be fully automated as well, mind you) or you need to use Docker for solving other problems.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-22. Score: 4 | Accepted answer
  
  If it's within scope, I would use [Packer](https://packer.io) to build the artifacts.
  
  * Write a [Packer template](https://www.packer.io/docs/templates/index.html) using the builders that you want:
    + [Vagrant builder](https://www.packer.io/docs/builders/vagrant.html) for the Vagrant image
    + [Docker builder](https://www.packer.io/docs/builders/docker.html) for the Docker image.
  * Use the same Ansible playbook as you already have to provision each of the builders.
  
  Note that you need to make sure that the Ansible playbook will work in both types of virtualised environments.
  
  This has the benefit of producing arbitrary types of artifacts, based off of a single input. You can use a single workflow for both the Vagrant and the Docker images, and they will be built off of the same state of the codebase (*i.e.* git hash).
  
  > **Comments:**
  > **username_0**: After I created my image and used Ansible for the provisioning, I'd like to deploy it to AWS. For Docker that would be ECR.  Can I only do this by directly using an AMI Builder? Or can I keep the Docker builder and just deploy to AWS instead of .e.g exporting it to a file.  I am trying to see the whole picture from pulling and building the image, provisioning it to deploying and starting it. Would be nice if you could give me some advice for that too :)  
  
  ---
  
  