Vagrant /etc/sysconfig/network and /etc/resolv.conf - puppet

I have the following Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.define "admin" , primary: true do |node|
node.vm.box = "centos-6.5-x86_64"
node.vm.hostname = "admin.example.com"
node.vm.network :private_network, ip: "10.10.10.10"
end
end
and I get the following:
$ vagrant up && vagrant ssh
$ cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=admin.example.com
$ cat /etc/resolv.conf
search example.com
nameserver 10.0.2.3
I need to have HOSTNAME=admin, instead of HOSTNAME=admin.example.com.
How to achieve that?
If I set node.vm.hostname = "admin",
then /etc/resolv.conf does not have search example.com.
I could add a vagrant shell provisioner to create myself the /etc/sysconfig/network and /etc/resolv.conf files, but it does not look nice (for instance, I would need to know the nameserver).
And what is the proper way to set also domain example.com in /etc/resolv.conf?

Add the following lines to your VagrantFile
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end

Related

How can I set up my desktop user inside a Linux Vagrant VM so that it behaves like a standard tools console with my usual username, ssh keys, etc.?

I am a Windows user who uses Linux a lot for development work.
In my company, developers' tastes for desktops vary (Mac, Windows, Arch Linux) but we use Vagrant VMs to make sure that everyone has a common environment for development.
One specific annoyance on Windows is the lack of Linux compatible tools. There are various ways around it, like msys, cygwin but nothing works better than a fully compatible tools console (Ubuntu 14.04 in our case).
So, I made a Vagrant VM for it but discovered that my standard login id, ssh keys inside C:/Users/devang/.ssh had to be created manually inside the VM.
Is there a standard way to build it in Vagrant?
There were some useful answers in stackoverflow related to configuration of the default vagrant user but in the end I had to make my own inline provisioner.
I am posting my solution here. It does not make any assumption about host OS but it does assume a Debian/Ubuntu Vagrant VM as the guest.
The key part is the inline shell provisioner which provides access to the variable Dir.home and ENV['USER'] from the host OS.
AFAIK, to have access to those variables from host OS, one has to use an inline provisioner.
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.hostname = "tools"
config.vm.provision "shell" do |s|
ssh_pub_key = File.open("#{Dir.home}/.ssh/id_rsa.pub", "rb").read
ssh_key = File.open("#{Dir.home}/.ssh/id_rsa", "rb").read
user_name = ENV['USER'].downcase
home_dir = "/home/#{user_name}"
s.inline = <<-SHELL
sudo useradd -d "#{home_dir}" -m "#{user_name}" -s /bin/bash
sudo chmod 755 "#{home_dir}"
sudo mkdir "#{home_dir}/.ssh"
sudo chmod 700 "#{home_dir}/.ssh"
sudo echo "#{ssh_pub_key}" > "#{home_dir}/.ssh/id_rsa.pub"
sudo echo "#{ssh_key}" > "#{home_dir}/.ssh/id_rsa"
sudo echo "#{ssh_pub_key}" > "#{home_dir}/.ssh/authorized_keys"
sudo chown -R "#{user_name}.#{user_name}" "#{home_dir}/.ssh"
sudo chmod -R 600 "#{home_dir}/.ssh/id_rsa"
sudo usermod -a -G sudo "#{user_name}"
echo "#{user_name} ALL=(ALL) NOPASSWD: ALL" | sudo cat > "/etc/sudoers.d/#{user_name}"
SHELL
end
config.vm.provision :shell, path: "contrib/build-server.sh"
config.vm.box = "debian/contrib-jessie64"
config.vm.network "private_network", ip: "192.168.70.150"
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
v.cpus = 4
end
end

