Skip to content

Instantly share code, notes, and snippets.

@sblack4
Last active January 15, 2022 21:01
Show Gist options
  • Save sblack4/af65c0dfdb6632aded1405d3498fa899 to your computer and use it in GitHub Desktop.
Save sblack4/af65c0dfdb6632aded1405d3498fa899 to your computer and use it in GitHub Desktop.
How to develop AMIs with Packer and Ansible

How to develop AMIs with Packer and Ansible

Things I wish they'd told me

Develop the Ansible role locally

When you try to debug something in packer by making a change in the Ansible role you want to see the effect immediately. To do that just symlink your project in ~/.ansible/roles with whatever name you are referencing it with. So, for example, a project refered to as rhythmic.jetty which is locally in ansible-role-jetty:

`ln -s $(pwd)/ansible-role-jetty ~/.ansible/roles/rhythmic.jetty`

Don't run packer with -debug

If you do this IT WILL ASK YOU FOR INPUT BETWEEN EVERY STEP. This is perhaps one of the most annoying debug behavior ever.

Do run with -on-error=ask and a specified keypair

Now packer will run until an error and then ask you what to do:

==> my-jetty-ami: [c] Clean up and exit, [a] abort without cleanup, or [r] retry step (build may fail even if retry succeeds)?

By specifying the keypair you can use a key you already have in AWS to SSH in:

  "builders": [{
            "ssh_keypair_name": "the_key_is_automation",
            "ssh_private_key_file": "~/.ssh/tempKeys/the_key_is_automation.pem",
            "name": "my-jetty-ami",
            "type": "amazon-ebs",
            ...

Taking a look at what's wrong becomes as easy as SSH:

ssh -i ~/.ssh/tempkeys/the_key_is_automation.pem ec2-user@54.175.220.128

Take advantage of the Ansible debugger

If you've got a box set up (maybe from running packer with -on-error=ask) you can run the ansible debugger. It's a lot like pdb for Ansible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment