Ansible ping模块的使用
环境:
Ansible端:192.168.178.14
被控节点:192.168.178.13
在Ansible端/etc/ansible/hosts文件内添加被控节点IP
ping模块,主要用于判断远程客户端是否在线,返回值为changed、ping。
[root@localhost ansible]# ansible -k 192.168.178.13 -m ping
SSH password:
192.168.178.13 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
上面报错的解决方法是:
先在ansible 服务器上使用ssh 登陆一下/etc/ansible/hosts 里面配置的服务器(也就是我们想要管理的服务器),然后再用ansible 去管理即可,步骤如下
先使用ssh登录一下想要远程操作的主机,解决了上面的报错,但是每次我们去远程操作一个主机都要用-k参数来手输入密码 ,很是麻烦,Ansible还有shell模块,可以远程执行shell,但是你每次手输密码,无法做到自动化。
还有一种方式常用方式: 秘钥认证,只需要一次秘钥认证,后续任何操作都不需要再输入密码。
(基于Ansible自动运维工具管理客户端案例操作,由于Ansible管理远程服务器基于SSH,在登录远程服务器执行命令时需要远程服务器的用户名和密码,也可以加入-k参数手动输入密码或者基于ssh-keygen生成免秘钥。)
2.Ansible 秘钥认证:
在Ansible端执行,生成秘钥对
生成密钥对
[root@localhost ansible]# ssh-keygen
在Ansible端执行,复制公钥到被控节点上
将生成的公钥复制到被控节点上
[root@localhost ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.178.13