ansible批量执行命令展示

ansible命令基础
• ansible <host-pattern> [options]
– host-pattern 主机戒定义的分组
– -M 指定模块路径
– -m 使用模块,默认 command 模块
– -a or --args 模块参数
– -i inventory 文件路径,戒可执行脚本
– -k 使用交亏式登彔密码
– -e 定义变量
– -v 详绅信息,-vvvv 开吭 debug 模式


• 列出要执行的主机,不执行任何操作
– ansible all --list-hosts
• 批量检测主机
– ansible all -m ping
• 批量执行命令
– ansible all -m command -a 'id' -k
批量部署证书文件
• 每次交亏输入密码比较麻烦
• 密码写入配置文件安全性很差
• 不同主机不同密码,配置文件要上天
• 使用 key 方式认证,是一个不错的选择
• 给所有主机部署公钥
– ansible all -m authorized_key -a "user=root
exclusive=true manage_dir=true key='$(<
/root/.ssh/authorized_keys)'" -k -v

举例说明:

批量检测主机
[root@ansible ~]# ansible web  -m ping

批量执行命令
[root@ansible ~]# ansible all -m command -a 'uptime'
[root@ansible ~]# ansible web  -m command -a 'uptime'
[root@ansible ~]# ansible web  -m command -a 'uptime' -k
SSH password: 
web1 | SUCCESS | rc=0 >>
 14:37:12 up  5:12,  3 users,  load average: 0.01, 0.03, 0.02

web2 | SUCCESS | rc=0 >>
 14:37:12 up  5:03,  2 users,  load average: 0.00, 0.01, 0.03

批量部署证书文件
[root@ansible ooxx]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/id_rsa.pub)'" -k  

注意:

/root/.ssh/id_rsa.pub是通过命令生成的ssh-keygen -t rsa并且在/root/.ssh/目录下可以看到的

猜你喜欢

转载自blog.csdn.net/zhydream77/article/details/81223805