ssh를 통해 2개의 Vagrant 머신(Virtualbox Hypervisor)에 ansible을 어떻게 연결할 수 있나요?

ssh를 통해 2개의 Vagrant 머신(Virtualbox Hypervisor)에 ansible을 어떻게 연결할 수 있나요?

ansible 명령을 수행하기 위해 한 시스템을 다른 시스템과 SSH로 연결하고 싶습니다. CentOS7이 있고 OS 내부에서 Ubuntu16 VM에 연결하고 싶습니다.

내 Vagrantfile과 내가 시도한 것을 살펴보십시오.

Vagrant.configure("2") do |config|
  config.vm.provider "virtualbox" do |vb|
     vb.gui = false
     vb.memory = "4096"
     vb.cpus = "4"
  end

  config.vm.define "centos7" do |centos7|
      centos7.vm.box = "centos/7"
      centos7.vm.hostname = "centos-vm"
      centos7.vm.network "public_network", ip: "192.168.0.105"
      #centos7.vm.network "forwarded_port", guest: 8080, host: 8888
      #centos7.vm.network "forwarded_port", guest: 22, host: 2222
      centos7.vm.provision "shell", run: "always", inline: <<-SHELL
         sudo route add default gw 192.168.0.1
         sudo yum update
         sudo yum -y install wget ntpdate net-tools nano firewalld telnet

         # Install java
         sudo yum -y install epel-release
         sudo yum install java-1.8.0-openjdk-devel
         java -version

         # isntall jenkins
         curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo
         sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp

         # Install ansible
         sudo yum -y install ansible

         # Install docker
         curl -fsSL https://get.docker.com/ | sh
         sudo usermod -aG docker $USER
         sudo systemctl start docker
         sudo systemctl status docker
         sudo systemctl enable docker
         sudo ps -ef | grep dockerd
      SHELL
  end

  config.vm.define "ubuntu16" do |ubuntu16|
      ubuntu16.vm.box = "ubuntu/xenial64"
      ubuntu16.vm.hostname = "ubuntu-vm"
      ubuntu16.vm.network "public_network", ip: "192.168.0.106"
      ubuntu16.vm.provision "shell", run: "always", inline: <<-SHELL
         sudo route add default gw 192.168.0.1
         # isntall python with virtuaenv
         sudo apt-get update
         sudo apt-get install build-essential libssl-dev libffi-dev python-dev
         sudo apt install python3-pip
         sudo pip3 install virtualenv
         virtualenv -p python3 env3
         . env3/bin/activate # or source env3/bin/activate which does exactly the same thing
         # you can make sure you are now working with Python 3
         python -- version
         which python
         deactivate
         sudo apt install wget ntpdate net-tools nano default-jdk -y
         sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common telnet -y
      SHELL
  end
end

SSH로 연결하려고 하면 다음 오류가 발생합니다.

[root@centos-vm vagrant]# ansible -m ping all
192.168.0.105 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).",
    "unreachable": true
}
192.168.0.106 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey).",
    "unreachable": true
}
[root@centos-vm vagrant]# ssh 192.168.0.106
Permission denied (publickey).
[root@centos-vm vagrant]# cat /etc/ansible/hosts

[servers]
192.168.0.105
192.168.0.106

vagrant@ubuntu-vm:~/.ssh$ telnet 192.168.0.105 22
Trying 192.168.0.105...
Connected to 192.168.0.105.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
^C^C^C
Connection closed by foreign host.

[vagrant@centos-vm .ssh]$ telnet 192.168.0.106
Trying 192.168.0.106...
Connected to 192.168.0.106.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
^C^C^C
Connection closed by foreign host.

Centos VM에서 수행해야 할 설정이 있습니까? 또는 원본 Vagrantfile의 일부 기본 구성을 허용해야 하는 경우. 문서를 직접 공부했습니다.

업데이트됨: Debian 10 상자가 repo에서 패키지를 가져올 때 많은 오류가 발생했기 때문에 두 번째 배포판을 Ubuntu 16 LTS로 변경했습니다.

답변1

두 서버 공개 키 모두에 대해 발생한 문제를 해결했습니다.

두 대의 서버에 대해 수행했습니다.

ssh-keygen -t RSA

So I connect Centos 7 to Ubuntu 16:
[vagrant@centos-vm .ssh]$ ssh 192.168.0.106
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-173-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


7 packages can be updated.
7 updates are security updates.

New release '18.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Sat Feb 15 20:42:20 2020 from 192.168.0.105
vagrant@ubuntu-vm:~$

vagrant@ubuntu-vm:~/.ssh$ ssh 192.168.0.105
Last login: Sat Feb 15 20:37:28 2020 from 192.168.0.106
[vagrant@centos-vm ~]$

그리고 마지막으로 Ansible을 테스트했는데 잘 작동했습니다!

[vagrant@centos-vm .ssh]$ ansible -m ping all

두 번째 오류는 ansible이 동일한 호스트에 대해 ssh를 수행하려고 하기 때문이라고 생각합니다.

[vagrant@centos-vm .ssh]$ ansible -m ping all
The authenticity of host '192.168.0.105 (192.168.0.105)' can't be established.
ECDSA key fingerprint is SHA256:Dydts4hM79oPtDfElLzavhMw2SrNwu19YOcpd7bAUe0.
ECDSA key fingerprint is MD5:b1:2f:58:d0:59:28:a1:ab:a4:e4:9d:d7:b2:9c:b2:49.
Are you sure you want to continue connecting (yes/no)? 192.168.0.106 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

192.168.0.105 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Host key verification failed.",
    "unreachable": true
}

관련 정보