Skip to content

Instantly share code, notes, and snippets.



Created Mar 26, 2017
What would you like to do?
Build docker and vagrant images with packer and ansible
#!/bin/bash -eux
# Install Ansible repository.
apt -y update && apt-get -y upgrade
apt -y install software-properties-common
apt-add-repository ppa:ansible/ansible
# Install Ansible.
apt -y update
apt -y install ansible
#!/bin/bash -eux
# Uninstall Ansible and remove PPA.
apt -y remove --purge ansible
apt-add-repository --remove ppa:ansible/ansible
# Apt cleanup.
#apt autoremove # docker freezes here
apt update
# Delete unneeded files in vagrant
if [ -d /home/vagrant ]; then
rm -f /home/vagrant/*.sh
# Zero out the rest of the free space using dd, then delete the written file.
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
# Add `sync` so Packer doesn't quit too early, before the large file is deleted.
# hosting group excluded roles who don't work in docker
# need for local environments
- hosts: all
become: yes
- apache
- mysql
- nginx
- php
choose-mirror-bin mirror/http/proxy string
d-i base-installer/kernel/override-image string linux-server
d-i clock-setup/utc boolean true
d-i clock-setup/utc-auto boolean true
d-i finish-install/reboot_in_progress note
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/method string lvm
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm_write_new_label boolean true
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev linux-source dkms nfs-common
d-i pkgsel/install-language-support boolean false
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select full-upgrade
d-i time/zone string UTC
tasksel tasksel/first multiselect standard, ubuntu-server
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layoutcode string us
d-i keyboard-configuration/modelcode string pc105
d-i debian-installer/locale string en_US
# Create vagrant user account.
d-i passwd/user-fullname string vagrant
d-i passwd/username string vagrant
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
d-i passwd/user-default-groups vagrant sudo
d-i passwd/user-uid string 900
#!/bin/bash -eux
# Add vagrant user to sudoers.
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
sed -i "s/^.*requiretty/#Defaults requiretty/" /etc/sudoers
# Disable daily apt unattended updates.
echo 'APT::Periodic::Enable "0";' >> /etc/apt/apt.conf.d/10periodic
"variables": {
"playbook": "playbooks/groups/hosting-base.yml",
"requirements": "requirements-hosting-base-galaxy.yml",
"docker_output_image": "popstas/ansible-server-base",
"docker_output_tag": "latest"
"provisioners": [
"type": "shell",
"script": "",
"override": {
"virtualbox-iso": {
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
"type": "shell",
"script": "",
"override": {
"virtualbox-iso": {
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
"type": "ansible-local",
"playbook_file": "{{user `playbook`}}",
"extra_arguments": [ "--extra-vars 'zsh_user=vagrant hostname_custom_name=ansible-server'" ],
"group_vars" : "group_vars",
"galaxy_file": "{{user `requirements`}}"
"type": "shell",
"script": "packer/scripts/",
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"only": ["virtualbox-iso"]
"type": "shell",
"script": "packer/scripts/",
"override": {
"virtualbox-iso": {
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'"
"builders": [
"type": "virtualbox-iso",
"boot_command": [
" auto<wait>",
" console-setup/ask_detect=false<wait>",
" console-setup/layoutcode=us<wait>",
" console-setup/modelcode=pc105<wait>",
" debconf/frontend=noninteractive<wait>",
" debian-installer=en_US<wait>",
" fb=false<wait>",
" initrd=/install/initrd.gz<wait>",
" kbd-chooser/method=us<wait>",
" keyboard-configuration/layout=USA<wait>",
" keyboard-configuration/variant=USA<wait>",
" locale=en_US<wait>",
" netcfg/get_domain=vm<wait>",
" netcfg/get_hostname=vagrant<wait>",
" grub-installer/bootdev=/dev/sda<wait>",
" noapic<wait>",
" preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
" -- <wait>",
"boot_wait": "10s",
"disk_size": 81920,
"guest_os_type": "Ubuntu_64",
"headless": true,
"http_directory": "packer/http",
"iso_urls": [
"iso_checksum_type": "sha256",
"iso_checksum": "737ae7041212c628de5751d15c3016058b0e833fdc32e7420209b76ca3d0a535",
"output_directory": "packer/builds/images",
"ssh_username": "vagrant",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_wait_timeout": "10000s",
"shutdown_command": "echo 'vagrant'|sudo -S shutdown -P now",
"guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
"virtualbox_version_file": ".vbox_version",
"vm_name": "packer-ubuntu-16.04-amd64",
"vboxmanage": [
"type": "docker",
"image": "ubuntu:16.04",
"commit": true
"post-processors": [
"type": "vagrant",
"output": "packer/builds/{{.Provider}}",
"compression_level": "9",
"only": ["virtualbox-iso"]
"type": "docker-tag",
"repository": "{{user `docker_output_image`}}",
"tag": "{{user `docker_output_tag`}}",
"only": ["docker"]
#!/bin/bash -eux
mkdir -p $HOME_DIR/.ssh;
if command -v wget >/dev/null 2>&1; then
wget --no-check-certificate "$pubkey_url" -O $HOME_DIR/.ssh/authorized_keys;
elif command -v curl >/dev/null 2>&1; then
curl --insecure --location "$pubkey_url" > $HOME_DIR/.ssh/authorized_keys;
echo "Cannot download vagrant public key";
exit 1;
chown -R vagrant $HOME_DIR/.ssh;
chmod -R go-rwsx $HOME_DIR/.ssh;
# install virtualbox guest additions
sudo apt-get install virtualbox-guest-utils --no-install-recommends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.