【ansible 设置host为localhost,执行ping命令报错】
[eniq-s@localhost ansible]$ ansible all -m ping -i inventory
localhost | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
"unreachable": true
}
https://serverfault.com/questions/1085427/ansible-playbook-not-running-for-localhost-also-how-to-fix-this
【加 -k 交互式输入密码可以成功】
[eniq-s@localhost ansible]$ ansible all -k -m ping -i inventory
SSH password:
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
https://wenku.baidu.com/view/4a4a69f64a649b6648d7c1c708a1284ac85005a7.html?_wkts_=1677651890658&bdQuery=authorized_keys%E6%96%87%E4%BB%B6%E5%9C%A8%E5%93%AA
【在local生成公钥,导入到server上的~/.ssh/authorized_keys, 之后问题解决】
[eniq-s@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/eniq-s/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/eniq-s/.ssh/id_rsa.
Your public key has been saved in /home/eniq-s/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:E3Hamok7mfc4bxwEdLSfCxqnmau9rMKqzUryhDyrMHM eniq-s@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .o.+ |
| .* . |
| o.o |
| . =.. . |
| . S.o o |
|.. + O.. . |
|*+E. = *. .. |
|+Oo o =.+o |
|=+=. .oo*=o |
+----[SHA256]-----+
[eniq-s@localhost ~]$ ls -alt .ssh/
total 12
drwx------. 2 eniq-s eniq-s 57 Feb 28 23:38 .
-rw-------. 1 eniq-s eniq-s 1679 Feb 28 23:38 id_rsa
-rw-r--r--. 1 eniq-s eniq-s 410 Feb 28 23:38 id_rsa.pub
drwx------. 10 eniq-s eniq-s 252 Feb 28 22:44 ..
-rw-r--r--. 1 eniq-s eniq-s 171 Feb 28 20:36 known_hosts
[eniq-s@localhost ~]$
[eniq-s@localhost ~]$ pwd
/home/eniq-s
[eniq-s@localhost ~]$ cat >> .ssh/authorized_keys < .ssh/id_rsa.pub
[eniq-s@localhost ~]$ ansible all -m ping -i ./ansible/inventory
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}