We have just seen how to write a Packer template that uses command scripts (for example, apt-get), but it is also possible to use Ansible playbooks to create an image. Indeed, when we use IaC to configure VMs, we are often used to configuring the VMs directly using Ansible before thinking about making them into VM images.
What is interesting about Packer is that we can reuse the same playbook scripts that we used to configure VMs to create our VM images. So, it's a huge time saver because we don't have to rewrite the scripts.
To put this into practice, we will write the following:
- An Ansible playbook that installs nginx
- A Packer template that uses Ansible with our playbook
Let's start with the writing of the Ansible playbook.