ansible-for-devops
ansible-for-devops copied to clipboard
Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,password).",
OS: MacosX
ansible -m ping all
192.168.60.4 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,password).",
"unreachable": true
}
192.168.60.5 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,password).",
"unreachable": true
}
192.168.60.6 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,password).",
"unreachable": true
}
cat /etc/ansible/hosts
# Lines beginning with a # are comments and are only included for
# illustration. These comments are overkill for most inventory files.
# Application servers
[app]
192.168.60.4
192.168.60.5
# Database server
[db]
192.168.60.6
# Group 'multi' with all servers
[multi:children]
app
db
# Variables that will ve applied to all servers
[multi:vars]
ansible_ssh_user=vagrant
ansbile_ssh_private_key_file=/Users/florin/.vagrant.d/insecure_private_key
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
$ vagrant ssh-config
Host app1
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/florin/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
Host app2
HostName 127.0.0.1
User vagrant
Port 2201
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/florin/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
Host db
HostName 127.0.0.1
User vagrant
Port 2202
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/florin/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
~/three_servers cat Vagrantfile
# _*_ mode: ruby _*_
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# General Vagrant VM configuration
config.vm.box = "geerlingguy/centos7"
config.ssh.insert_key = false
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider :virtualbox do |v|
v.memory = 256
v.linked_clone = true
end
# Application server 1.
config.vm.define "app1" do |app|
app.vm.hostname = "orc-app1.test"
app.vm.network :private_network, ip: "192.168.60.4"
end
# Application server 2.
config.vm.define "app2" do |app|
app.vm.hostname = "orc-app2.test"
app.vm.network :private_network, ip: "192.168.60.5"
end
# Database server.
config.vm.define "db" do |db|
db.vm.hostname = "orc-db.test"
db.vm.network :private_network, ip: "192.168.60.6"
end
end
vagrant status
Current machine states:
app1 running (virtualbox)
app2 running (virtualbox)
db running (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
ansible --version
ansible 2.10.5
config file = None
configured module search path = ['/Users/florin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible
executable location = /Library/Frameworks/Python.framework/Versions/3.8/bin/ansible
python version = 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) [Clang 6.0 (clang-600.0.57)]
I can connect if I do
ssh -p 2200 -i /Users/florin/.vagrant.d/insecure_private_key [email protected]
But when I try
ssh -vvv -p 2200 -i /Users/florin/.vagrant.d/insecure_private_key [email protected]
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/florin/.ssh/config
debug1: /Users/florin/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug2: resolve_canonicalize: hostname 192.168.60.4 is address
debug2: ssh_connect_direct
debug1: Connecting to 192.168.60.4 [192.168.60.4] port 2200.
debug1: connect to address 192.168.60.4 port 2200: Connection refused
ssh: connect to host 192.168.60.4 port 2200: Connection refused
Was able to make it work, after reading this answer https://stackoverflow.com/a/35969858 It seems that I did not have an ansible.cfg file and this was creating this error.
wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
~/three_servers pwd
/Users/florin/three_servers
~/three_servers ls
Vagrantfile ansible.cfg
~/three_servers ansible -m ping multi
192.168.60.4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.60.6 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.60.5 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
If you didn't have the hostnames set to 127.0.0.1 I think it would have worked out without the ansible.cfg
Was able to make it work, after reading this answer https://stackoverflow.com/a/35969858 It seems that I did not have an ansible.cfg file and this was creating this error.
wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg ~/three_servers pwd /Users/florin/three_servers ~/three_servers ls Vagrantfile ansible.cfg
~/three_servers ansible -m ping multi 192.168.60.4 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.60.6 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.60.5 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
I tried ansible.cfg but still same error
[defaults]
inventory = hosts.ini
nocows = True
Went through this issue as well. If you're like me, you just need to pull down the latest code for this chapter.
If you're following an old version of the book, or the youtube playlist you'll want to update your Vagrantfile and inventory file (hosts.ini) to the latest published in this repo.
vagrant destroy your old machines and vagrant up the new ones, then
ansible multi -i hosts.ini -a "hostname"
and you should get the output you're expecting