设置密码

一、主控机安装ansible 

  yum install -y ansible

二、主控机生成秘钥对并给所有主机部署密钥

[root@ansible ~]# cd /root/.ssh/
[root@ansible .ssh]# ssh-keygen -t rsa -b 2048 -N '' 
[root@ansible .ssh]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k

ssh-keyscan host_name ip >>  ~/.ssh/known_hosts

三、主控机编写ansible文件

  3.1 取消每次敲命令输入密码参数 

    echo "export ANSIBLE_HOST_KEY_CHECKING=False" >>/etc/profile

    source /etc/profile

  3.2 添加 /etc/ansible/key.yaml文件

[root@ray ansible]# ansible-doc authorized_key
---
- hosts: all
  tasks:
    - name: Non secret authentication
      authorized_key: user=root key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"  state=present

    3.3 /etc/ansible/hosts文件如下,将密码一同写入

[ssh]
192.168.228.[126:170]
[ssh:vars]
ansible_ssh_pass="123456" #这个key是ansible默认的

  四、执行命令

  # ansible-playbook  /etc/ansible/key.yml

五、测试是否成功

  ssh 10.22.0.186

inventory内置参数,如下:

复制代码
ansible_ssh_host # 要连接的主机名
ansible_ssh_port # 端口号默认是22
ansible_ssh_user # ssh连接时默认使用的用户名
ansible_ssh_pass # ssh连接时的密码
ansible_sudo_pass # 使用sudo连接用户是的密码
ansible_ssh_private_key_file # 秘钥文件如果不想使用ssh-agent管理时可以使用此选项
ansible_shell_type # shell的类型默认sh
ansible_connection # SSH 连接的类型: local , ssh , paramiko在 ansible 1.2 之前默认是 paramiko ,后来智能选择,优先使用基于 ControlPersist 的 ssh (支持的前提)
ansible_python _ interpreter #用来指定 python 解释器的路径,同样可以指定ruby 、perl 的路径
复制代码
原文地址:https://www.cnblogs.com/ray-mmss/p/10289409.html