Created
March 26, 2017 01:41
-
-
Save popstas/9a42d198fe7c5bee317d0bc4e2e2af9f to your computer and use it in GitHub Desktop.
Build docker and vagrant images with packer and ansible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
sync | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# hosting group excluded roles who don't work in docker | |
# need for local environments | |
- hosts: all | |
become: yes | |
roles: | |
- apache | |
- mysql | |
- nginx | |
- php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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": "ansible.sh", | |
"override": { | |
"virtualbox-iso": { | |
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'" | |
} | |
} | |
}, | |
{ | |
"type": "shell", | |
"script": "setup.sh", | |
"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/vagrant.sh", | |
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'", | |
"only": ["virtualbox-iso"] | |
}, | |
{ | |
"type": "shell", | |
"script": "packer/scripts/cleanup.sh", | |
"override": { | |
"virtualbox-iso": { | |
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'" | |
} | |
} | |
} | |
], | |
"builders": [ | |
{ | |
"type": "virtualbox-iso", | |
"boot_command": [ | |
"<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>", | |
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>", | |
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>", | |
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>", | |
"/install/vmlinuz<wait>", | |
" 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>", | |
"<enter><wait>" | |
], | |
"boot_wait": "10s", | |
"disk_size": 81920, | |
"guest_os_type": "Ubuntu_64", | |
"headless": true, | |
"http_directory": "packer/http", | |
"iso_urls": [ | |
"ubuntu-16.04.2-server-amd64.iso", | |
"http://releases.ubuntu.com/16.04/ubuntu-16.04.2-server-amd64.iso" | |
], | |
"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": [ | |
[ | |
"modifyvm", | |
"{{.Name}}", | |
"--memory", | |
"1024" | |
], | |
[ | |
"modifyvm", | |
"{{.Name}}", | |
"--cpus", | |
"1" | |
] | |
] | |
}, | |
{ | |
"type": "docker", | |
"image": "ubuntu:16.04", | |
"commit": true | |
} | |
], | |
"post-processors": [ | |
{ | |
"type": "vagrant", | |
"output": "packer/builds/{{.Provider}}-ubuntu1604.box", | |
"compression_level": "9", | |
"only": ["virtualbox-iso"] | |
}, | |
{ | |
"type": "docker-tag", | |
"repository": "{{user `docker_output_image`}}", | |
"tag": "{{user `docker_output_tag`}}", | |
"only": ["docker"] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -eux | |
pubkey_url="https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub"; | |
HOME_DIR="${HOME_DIR:-/home/vagrant}"; | |
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; | |
else | |
echo "Cannot download vagrant public key"; | |
exit 1; | |
fi | |
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