How to keep vagrant configuration organized - shell

I'm provisioning vagrant box with ansible. However, my vagrant host doesn't support ansible, so I must execute ansible against localhost from within the vagrant box. I'm using shell provisioner, executing a script which will install ansible and run the correct ansible command against a playbook in a synced folder.
This solution works, but feels messy and hard to maintain, as configuration data is cluttered between files. As you can see below, there is duplication of playbook path, hard coded both to Vagrantfile and provisioning script. And as we know, duplication is the source of evil. Rather, I would like to keep all configuration strings in Vagrantfile or external file. Is this possible in my case? It would be also nice to be able to override these values with command line parameters.
Here is the vagrant file
VAGRANT_API_VERSION = 2
BOX_IMAGE = "ubuntu/trusty64"
PROVISIONING_SRC = "./provisioning"
PROVISIONING_DEST = "/etc/provisioning/vagrant"
Vagrant.configure(VAGRANT_API_VERSION) do |config|
config.vm.box = BOX_IMAGE
config.vm.network "private_network", ip: "192.168.22.22"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 8081, host: 8081
config.vm.network "forwarded_port", guest: 2222, host: 2223
config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.network "forwarded_port", guest: 50000, host: 50000
config.vm.provision :shell, path: "provisioning.sh"
config.vm.synced_folder "./provisioning", "/etc/provisioning/vagrant"
end
Here is the provisioning script
apt-get install -y python-pip
apt-get install -y git-core
if [ -z $PLAYBOOK_PATH ]; then
echo "PLAYBOOK_PATH not set, using default"
PLAYBOOK_PATH=/etc/provisioning/vagrant/playbook.yml
fi
if [ -n $PLAYBOOK_PATH ]; then
echo "PLAYBOOK_PATH set at $PLAYBOOK_PATH"
if [ -f $PLAYBOOK_PATH ]; then
echo "PLAYBOOK FOUND, insalling ansible and starting provisioning"
pip install ansible
pip install markupsafe
if ( ansible-playbook $PLAYBOOK_PATH ); then
echo "provisioning OK"
else
echo "prvosioning failed"
fi
else
echo "PLAYBOOK not found"
fi
else
echo "PLAYBOOK_PATH not set"
fi
And finally here is the playbook inside provisioning folder
- name: Provision local docker host
hosts: localhost
become : yes
roles:
- docker.ubuntu
tasks:
- name: Add vagrant to docker group
become: yes
user: name=vagrant groups=docker append=yes

You dont necessarily need to do all this.
Vagrant supports running ansible on the guest VM aka ansible local
The Vagrant Ansible Local provisioner allows you to provision the guest using Ansible playbooks by executing ansible-playbook directly on the guest machine.
You can have a Vagrantfile like
VAGRANT_API_VERSION = 2
BOX_IMAGE = "ubuntu/trusty64"
PROVISIONING_SRC = "./provisioning"
PROVISIONING_DEST = "/etc/provisioning/vagrant"
Vagrant.configure(VAGRANT_API_VERSION) do |config|
config.vm.box = BOX_IMAGE
config.vm.network "private_network", ip: "192.168.22.22"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 8081, host: 8081
config.vm.network "forwarded_port", guest: 2222, host: 2223
config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.network "forwarded_port", guest: 50000, host: 50000
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "provisioning/playbook.yml"
end
end

Related

vagrant port forwarding guest and host port messed up

I'm using a vagrant trusty64 box with nginx, flask, gunicorn and port forwarding is not working as expected
In the vagrant file, I have:
config.vm.network "forwarded_port", guest: 8090, host: 3100
config.vm.network "private_network", ip: "10.10.1.10"
On the box, I have run:
gunicorn myprj:app -b 10.10.1.10:8090
find nothing on host machine with http://10.10.1.10:3100
trying curl -v http://10.10.1.10:3100 gives the following output:
connect to 10.10.1.10 port 3100 failed: Connection refused
Reachable with guest port on host machine http://10.10.1.10:8090
I am new to the vagrant, did I miss/mess to Vagrant file.
Complete Vagrant file:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.box_check_update = false
config.vm.network "forwarded_port", guest: 8090, host: 3100
config.vm.network "private_network", ip: "10.10.1.10"
config.vm.synced_folder ".", "/vagrant_data"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "lopamudra_dev"
end
config.vm.provision "shell", inline: <<-SHELL
# Upgrading the environment
apt-get update
apt-get upgrade
# Installing nginx + uwsgi + flask
apt-get install -y python-pip python-dev nginx
pip install uwsgi flask gunicorn
SHELL
end
You are forwarding container 8090 to hostIP:3100 (localhost:3100)
At the same time you are creating a IP to access your container with 10.10.1.10 and you container has 8090 exposed, thats why you can acces it on 10.10.1.10:8090
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
config.vm.network "forwarded_port", guest: 8090, host: 3100
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "10.10.1.10"

Vagrant Machines - Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001

I am currently setting up a development environment whereby there are two vagrant machines. The first hosts a website etc while the second hosts an API which the website calls. Currently my vagrantfiles are as follows:
API
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3001, host: 3001
end
Website
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
end
When i attempt to load the site which makes a call to the API I get:
Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001 (Connection refused - connect(2) for "localhost" port 3001)):
I understand there are multi-machine approaches but as I already have an seperate environment set up for each project I was wondering if there is any way for me to allow the connection between the two. I have looked into changing the hosts file on the vagrant machine hosting the site but not sure what I should do.
You need to put both VMs into the same private network and Website VM will be able to reach API VM via it:
config.vm.network "private_network", ip: "<ip_address>"
For example, for your case:
API VM:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3001, host: 3001
config.vm.network "private_network", ip: "192.168.200.3"
end
Website VM:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "private_network", ip: "192.168.200.2"
end
Now, Website VM can reach API VM on 192.168.200.3:3001. And of course you can reach Website VM on 127.0.0.1:3000 and API VM on 127.0.0.1:3001 from your host machine.

Files created in Vagrant centos/7 do not appear in Windows [duplicate]

This question already has answers here:
Vagrant Synced Folder without reload?
(2 answers)
Closed 5 years ago.
I have primarily used Ubuntu boxes in Vagrant and in those any files created within the box have been successfully synced to Windows also. Usually things like git pull.
However, I now have centos/7 and cannot get any files to sync to Windows. If you create a file in /vagrant it never appears anywhere in Windows - not under C:\cygwin64\ nor C:\vm\machine. When I vagrant reload the box, all the files created inside the box are gone, except configuration files eg. /etc/httpd/conf/httpd.conf. Any changes in them are still present upon vagrant reload and also vagrant halt; vagrant up.
Here's the Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 8086, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 3306, host: 3386, host_ip: "127.0.0.1"
config.vm.synced_folder "./", "/vagrant"
# I have also tried the following:
# config.vm.synced_folder "C:/vm/machine", "/vagrant"
# config.vm.synced_folder "./", "/vagrant", type: "rsync", disabled: false
end
When doing vagrant up, both methods for synced_folder display this:
==> default: Rsyncing folder: /cygdrive/c/vm/machine/ => /vagrant
I have tried doing vagrant up from both cmd.exe and also from the Cygwin64 Terminal (under /c/vm/machine). Neither of them create files to the Windows directory, which is in C:\vm\machine.
What do I need to do to make files created in the box also sync to Windows?
When I try the same in an Ubuntu box, the file appears in Windows and does not disappear:
This is the Vagrantfile of the Ubuntu box:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "v0rtex/xenial64"
config.vm.network "forwarded_port", guest: 80, host: 8083, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 443, host: 8443, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 3306, host: 3303, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 5000, host: 5003, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 8000, host: 8003, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 9090, host: 9093, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 9091, host: 9193, host_ip: "127.0.0.1"
end
Upon vagrant up for the Ubuntu box, I see this:
==> default: Mounting shared folders...
default: /vagrant => C:/vm/ubu1604-64
The solution is as follows:
Change the Vagrantfile synced_folder part to this:
config.vm.synced_folder "./", "/vagrant", type: "virtualbox", disabled: false
And then run this command in the directory where the Vagrantfile is:
vagrant plugin install vagrant-vbguest
Then do either vagrant reload if it was already running, or vagrant up otherwise.
It will run for quite a while - for me it took about 7 minutes altogether. However, it is a one-time install, so all future vagrant up's will take a normal amount of time.
After that, the files created in the /vagrant directory will also appear in Windows C:\vm\machine (for example) and will persist between Vagrant sessions, ie. vagrant halt + vagrant up

Switch from single machine to multiple in Vagrant without losing the original

I have the following vagrant VM:
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "forwarded_port", guest: 8000, host: 8080
config.vm.network "forwarded_port", guest: 8001, host: 8081
config.vm.synced_folder "../..", "/home/edgleweb"
config.vm.provider "virtualbox" do |vb|
vb.memory = "4000"
vb.cpus = 2
end
config.vm.provision "shell", path: "./provisioner.sh", args: "edgleweb" #, run: "always"
end
I am having some issues with my VM which I use for development. I would like to spin another exact copy of it after a box update. I don't want to copy and paste the folder as that is against DRY principle. Vagrant documentation calls for a multi-machine setup as follows:
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: "echo Hello"
config.vm.define "web" do |web|
web.vm.box = "apache"
end
config.vm.define "db" do |db|
db.vm.box = "mysql"
end
end
But I do want to keep my initial VM intact until I fully test the new one. I don't want to lose it. It is not apparent from the documentation how I keep it. How do I keep it?
You could use the vagrant snapshot command with either the save or push sub-commands to achieve this. See the documentation here.

Vagrant cURL works but Mozilla doesn't

I have the following issue. I just configured Vagrant box on Windows 7. This is my first time to use Vagrant. Guest port 80, host 8008(8080 is in use). It's all working well but http://localhost:8008/ in Mozilla dosen't respond in any way. From the virtual mashine curl 'http://localhost:80' works as expected. From the local mashine curl -v "http://localhost:8008/"as expected too. I tryed different browsers and firewall off - notting. Restart win, restart browser, clear Mozilla cash - all the same. The server is Ubuntu.
This is the vagrantfile:
Vagrant.configure("2") do |config|
config.ssh.forward_agent = true
config.vm.box = "ubuntu/trusty64"
config.vm.network :private_network, ip: "192.168.50.4"
config.vm.network "forwarded_port", guest: 80, host: 8008
config.vm.provision :shell, :path => File.join( "provision", "provision.sh")
end
I'm probably doing something wrong but I'm not sure what. Please for any suggestions what that may be.
you're using both private IP and forward port - use one or the other
Using forwarded port
Vagrant.configure("2") do |config|
config.ssh.forward_agent = true
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 8008
config.vm.provision :shell, :path => File.join( "provision", "provision.sh")
end
and from your host you will be able to access http://localhost:8008/
using private IP
Vagrant.configure("2") do |config|
config.ssh.forward_agent = true
config.vm.box = "ubuntu/trusty64"
config.vm.network :private_network, ip: "192.168.50.4"
config.vm.provision :shell, :path => File.join( "provision", "provision.sh")
end
and from your host you will be able to access http://192.168.50.4/

Resources