ansible 使用密码登录

安装ansible

apt install ansible

1.安装sshpass
apt :

apt install sshpass

源码:

tar -zxvf sshpass-1.05.tar.gz
cd sshpass-1.05                
./configure             
make && make install

创建一个hosts文件,添加用户密码,认证ssh连接

[remote]
10.33.80.70
10.33.80.71

[remote:vars]
ansible_ssh_pass='passs'
ansible_ssh_user='test'

2.错误汇总:

ansible -i hosts test -m ping

10.33.80.71 | 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."
}

3.原因和解决办法:
ssh第一次连接的时候一般会提示输入yes 进行确认为将key字符串加入到 ~/.ssh/known_hosts 文件中。而本机的~/.ssh/known_hosts文件中并有fingerprint key串
解决方法:在ansible.cfg文件中更改下面的参数:

#host_key_checking = False 将#号去掉即可

执行shell 命令

# -m shell -a
ansible -i hosts remote -m shell -a  "netstat -anp|grep 80"
# -m raw -a 
ansible -i hosts remote -m raw  -a "ifconfig"

你可能感兴趣的:(ansible 使用密码登录)