我正在设置一个 Vagrantbox 作为我的一台物理机的测试虚拟机。我在那里测试我的 Ansible 脚本。到目前为止,效果很好。
我的Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
IMAGE_NAME = "ubuntu/jammy64" # jammy = 22.04
HOSTNAME = "caprica-test"
VAGRANT_COMMAND = ARGV[0]
Vagrant.configure(2) do |config|
config.vm.box = IMAGE_NAME
config.vm.hostname = HOSTNAME
config.vm.network :private_network, type: "dhcp"
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
v.customize ["modifyvm", :id, "--groups", "/vagrantboxes"]
v.name = HOSTNAME
end
config.vm.provision "ansible" do |ansible|
ansible.verbose = "v"
ansible.playbook = "../../../main/workstations/caprica/provision/ansible-playbook.yml"
end
# Don't generate SSH keys -> don't override the keys provisioned by ansible
config.ssh.insert_key = false
config.ssh.private_key_path = ["~/.vagrant.d/insecure_private_key", "~/.ssh/id_rsa"]
# provision box as user "vagrant", but connect as different user (when running `vagrant ssh`)
if VAGRANT_COMMAND == "ssh"
config.ssh.username = 'starbuck'
end
end
现在我想验证我的 Ansible 设置是否正确,是否符合我的要求。为此,我想使用 Chef InSpec 并在配置后运行一些检查。我的问题是我无法连接到 Vagrantbox,因为名称解析不起作用。
docker run -it --rm \
--volume "$HOME/.ssh/known_hosts:/home/starbuck/.ssh/known_hosts:ro" \
--volume "$SSH_AUTH_SOCK:$SSH_AUTH_SOCK" \
--volume "$(pwd):$(pwd)" \
--workdir "$(pwd)" \
chef/inspec:latest exec inspec-tests --target="ssh://starbuck@caprica-test" --key-files="$HOME/.ssh/id_rsa" --chef-license=accept
我总是收到连接错误。我安装了 SSH_AUTH_SOCK,以确保 Docker 容器使用来自主机的 SSH 信息和服务。
我尝试了一个基本方法ping -c 10 caprica-test
(没有docker),结果是ping: caprica-test: Temporary failure in name resolution
。所以我很确定我的主机无法解析主机名“caprica-test”。
我切换了 IP 地址的主机名并尝试再次连接,但总是收到这个错误,我不太明白,因为vagrant ssh
它运行正常。
I, [2022-07-05T21:23:39.829959 #1] INFO -- : [SSH] connection failed, retrying in 1 seconds (#<Net::SSH::AuthenticationFailed: Authentication failed for user [email protected]>)
I, [2022-07-05T21:23:40.866179 #1] INFO -- : [SSH] connection failed, retrying in 1 seconds (#<Net::SSH::AuthenticationFailed: Authentication failed for user [email protected]>)
I, [2022-07-05T21:23:41.905142 #1] INFO -- : [SSH] connection failed, retrying in 1 seconds (#<Net::SSH::AuthenticationFailed: Authentication failed for user [email protected]>)
I, [2022-07-05T21:23:42.944189 #1] INFO -- : [SSH] connection failed, retrying in 1 seconds (#<Net::SSH::AuthenticationFailed: Authentication failed for user [email protected]>)
W, [2022-07-05T21:23:44.005679 #1] WARN -- : [SSH] connection failed, terminating (#<Net::SSH::AuthenticationFailed: Authentication failed for user [email protected]>)
Transport error, can't connect to 'ssh' backend: SSH session could not be established
有没有办法将 DNS 功能添加到我的 Vagrant 设置中?我的主机正在运行 Ubuntu 22.04 LTS。
答案1
这实际上不是一个ssh
问题,而是一个如何设置(Ubuntu)本地 DNS 解析器并vagrant
使用解析器注册 VM 的问题。
最新版本的 Ubuntu 带有systemd
受控解析器(systemd-resolved
),它也可充当主机本地的缓存 DNS 服务器。
或者,有一些 vagrant-dns 插件可以满足您的需要。
虽然这不是一个完整的答案,但我希望这能为您指明正确的方向。