Trying to set authorized keys based on ssh-keygen

68 views
Skip to first unread message

Nimon Ambient

unread,
Jul 31, 2015, 10:58:22 AM7/31/15
to Vagrant
Hi guys,

I'm still new to vagrant and I'm trying to pull up a jenkins, svn instance and a couple of clients and that's all fine but I'm then trying to push the jenkins root ssh key out so it can connect to the other boxes pretty much straight away, and that's where I'm failing.

I'm trying to set a variable depending on the output of a cat inside the box and it doesn't work, the other boxes just get a blank authorized_keys. I'm sure there's a way to do this but I can't see it. I tried putting a "puts" at the end to print the variable but vagrant doesn't seem to run a puts like standard ruby would.

Here's my vagrantfile. I've bolded the non-working bits. Thanks in advance.

# -*- mode: ruby -*-
# vi: set ft=ruby :
#
SUBNET="10.0.10"
#

vmcount=1
Vagrant.configure(2) do |config|
  jenkins = "#{SUBNET}.100"
  subversion = "#{SUBNET}.150"

# Jenkins box
$jenkinsInstall= <<SCRIPT
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update -y
sudo apt-get install jenkins -y
sudo ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -q -N ""
systemctl stop jenkins
systemctl start jenkins
SCRIPT

  config.vm.define "jenkins_server" do |j|
    j.vm.provider "virtualbox" do |v|
        v.name = "Jenkins"
    end
    j.vm.box = "mokote/debian-8"
    j.vm.network "private_network", ip: "#{jenkins}"
    j.vm.network :forwarded_port, guest: 8080, host: 8082
    j.vm.hostname = "jenkins.keef.local"
    j.vm.provision "shell", inline: $jenkinsInstall
    $ssh_pub_key = j.vm.provision "shell", inline: "sudo cat /root/.ssh/id_rsa.pub"
  end

# subversion box
$subversionInstall= <<SCRIPT
sudo apt-get install subversion -y
sudo mkdir /svn-repo
sudo chmod 777 /svn-repo
sudo svnadmin create /svn-repo
sudo svnserve -d
sudo mkdir /root/.ssh/
SCRIPT

  config.vm.define "subversion_server" do |s|
    s.vm.provider "virtualbox" do |v|
        v.name = "Subversion"
    end
    s.vm.box = "mokote/debian-8"
    s.vm.network "private_network", ip: "#{subversion}"
    s.vm.network :forwarded_port, guest: 3690, host: 3690
    s.vm.hostname = "subversion.keef.local"
    s.vm.provision "shell", inline: $subversionInstall
    s.vm.provision "shell", inline: "sudo echo #{$ssh_pub_key} >> /root/.ssh/authorized_keys"
  end

# generic clients
$clientInstall= <<SCRIPT
sudo yum install -y subversion ruby
sudo mkdir /root/.ssh/
SCRIPT

(1..vmcount.to_i).each do |host|
  hosts = "node#{host}.keef.local"
  config.vm.define hosts do |cl|
    cl.vm.provider "virtualbox" do |v|
        v.name = "client#{host}"
    end
    cl.vm.box = "chef/centos-7.0"
    cl.vm.hostname = hosts
    cl.vm.network "private_network", ip: "#{SUBNET}.#{host + 10}"
    cl.vm.provision "shell", inline: $clientInstall
    cl.vm.provision "shell", inline: "sudo echo #{$ssh_pub_key} >> /root/.ssh/authorized_keys"
  end
end
end

Alvaro Miranda Aguilera

unread,
Jul 31, 2015, 9:32:08 PM7/31/15
to vagra...@googlegroups.com
What you can do and is something like this:


j.vm.provision "shell", inline: "sudo cat /root/.ssh/id_rsa.pub >
/vagrant/j.id_rsa.pub"

That will create a copy of the pub rsa in the vagrant folder, that is
shared between all the gue4st and host

Then you can use

s.vm.provision "shell", inline: "sudo cat /vagrant/j.id_rsa.pub |
sudo tee -a /root/.ssh/authorized_keys"

sudo tee -a should be used instead of `>>`

sudo tee should be used instead of `>`

Hope this helps
> --
> This mailing list is governed under the HashiCorp Community Guidelines -
> https://www.hashicorp.com/community-guidelines.html. Behavior in violation
> of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/mitchellh/vagrant/issues
> IRC: #vagrant on Freenode
> ---
> You received this message because you are subscribed to the Google Groups
> "Vagrant" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to vagrant-up+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/vagrant-up/e3f77c35-ed98-418f-bcae-4f38310c4f6c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nimon Ambient

unread,
Aug 1, 2015, 3:50:03 AM8/1/15
to Vagrant
Thankyou! Of course, I should have thought of /vagrant!!
Reply all
Reply to author
Forward
0 new messages