Skip to main content
  1. Tutorial/

Ansible: Failed to connect to the host via ssh: Permission denied

410 words·2 mins
Ansible
Table of Contents

Failed to connect to the host via ssh: Permission denied
#

需要设置免密通信, 通过ssh-keygen命令执行生成密钥对

[root@localhost ansible_quickstart]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3AVLahF3gHMO5XGLI1PKNccmcybA/aFHJbcbpfBDNNk root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|       .+*@o*o*o.|
|       .=X*XBB.=E|
|        B==@o.*  |
|       o +ooo  + |
|        S ..  .  |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+

在管理节点执行添加目标节点的SSH认证信息
#

ssh-copy-id root@IP 

目标节点主机的~/.ssh/目录下将会出现一个authorized_keys文件

测试
#

制定3个目标机器的IP地址

# vim inventory.ini 
[myhosts]
192.168.139.200
192.168.139.201
192.168.139.202

清单测试

ansible-inventory -i inventory.ini --list
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped",
            "myhosts"
        ]
    },
    "myhosts": {
        "hosts": [
            "192.168.139.200",
            "192.168.139.201",
            "192.168.139.202"
        ]
    }
}
ansible myhosts -m ping -i inventory.ini
192.168.139.201 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.139.202 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.139.200 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

playbook 测试

vim playbook.yaml 
- name: My first play
  hosts: myhosts
  tasks:
   - name: Ping my hosts
     ansible.builtin.ping:

   - name: Print message
     ansible.builtin.debug:
      msg: Hello world
ansible-playbook -i inventory.ini playbook.yaml
PLAY [My first play] ***************************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [192.168.139.201]
ok: [192.168.139.202]
ok: [192.168.139.200]

TASK [Ping my hosts] ***************************************************************************
ok: [192.168.139.202]
ok: [192.168.139.201]
ok: [192.168.139.200]

TASK [Print message] ***************************************************************************
ok: [192.168.139.200] => {
    "msg": "Hello world"
}
ok: [192.168.139.201] => {
    "msg": "Hello world"
}
ok: [192.168.139.202] => {
    "msg": "Hello world"
}

PLAY RECAP *************************************************************************************
192.168.139.200            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.139.201            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.139.202            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

查看playbook 的执行会影响的hosts

ansible-playbook playbook.yaml --list-hosts

输出

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: myhosts

playbook: playbook.yaml

  play #1 (myhosts): My first play	TAGS: []
    pattern: ['myhosts']
    hosts (0):
[root@localhost ansible_quickstart]# ansible-playbook -i inventory.ini playbook.yaml --list-hosts

playbook: playbook.yaml

  play #1 (myhosts): My first play	TAGS: []
    pattern: ['myhosts']
    hosts (3):
      192.168.139.201
      192.168.139.202
      192.168.139.200

‘ssh’ connection type error
#

{“msg”: “to use the ‘ssh’ connection type with passwords or pkcs11_provider, you must install the sshpass program”}

yum install sshpass

Related

Centos7 install python and pip
77 words·1 min
Python
从源代码编译安装 # 安装构建 Python 所需的依赖包
Centos7: 误删python2导致yum无法使用
219 words·2 mins
Linux
uninstall python # 优先卸载,确保环境干净
Centos7 换源&更新网络连接
151 words·1 min
Linux
Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64 # reset /etc/yum.
Go Version Manager (GVM)
88 words·1 min
Golang
GVM(Go Version Manager)是一款用于管理和切换不同Go语言版本的工具 Install # bash < <(curl -s -S -L https://raw.
PTY Command Usage
110 words·1 min
Golang
PTY command usage # Go执行python脚本,异步读取输出,但是无法实时生成输出信息,有些许延迟