Created
October 27, 2024 17:53
-
-
Save danfoust/a259b9e3b2f2647e9c980b200eabb635 to your computer and use it in GitHub Desktop.
Local Ansible - Vagrant with Docker Provider
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
############################################################################################## | |
# This helps unblock me on following a long with materials for learning Ansible and Kubernetes | |
# locally, which rely on a VM service. | |
# | |
# I did not want to use VirtualBox, because on Linux it requires tainting the kernel. I also | |
# already have Docker installed and it's said to have better performance/less overhead. | |
# | |
# This Dockerfile will setup an Ubuntu container and configure it to support SSH, which is | |
# required for testing Ansible locally with Vagrant. | |
# | |
# Credit to Mario Garcia | |
# Medium Article: https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me | |
############################################################################################## | |
FROM ubuntu | |
# When running apt-get update -y or apt update -y, it will ask you to configure | |
# the timezone, the prompt will wait for you to enter the selected option. | |
# To avoid this, we add these configuration options. | |
ENV TZ=America/Mexico_City | |
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone | |
RUN apt-get update -y | |
RUN apt-get install -y --no-install-recommends ssh sudo | |
# Creates `vagrant` user. With Vagrant, we don't want to require a password for root level commands | |
# We also add them to the sudoers group | |
RUN useradd --create-home -s /bin/bash vagrant | |
RUN echo -n 'vagrant:vagrant' | chpasswd | |
RUN echo 'vagrant ALL = NOPASSWD: ALL' > /etc/sudoers.d/vagrant | |
RUN chmod 440 /etc/sudoers.d/vagrant | |
# Create the `.ssh` directory | |
RUN mkdir -p /home/vagrant/.ssh | |
RUN chmod 700 /home/vagrant/.ssh | |
# Vagrant connnects over ssh. Add an insecure key. It wil be replaced by Vagrant with a secure key on first `vagrant up` | |
RUN echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==" > /home/vagrant/.ssh/authorized_keys | |
RUN chmod 600 /home/vagrant/.ssh/authorized_keys | |
RUN chown -R vagrant:vagrant /home/vagrant/.ssh | |
RUN sed -i -e 's/Defaults.*requiretty/#&/' /etc/sudoers | |
RUN sed -i -e 's/\(UsePAM \)yes/\1 no/' /etc/ssh/sshd_config | |
RUN mkdir /var/run/sshd | |
RUN apt-get -y install openssh-client | |
EXPOSE 22 | |
# This makes Docker run the SSH daemon in the foreground. Docker expects a foreground process to be running or it will | |
# close the container immediately. | |
CMD ["/usr/sbin/sshd", "-D"] |
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
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
Vagrant.configure("2") do |config| | |
config.vm.provider "docker" do |d| | |
# Use `Dockerfile` that's in the same directory as `Vagrantfile` | |
d.build_dir = "." | |
# Tells Vagrant to expect this Docker container to remain running | |
d.remains_running = true | |
# Required for Vagrant to support SSH with the Docker container | |
d.has_ssh = true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment