Packer builds machine images. Ansible configures servers. Together, they let you bake your configuration straight into the image – no manual setup required after deployment.

The Ansible provisioner in Packer runs your playbooks during the image build process. You write a normal Ansible playbook, point Packer at it, and when the image is ready, your software is already installed and configured.

Note: If you specify a remote_user in your Ansible tasks, Packer will ignore it. The provisioner connects using the username from Packer’s own configuration.

Example

Here’s a basic Packer template using DigitalOcean as the builder:

{
  "provisioners": [
    {
      "type": "ansible",
      "playbook_file": "./playbook.yml"
    }
  ],

  "builders": [
    {
      "type": "digitalocean",
      "api_token": "YOUR_API_TOKEN_GOES_HERE",
      "image": "ubuntu-14-04-x64",
      "region": "sfo1"
    }
  ]
}

The playbook.yml file is just a standard Ansible playbook – nothing special required. See the Packer Ansible provisioner docs for more options.