Ansible命令格式
Ansible [主机组或主机名ip地址也可] -m 模块名 -a ‘指定模块的参数’
例如:
Ansible test -m shell -a ‘cp /etc/passwd /root’
复制所有test组里面的主机,/etc/passwd 到root下面
值得夸奖的是ansible支持在指定主机和主机组时支持通配符
如
Ansible *.amber.com -m copy -a ‘src=/opt/test.sh dest=/root/test.sh’
*.amber.com表示amber.com前面不管是什么的都将执行ansible所分发的指令。
常用模块介绍
Shell模块:
通过此模块可以使用bash中的命令 ls cp等
如 ansible 192.168.1.112 -m shell -a ’yum -y install bind bind-utils’
在指定的主机或主机组安装DNS服务
copy模块
同shell的scp差不多可以实现远程复制,常用来批量分发脚本文件等。
如:ansible test -m copy -a’src=/opt/test.sh dest=/root’
批量分发test.sh到指定的主机或主机组,src指定发送的脚本或者文件,dest指定发送到指定主机或主机组的具体位置。
Stat模块
可以查看主机户主机组的文件状态信息,uid gid MD5等
如:ansible test -m stat -a 'path=/opt/test.sh'
Cron模块
批量设置计划任务
如
ansible test -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/bin/ls /etc"
synchronize模块
使用synchronize时需要安装rsync远程同步。
推送数据,拉去数据,常用于数据同步,使用delete=yes以ansible为主
如:ansible test -m synchronize -a ‘mode=push delete=yes src=/aaa dest=/’
mode指定是推送数据还是从被控端拉去数据。Pull拉取数据。Push推送数据。