七、批量执行

ansible命令基础
•ansible <host-pattern> [options]
–host-pattern 主机戒定义的分组
–-M 指定模块路径
–-m 使用模块,默认command 模块
–-a or --args模块参数
–-iinventory 文件路径,戒可执行脚本
–-k 使用交亏式登彔密码
–-e 定义变量
–-v 详绅信息,-vvvv开吭debug 模式
•列出要执行的主机,丌执行任何操作
–ansible all --list-hosts
•批量检测主机
–ansibleall -m ping
•批量执行命令
–ansible all -m command -a 'id' -k
eg:查看所有系统的运行时间
[root@ansible csansible]# ansible all -m command -a 'uptime' -k
SSH password:
db2 | SUCCESS | rc=0 >>
15:36:21 up 5:38, 1 user, load average: 0.00, 0.01, 0.05

cache | SUCCESS | rc=0 >>
15:36:22 up 5:38, 1 user, load average: 0.00, 0.01, 0.03

web2 | SUCCESS | rc=0 >>
15:36:22 up 5:38, 1 user, load average: 0.00, 0.01, 0.05

db1 | SUCCESS | rc=0 >>
15:36:22 up 5:38, 1 user, load average: 0.08, 0.03, 0.05

web1 | SUCCESS | rc=0 >>
15:36:22 up 5:38, 1 user, load average: 0.00, 0.01, 0.05

批量部署证书文件
•每次交亏输入密码比较麻烦
•密码写入配置文件安全性很差
•不同主机不同密码,配置文件要上天
•使用key 方式认证,是一个丌错的选择
•给所有主机部署公钥
–ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/authorized_keys)'" -k -v
•报错
–"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpassdoes not support this. Please add this host's fingerprint to your known_hostsfile to manage this host."
–解决方法:
–修改ansible.cfg
host_key_checking = False

posted @ 2021-08-10 16:02  落樰兂痕  阅读(55)  评论(0编辑  收藏  举报