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