I am asked for password of ubuntu user instead of vagrant (the user I'm logged in with in GUI)

I have a vagrant project, provisioned with ansible.
For ssh settings I use:
config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
The problem is that I start the VM with GUI
vb.gui = true
have installed with ansible lightdm-gtk-greeter-settings,
https://github.com/valentin-nasta/development-environment/blob/master/playbooks/roles/desktop/tasks/main.yml#L14
but I'm asked for ubuntu's user password instead of the vagrant user.
This is the problem I'm trying to solve.
As a workaround (as I didn't know the ubuntu's user password), I logged into the vm and change it manually.
vagrant ssh
sudo passwd ubuntu
But still I would like to know the secret behind.
Here is my Vagrantfile
https://github.com/valentin-nasta/development-environment/blob/master/Vagrantfile
It seems the vagrant user is not added to sudoers on the official Vagrant ubuntu/trusty box https://atlas.hashicorp.com/ubuntu/boxes/trusty64
The fix was to add these lines as inline shell provisioning:
# provision
config.vm.provision :shell, inline: <<-SHELL
# Set up sudo
echo 'vagrant ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/vagrant
chmod 0440 /etc/sudoers.d/vagrant
# Setup sudo to allow no-password for "sudo" commands
usermod -a -G sudo vagrant
SHELL

How can I customize the output while 'vagrant up' runs?

I want to inform the vagrant user what ip address the used machine has. My first idea was to use '/etc/rc.local' and print the output of ifconfig to a file in the '/vagrant' directory, but it seems this directory is mounted after rc.local is called. So I need another way to inform the user without any ssh login.
My second idea is to write the ifconfig output to some "place" where it is shown in the vagrant start up output ... like in sample below.
...
default: Guest Additions Version: 4.3.10
default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /home/user/vagrant/test
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
# start of desired output
Adresses found:
10.0.2.15
172.28.128.3
# end of desired output
==> default: flag to force provisioning. Provisioners marked to run always will still run.
...
All ideas are welcome.
You may be interested in this SO answer, which attempts to achieve the same thing of outputting the network interface's IP to terminal on vagrant up.
Relevant bits from the answer -
On the Guest:
/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}
Should get you something like this:
10.0.2.15
Which could then be used in your Vagrantfile like so:
config.vm.provision "shell", inline: <<-SHELL
sudo -i /vagrant/my_provisioning_script.sh $(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
SHELL
The trick here is knowing which interface (eth0 in the example above) has the IP you want. Of course if you are great with grep or awk you could modify that first command to check the IPs on all the interfaces... but that's beyond my abilities.
# content of Vagrantfile
$infoScript = <<SCRIPT
echo 'IP-addresses of the vm ...'
ifconfig | grep 'inet A' | grep Bcast | awk '{print $2}' | sed 's/addr://'
SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.box_check_update = false
config.vm.network "private_network", type: "dhcp"
config.vm.network "forwarded_port", guest: 80, host: 8888
config.vm.provider "virtualbox" do |vb|
vb.name = "demo"
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
vb.memory = "2048"
vb.cpus = 2
end
# normal provision to set up the vm
config.vm.provision "shell", path: "scripts/bootstrap.sh"
# extra provision to print all the ip's of the vm
config.vm.provision "shell", inline: $infoScript,
run: "always"
end
EDIT
You may also be interested in the vagrant-hostmanager plugin if the purpose of echoing the IP is just so you can connect to the box, this plugin can modify your /etc/hosts on your guest or host so you don't need to worry about the IP and instead use something like http://mydevbox.local
First you will need to determine how the IP address is acquired (i.e. DHCP or static). Then based on that you could essentially just add the private networking code to the vargrantfile as such:
DHCP:
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
Static:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
Then you could add a mixture of shell and ruby:
$script = <<SCRIPT
echo private_network...
ip: "192.168.50.4"
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end
Hope that helps enough to get you going.
Because the other answer don't describes the way how to publish dynamic data from the inside of the vm, I write my solution.
Step 1: I put a webserver in the vagrant vm and publish guest port 80 to host port 8888
Step 2: prepare /etc/rc.local of guest so it gathers all needed dynamic vm information and write the output to webserver root in a file 'info.txt'
Step 3: add a second provision entry to vagrantfile that runs every 'vagrant up' and informs the user where he can get more information
# Vagrantfile
...
config.vm.provision "shell", path: "scripts/bootstrap.sh"
config.vm.provision "shell", inline: "echo 'check http://localhost:8888/info.txt for more information'",
run: "always"
...

Configuring multi node hadoop install

Im trying to configure a hadoop - master and slave env.
http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-multi-node-cluster/
So far,
I have created 2 vagrant(Ubuntu) boxes and installed Hadoop in both the machines and up in running.
Now, I have assigned a new ipaddress - 192.168.0.1 to my master machine and trying to ssh to that machine but does not work.
ssh localhost - works
ssh master - does not work
127.0.0.1 localhost
127.0.1.1 vagrant
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
#hadoop
192.168.0.1 master
You need to follow following steps to create passwordless ssh passwordless login.
edit /etc/hosts in all the nodes.
Add master and slave
192.168.0.1 master
192.168.0.2 slave
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub user#slave
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
Now try ssh master and ssh slave
Did you configure the interface with that IP address? Also from which machine you are trying to ssh to that master IP. Where did you build these 2 VMs? on your PC? Post /sbin/ifconfig and netstat -rn output from both master and slave VMs.
Since you mentioned vagrant. This might help https://docs.vagrantup.com/v2/networking/index.html

What does hostname function and HOSTNAME variable actually do

I use hostname 'host' and domainname 'domain.com'
I set HOSTNAME env to 'user.linux'
I add test.testdns.com to 127.0.0.1 to /etc/hosts
Which one is really the hostname and domainname, and what does hostname command do actually?

Resources