首先,ansible 每个版本的模块的命令会有一定的区别,官方文档:http://docs.ansible.com/
中文文档:http://www.ansible.com.cn/ 是由马哥团队负责,他的视频教程也很经典
使用ansible-doc -s "模块名"查看帮助
“vim /etc/ansible/hosts”文件可以定义主机分组,方便灵活运维,也可以使用“all”参数制定所有
常用模块:
1.ping
检查指定节点机器是否还能连通,用法很简单,不涉及参数,主机如果在线,则回复pong
ansible all -m ping 检测所有主机
ansible web -m ping 检测web组主机
2.raw模块
执行原始的命令,而不是通过模块子系统。在任何情况下,使用shell或命令模块是合适的。给定原始的参数直接通过配置的远程shell运行。可返回标准输出、错误输出和返回代码。此模块没有变更处理程序支持。
这个模块不需要远程系统上的Python,就像脚本模块一样。此模块也支持Windows目标。
ansible web -m raw -a "echo hello"
说到此模块,此模块还有两个可以实现相同结果的模块(shell command),但是有区别的
ansible web -m command -a "echo hello"
ansible web -m shell -a "echo hello"
ansible 192.168.3.129 -m raw -a "/tmp/zabbix.sh"执行脚本
ansible web -m shell -a "ps -ef |wc -l"
3.yum 模块
这个模块是RedHat / CentOS作为远端节点的OS的时候,用的最多的。一个基于python编写的RedHat / CentOS包管理工具
使用`yum’软件包管理器管理软件包,其选项有:
– config_file:yum的配置文件 (optional)
– disable_gpg_check:关闭gpg_check (optional)
– disablerepo:不启用某个源 (optional)
– enablerepo:启用某个源(optional)
– name:要进行操作的软件包的名字,默认最新的程序包,指明要安装的程序包,可以带上版本号,也可以传递一个url或者一个本地的rpm包的路径
– state:状态(present,absent,latest),表示是安装还卸载
present:默认的,表示为安装
lastest: 安装为最新的版本
absent:表示删除
ansible web -m yum -a 'name="@Development tools" state=lastest' 安装开发工具包
ansible web -m shell -a "rpm -qa Development tools" 查询安装情况
ansible web -m yum -a "name=httpd state=latest" 安装最新的http
ansible web -m shell -a "rpm -qa |grep httpd"
查询 httpd包
ansible web -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present' 安装nginx
4.copy模块
使用copy模块,可以实现向目标机器进行远程copy的能力

default的情况下,force是yes的,所以什么都不写,文件存在的情况是会被覆盖的
ansible web -m copy -a "src=1.sh dest=/opt backup=no"
如提示以下报错,则在被控制节点(非安装有ansible服务器上)执行“yum install libselinux-python -y”
Aborting, target uses selinux but python bindings (libselinux-python) aren
5.script 模块
script模块 ,在远程主机执行主控端的shell/python脚本。也就是说 script模块实现了将主控节点的脚本复制到远程节点,然后在远程节点执行脚本
ansible web -m script -a "5.sh"
6.setup 模块
setup模块,用于收集对象机器的基本设定信息
(1)不用option的情况会输出所有相关的对象机器的facts
ansible web -m setup
(2)etup常用Option:filter
比如收集对象机器的环境变量信息
ansible web -m setup -a "ansible_env"
7.user group 模块
user模块,用于管理用户
group模块,用于管理group
ansible web -m shell -a "id test" 查询
ansible web -m user -a 'name=xw state=present system=yes uid=200 groups=root,ntp shell=/bin/bash home=/home/xw'
创建xw用户为系统用户指定uid(200),组为root组,shell为/bin/bash,家目录为xw
ansible web -m user -a 'name=xw state=absent'
删除用户
ansible web -m user -a 'name=xw state=absent'
此命令不会删除家目录
8.
group:组管理模块
常用参数
gid= #指定组gid
name= #指定组名(必须指定)
state=present/absent #添加/删除组
system=yse/no #是否创建为系统组
ansible web -m group -a "name=xw gid=500 state=present system=yes"
创建xw组,gid为500,为系统用户
ansible web -m group -a "name=xw gid=300 state=absent"
删除gid为300,组名为300的组
9.cron:计划任务管理模块
name #任务计划的描述信息(必须指定)
minute #分(0-59 , ,/2)
hour #时(0-23 , ,/2)
day #日(1-31 , ,/2)
month #月(1-12 , ,/2)
weekday #周(0-6 ,*)
job=path #执行的命令的绝对路径
backup=yes/no #是否先备份再创建新的任务计划
user #以哪个用户新建任务计划,默认 root
state=present/absent #创建删除计划任务
ansible web -m cron -a 'name="system_script" minute= hour= day= month= weekday=* user=root job="/bin/bash /opt/4.sh >/opt/13.txt 2>&1"'
每分中执行一次/opt/4.sh这个脚本,并把执行的结果保存在13.txt文件中
10.
service模块:服务管理模块
常用参数:
name= #服务名称
state=stopped/started/restarted/reloaded #停止/启动/重启/重读配置文件
enabled=yes/no #是否开机自启动
ansible web -m service -a "name=iptables state=started"
启动服务
ansible web -m shell -a "service iptables status"
查看服务的状态
11.setup模块:系统信息收集
ansible web -m setup
小结:ansible的模块或者命令都非常简单,对于开发或者运维,经过很少的时间学习,都能很快掌握,而且度娘上的资料非常多,也有一本书《ansible权威指南》