rubyvagrantstring-concatenationprovisioningjuniper-network-connect

Strings concatenation in Ruby (script provisioning of a Junos vSRX virtual lab in Vagrant)


I have following command which is working :

my_host_shell.inline = 'vagrant ssh router3 -c "/usr/sbin/cli -f /tmp/provision01_router3.sh"'

("vagrant ssh" is launched in the host, connects to the Virtual Machine, where it feeds the "provision01_router3.sh" script (already located in the VM) into the cli interpreter).

Now I want the "router3" and "provision01_router3.sh" strings to be replaced by variables, as they will be different for each Virtual Machine. I tried many ways, including following, but none is working...

my_host_shell.inline = '"vagrant ssh " + node.vm.hostname +  " -c /usr/sbin/cli -f /tmp/" + script[:script_name]'

Can you please help debug this line?

Here the full Vagrantfile (see last lines): https://github.com/XiaoJu/junos_vagrant_virtual_lab/

VAGRANTFILE_API_VERSION = "2"
require "vagrant-host-shell"
require "vagrant-junos"
mynodes=[
    {
    :hostname => "router3",
    :box => "juniper/ffp-12.1X47-D15.4-packetmode",
    :ram => 512,
    :cpus => 2,
    :myscripts => [
        {
            :script_name => "provision01_router3.sh"
        },
        {
            :script_name => "provision02_all.sh"
        }
    ],
    :interfaces => [
        {
            :subnet_name => "3-to-6",
            :subnet_ip => "172.23.10.13"
        },
        {
            :subnet_name => "3-to-4",
            :subnet_ip => "172.23.3.13"
        },
        {
            :subnet_name => "exit3",
            :subnet_ip => "10.10.2.13"
        }
    ]
    } ]

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box_check_update = false
    mynodes.each do |machine|
        config.vm.define machine[:hostname] do |node|
            node.vm.box = machine[:box]
            node.vm.hostname = machine[:hostname]

            node.vm.provider "virtualbox" do |vb|
                vb.check_guest_additions = false
                vb.customize ["modifyvm", :id, "--memory", machine[:ram], "--cpus", machine[:cpus]]
            end

            # assign ip addresses to interfaces
            machine[:interfaces].each do |subnet|
                node.vm.network "private_network",
                            autoconfig: false,
                            ip: subnet[:subnet_ip],
                            virtualbox__intnet: subnet[:subnet_name]
            end

            # provision each node
            machine[:myscripts].each do |script|
                node.vm.provision "file",
                              source: script[:script_name], 
                              destination: '/tmp/' + script[:script_name]
                node.vm.provision :host_shell do |my_host_shell|
                              # my_host_shell.inline = 'vagrant ssh router3 -c "/usr/sbin/cli -f /tmp/provision01_router3.sh"'
                              my_host_shell.inline = '"vagrant ssh " + node.vm.hostname +  " -c /usr/sbin/cli -f /tmp/" + script[:script_name]'
                end
            end
        end
    end 
end

Solution

  • Use string interpolation; it will make your code much more readable:

    my_host_shell.inline = "vagrant ssh #{node.vm.hostname} -c \"/usr/sbin/cli -f /tmp/#{script[:script_name]}\""