1

I am using vagrant 1.6.3 version and a default linux box for vagrant.

My box is running fine, so in the vagrantfile i added to run an external shell script. The script contains this command.

custom.sh file

#!/bin/bash

DEPLOY_PASSWORD="xxxxx" &&
DB_NAME="test-sync.sql.gz" &&
apt-get install sshpass &&
sshpass -p "$DEPLOY_PASSWORD" scp -P 22 -v user@test01.admin.com:"~/mysql_dumps/$DB_NAME" "./"

I tried running the command manually and it works fine without any issues but it doesn't work from a bash script. This is the Error Log i get by running vagrant provision:

 The following SSH command responded with a non-zero exit status.
 Vagrant assumes that this means the command failed!
 chmod +x /tmp/vagrant-shell && /tmp/vagrant-shell test-sync.sql.gz

Vagrantfile

    Vagrant.configure("2") do |config|

  # Load config JSON.
  config_json = JSON.parse(File.read("config.json"))

  # Prepare base box.
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  # Configure networking.
  config.vm.network :private_network, ip: config_json["vm"]["ip"]

  # Configure forwarded ports.
  config.vm.network "forwarded_port", guest: 35729, host: 35729, protocol: "tcp", auto_correct: true
  config.vm.network "forwarded_port", guest: 8983, host: 8983, protocol: "tcp", auto_correct: true
  # User defined forwarded ports.
  config_json["vm"]["forwarded_ports"].each do |port|
    config.vm.network "forwarded_port", guest: port["guest_port"],
      host: port["host_port"], protocol: port["protocol"], auto_correct: true
  end

  # Customize provider.
  config.vm.provider :virtualbox do |vb|
    # RAM.
    vb.customize ["modifyvm", :id, "--memory", config_json["vm"]["memory"]]

    # Synced Folders.
    config_json["vm"]["synced_folders"].each do |folder|
      case folder["type"]
      when "nfs"
        config.vm.synced_folder folder["host_path"], folder["guest_path"], type: "nfs"
        # This uses uid and gid of the user that started vagrant.
        config.nfs.map_uid = Process.uid
        config.nfs.map_gid = Process.gid
      else
        config.vm.synced_folder folder["host_path"], folder["guest_path"]
      end
    end
  end

  # Run initial shell script.
  config.vm.provision :shell, :path => "chef/shell/initial.sh"

  # Customize provisioner.
  config.vm.provision :chef_solo do |chef|
    chef.json = config_json
    chef.custom_config_path = "chef/solo.rb"
    chef.cookbooks_path = ["chef/cookbooks/berks", "chef/cookbooks/core", "chef/cookbooks/custom"]
    chef.data_bags_path = "chef/data_bags"
    chef.roles_path = "chef/roles"
    chef.add_role "vdd"
  end

  # Run final shell script.
  config.vm.provision :shell, :path => "chef/shell/final.sh", :args => config_json["vm"]["ip"]
  #custom tasks
  config.vm.provision :shell, :path => "custom.sh", :args => config_json["test_config"]["db_name"]
end
РАВИ
  • 11,467
  • 6
  • 31
  • 40
  • To troubleshoot this, I would start with a simple script that just does something like `echo Hello`. Then you could isolate whether it's Vagrant or your script which is at fault. Could you also post your whole Vagrantfile to show how you're running the script? – BrianC Aug 13 '14 at 21:31
  • I tried echo Hello and it works as intended, only when it does any kind of ssh the script fails in vagrant. – РАВИ Aug 14 '14 at 00:27

2 Answers2

1

I don't use sshpass (the idea of having passwords in the clear in a script just makes me cringe), but the man-page seems to suggest that inside a script you should be setting the variable SSHPASS rather than using the -p incantation ...

EXAMPLES
   Run rsync over SSH using password authentication, passing the password on the command line:
   rsync --rsh='sshpass -p 12345 ssh -l test' host.example.com:path .
   To do the same from a bourne shell script in a marginally less exposed way:
   SSHPASS=12345 rsync --rsh='sshpass -e ssh -l test' host.example.com:path .
tink
  • 14,342
  • 4
  • 46
  • 50
  • again, there is nothing wrong with my statement, works fine as intended. It just wont work in vagrant. Please review my ? again. – РАВИ Aug 14 '14 at 00:39
  • 1
    "I tried running the command manually and it works fine without any issues but it doesn't work from a bash script." ... it would appear that my response is trying to address exactly that? /me shrugs – tink Aug 14 '14 at 00:42
  • Sry, still the same error with your method as well. What i was trying tell you is that, there might be some setting in vagrant that block scripts to run ssh commands rather than the code itself. – РАВИ Aug 14 '14 at 03:32
0

sshpass works only if the prompt ends with assword:. In your case it will be password for user@test01.admin.com because you specified the username.

See http://sourceforge.net/p/sshpass/code/HEAD/tree/trunk/main.c row 362

Why don't you use public key autentication instead?

Community
  • 1
  • 1
pqnet
  • 6,070
  • 1
  • 30
  • 51
  • There is nothing wrong with the command. It works fine when i copy it and paste it into the terminal and runs as it should. – РАВИ Aug 13 '14 at 23:21