无法通过Vagrant中的ssh连接到主机(Ansible)

时间:2018-05-07 19:57:36

标签: ubuntu vagrant ansible

想在本地设置网站,拥有Ubuntu 18.04(Vargant 2.1,VM 5.2,Ansible) Vagrant文​​件包含...

Copy

运行vagrant up(provision)并在控制台中收到该错误。

# -*- 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.box = "ubuntu/xenial64"
  config.vm.box_check_update = false

  config.vm.define "web3" do |web3|
  end
  config.vm.network "private_network", ip: "192.168.56.56"

  # share/www
  config.vm.synced_folder "share/www", "/var/www", id: "vagrant-www",
    :create        => true,
    :owner         => "vagrant",
    :group         => "vagrant",
    :mount_options => ["dmode=775,fmode=775"]

  config.vm.provider "virtualbox" do |vb|
    vb.name   = "web3"
    vb.memory = 4096
    vb.cpus   = 2
    vb.customize ["modifyvm", :id, "--cpuexecutioncap", "90"]
    vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4" ]
    vb.customize ["modifyvm", :id, "--uartmode1", "file", "console.log" ]
  end

  config.vbguest.auto_update = false

  config.vm.provision "shell", inline: <<-SHELL
     echo "ubuntu:ubuntu" | sudo chpasswd
  SHELL

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provision/playbook.yml"
    ansible.inventory_path = "provision/vagrant_ansible_inventory"
    ansible.sudo = true
    ansible.host_key_checking = false

    if ENV.has_key?('verbose')
      ansible.verbose = ENV['verbose']
    end

    ansible.extra_vars = { 
      ansible_ssh_user:   'ubuntu', 
      ansible_connection: 'ssh',
      ansible_ssh_args:   '-o ForwardAgent=yes'
    }

    ansible.raw_arguments = [
      "--private-key=.vagrant/machines/web3/virtualbox/private_key"
    ]

    if ENV.has_key?('tags')
      ansible.tags = ENV['tags'].split(',')
    end
  end
end

我应该在vargant文件中写什么来避免这个错误? 使用SSH是必要的,没有它可以连接吗?

另一台机器上出现同样的错误(ubuntu 16.04) 满堆

TASK [install python2] ********************************************************* fatal: [web3]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.56.56' (ECDSA) to the list of known hosts.\r\nalex@192.168.56.56: Permission denied (publickey).\r\n", "unreachable": true} to retry, use: --limit @/home/alex/Projects/vm/provision/playbook.retry PLAY RECAP ********************************************************************* web3 : ok=0 changed=0 unreachable=1 failed=0

1 个答案:

答案 0 :(得分:0)

我在Ansible的Vagrants中做的是:

 "PostgreSQL 8.2.15 (Greenplum Database 4.3.18.0 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 22 2017 18:54:31"

然后是脚本:

hostsfile.sh:

       required_plugins = %w( vagrant-hostsupdater )
       machine.vm.synced_folder "./", "/vagrant"

       machine.vm.provision "shell", inline: <<-SHELL
        sudo su
        rm -f /etc/localtime && ln -s /usr/share/zoneinfo/UTC /etc/localtime
        echo 'Adding `sudo su` to the `.bashrc` for `vagrant` user...'
        echo 'sudo su' >> /home/vagrant/.bashrc
        echo "Settings the hosts file and Ansible access..."
        bash /vagrant/scripts/hostsfile.sh
        bash /vagrant/scripts/ansible_ssh_key.sh
        echo "Done..."
      SHELL

ansible_ssh_key.sh:

cp /vagrant/files/hostsfile /etc/hosts

文件/ hosts文件

sudo su
USER="ansible"
HOME_DIR="/home/$USER"
useradd -m $USER
usermod -aG sudo $USER
passwd -d $USER
mkdir $HOME_DIR/.ssh
echo 'yourprivkeyhere' >  $HOME_DIR/.ssh/authorized_keys
chown -R $USER:root $HOME_DIR/.ssh
chmod 400  $HOME_DIR/.ssh/authorized_keys
sed -i 's/^#\s*\(%sudo\s*ALL=(ALL)\s*NOPASSWD:\s*ALL\)/\1/' /etc/sudoers

使用此配置,您将能够连接到您的盒子

当然,您应该在127.0.0.1 localhost 172.10.10.11 centos7-1.vagrant.local centos7-1

中添加类似的内容
~/.ssh/config