Ansible ad-hoc是一种通过命令行批量管理的方式
格式:ansible 主机集合 -m 模块名 -a "参数"
其他参数: -k 使用密码远程、-i 指定主机列表文件
以上是调用ping模块,模块就是脚本(多数为Python脚本),多数脚本都支持参数,默认模块是command
command 模块(默认)
也可以是这样
执行以下命令可以列出ansible的所有模块
# 查看ansible有哪些模块
ansible-doc -l
# 过滤指定模块
ansible-doc -l | grep yum
# 查看模块的帮助信息
ansible-doc yum
# ansible 模块总数
[root@node01 ansible]# ansible-doc -l | wc -l
3387
shell 模块
command和shell模块的区别
- command模块的命令不启动shell,直接通过ssh执行命令
- command不支持bash的特性,如管道和重定向等功能
- 所有需要调用shell的功能都无法实现
使用场景
首先,ansible.cfg和hosts是这样的......
[root@node01 ansible]# cat ansible.cfg
[defaults]
inventory = ~/ansible/hosts
#forks = 5
#ask_pass = True
#remote_port = 22
#host_key_checking = False
[root@node01 ansible]# cat hosts
[master]
node01
[agent]
node02
node03
node04
[master:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_become_pass=123456
[agent:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_become_pass=123456
[cluster:children]
master
agent
1、查看agent组的内核版本
2、查看agent组防火墙运行状态
3、查看嵌套主机组cluster(即集群所有服务器)的network服务运行状态