常用模块:

     1.command模块

      2.shell模块

      3.group模块

      4.user模块

      5.copy模块

      6.file模块

      7.mount模块

      8.yum模块

      9.service模块

      10.cron模块


1.command模块

    远程主机运行命令

[root@guoxh ~]# ansible client -m command -a 'uptime'
192.168.0.132 | SUCCESS | rc=0 >>
 06:38:42 up 9 min,  3 users,  load average: 0.00, 0.00, 0.00

192.168.0.131 | SUCCESS | rc=0 >>
 06:38:42 up 9 min,  2 users,  load average: 0.00, 0.00, 0.00

2.shell模块

  远程主机在shel下执行命令,支持管道符

[root@guoxh ~]# ansible client -m shell -a 'ss -ntl | grep 22'
192.168.0.132 | SUCCESS | rc=0 >>
LISTEN     0      128                      :::22                      :::*     
LISTEN     0      128                       *:22                       *:*     

192.168.0.131 | SUCCESS | rc=0 >>
LISTEN     0      128                      :::22                      :::*     
LISTEN     0      128                       *:22                       *:*

3.group模块

  管理组:新建或删除

gid:指定组的gid
name:指定组的name
state:创建或删除
system:是否为系统组

创建gid为300,组名为test的系统组
[root@guoxh ~]# ansible client -m group -a 'name=test gid=300 state=present system=yes'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "gid": 300, 
    "name": "test", 
    "state": "present", 
    "system": true
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "gid": 300, 
    "name": "test", 
    "state": "present", 
    "system": true
}
查看刚才新建的系统组
[root@guoxh ~]# ansible client -m shell -a 'grep test /etc/group '
192.168.0.132 | SUCCESS | rc=0 >>
test:x:300:

192.168.0.131 | SUCCESS | rc=0 >>
test:x:300:
删除test组
[root@guoxh ~]# ansible client -m group -a 'name=test state=absent'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "name": "test", 
    "state": "absent"
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "name": "test", 
    "state": "absent"
}

4.user模块

  管理用户账号

name:指定USERNAME
uid:指定用户的uid
group:指定用户的主要组
system:是否为系统用户
createhome:是否创建家目录
home:指定用户家目录
state:创建或删除
rename:是否删除家目录
password:设置用户密码(密码不能使用明文,必须做加密处理)
shell:设置用户默认登陆shell环境

创建uid为300,group为test1的系统用户,用户名为guoxh,且不创建家目录,登录shell为nologin
[root@guoxh ~]# ansible client -m user -a 'name=guoxh uid=300 group=test1 system=yes createhome=no shell=/sbin/nologin state=present'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": false, 
    "group": 300, 
    "home": "/home/guoxh", 
    "name": "guoxh", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": true, 
    "uid": 300
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": false, 
    "group": 300, 
    "home": "/home/guoxh", 
    "name": "guoxh", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": true, 
    "uid": 300
}
验证
[root@guoxh ~]# ansible client -m shell -a 'tail -1 /etc/passwd'
192.168.0.131 | SUCCESS | rc=0 >>
guoxh:x:300:300::/home/guoxh:/sbin/nologin

192.168.0.132 | SUCCESS | rc=0 >>
guoxh:x:300:300::/home/guoxh:/sbin/nologin

[root@guoxh ~]# ansible client -m shell -a 'id guoxh'
192.168.0.131 | SUCCESS | rc=0 >>
uid=300(guoxh) gid=300(test1) 组=300(test1)

192.168.0.132 | SUCCESS | rc=0 >>
uid=300(guoxh) gid=300(test1) 组=300(test1)
删除用户guoxh
[root@guoxh ~]# ansible client -m user -a 'name=guoxh state=absent'
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "guoxh", 
    "remove": false, 
    "state": "absent"
}
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "guoxh", 
    "remove": false, 
    "state": "absent"
}
验证 
[root@guoxh ~]# ansible client -m shell -a 'id guoxh'
192.168.0.131 | FAILED | rc=1 >>
id: guoxh:无此用户

192.168.0.132 | FAILED | rc=1 >>
id: guoxh:无此用户

5.copy模块

    拷贝本地文件到远程主机

src:文件源路劲
dest:文件目的路劲,如果src是目录,此项也为目录
owner:文件所有者
group:文件所属组
mode:文件权限

新建一个测试文件
[root@guoxh ~]# echo  'This is a test file'  >>  /tmp/test.txt

拷贝:源文件/tmp/test.txt目标文件:/tmp/test1.txt,属主为root,属组为root,权限为700,
[root@guoxh ~]# ansible client -m copy -a 'src=/tmp/test.txt dest=/tmp/test1.txt owner=root group=root mode=700'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "checksum": "b56df8ed5365fca1419818aa384ba3b5e7756047", 
    "dest": "/tmp/test1.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "5dd39cab1c53c2c77cd352983f9641e1", 
    "mode": "0700", 
    "owner": "root", 
    "size": 20, 
    "src": "/root/.ansible/tmp/ansible-tmp-1492010412.42-138960369354783/source", 
    "state": "file", 
    "uid": 0
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "checksum": "b56df8ed5365fca1419818aa384ba3b5e7756047", 
    "dest": "/tmp/test1.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "5dd39cab1c53c2c77cd352983f9641e1", 
    "mode": "0700", 
    "owner": "root", 
    "size": 20, 
    "src": "/root/.ansible/tmp/ansible-tmp-1492010412.43-125764504420012/source", 
    "state": "file", 
    "uid": 0
}
验证:
[root@guoxh ~]# ansible client -m shell -a 'cat /tmp/test1.txt'
192.168.0.132 | SUCCESS | rc=0 >>
This is a test file

192.168.0.131 | SUCCESS | rc=0 >>
This is a test file

6.file模块

   设置文件属性

owner:指定文件或或目录的属主
group:指定文件或目录的属组
mode:指定文件或目录的权限
path:指定文件或目录的路径,必填
recurse:递归,只对目录有效
src:文件源路径,只用于链接文件
dest:链接文件目的路径,只用于链接文件
state:
  file:即使文件不存在,也不创建
  directory:如果目录不存在,则创建
  link:创建软链接文件
  hard:创建硬链接文件
  touch:创建新文件,如果文件存在,替换目标文件
  absent:删除目录文件,或取消链接文件
  
创建一个链接文件,把/etc/inittab文件链接到/tmp目录下,命名为inittab.test,属主为guoxh,属组为root,权限为777.
[root@guoxh ~]# ansible client -m file -a 'src=/etc/inittab dest=/tmp/inittab.test owner=guoxh group=root mode=777 state=link'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/inittab.test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "guoxh", 
    "size": 12, 
    "src": "/etc/inittab", 
    "state": "link", 
    "uid": 500
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/inittab.test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "guoxh", 
    "size": 12, 
    "src": "/etc/inittab", 
    "state": "link", 
    "uid": 500
}
验证:
[root@guoxh ~]# ansible client -m shell -a 'ls -l /tmp/inittab.test'
192.168.0.132 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 guoxh root 12 4月  13 07:35 /tmp/inittab.test -> /etc/inittab

192.168.0.131 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 guoxh root 12 4月  13 07:35 /tmp/inittab.test -> /etc/inittab

7.mount模块

   控制远程主机挂载设备

src:要挂载的设备或文件系统
name:指定挂载点
fstype:指定文件系统类型
state:
   present:只修改fstab文件中的配置,不自动创建挂载点,而且不挂载
   absent:删除挂载点,修改fstab文件
   mounted:自动创建挂载点并挂载,添加自动挂载(fstab)
   unmounted:只卸载,不删除挂载点,不修改fstab文件
   
挂载iso镜像到/guoxh目录下
[root@guoxh ~]# ansible client -m mount -a 'name=/guoxh src=/dev/cdrom fstype=iso9660 state=mounted'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "iso9660", 
    "name": "/guoxh", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "/dev/cdrom"
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "iso9660", 
    "name": "/guoxh", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "/dev/cdrom"
}
验证
[root@guoxh ~]# ansible client -m shell -a 'mount  | tail -1'
192.168.0.131 | SUCCESS | rc=0 >>
/dev/sr0 on /guoxh type iso9660 (ro)

192.168.0.132 | SUCCESS | rc=0 >>
/dev/sr0 on /guoxh type iso9660 (ro)

验证fstab文件
[root@guoxh ~]# ansible client -m shell -a 'tail -1 /etc/fstab'
192.168.0.131 | SUCCESS | rc=0 >>
/dev/cdrom /guoxh iso9660 defaults 0 0

192.168.0.132 | SUCCESS | rc=0 >>
/dev/cdrom /guoxh iso9660 defaults 0 0

卸载ISO镜像,并删除挂载点
[root@guoxh ~]# ansible client -m mount -a 'name=/guoxh state=absent'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "name": "/guoxh", 
    "opts": "defaults", 
    "passno": "0"
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "name": "/guoxh", 
    "opts": "defaults", 
    "passno": "0"
}

8.yum模块

   使用yum的包管理器管理安装包

name:指定要安装的安装包名称
state:
   安装:present
         installed
         latest
   卸载:absent
         removed
安装lrzsz
[root@guoxh ~]# ansible client -m yum -a 'name=lrzsz state=installed'
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package lrzsz.x86_64 0:0.12.20-27.1.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch            Version                   Repository       Size\n================================================================================\nInstalling:\n lrzsz          x86_64          0.12.20-27.1.el6          server           71 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 71 k\nInstalled size: 159 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\r  Verifying  : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\nInstalled:\n  lrzsz.x86_64 0:0.12.20-27.1.el6                                               \n\nComplete!\n"
    ]
}
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package lrzsz.x86_64 0:0.12.20-27.1.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch            Version                   Repository       Size\n================================================================================\nInstalling:\n lrzsz          x86_64          0.12.20-27.1.el6          server           71 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 71 k\nInstalled size: 159 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\r  Verifying  : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\nInstalled:\n  lrzsz.x86_64 0:0.12.20-27.1.el6                                               \n\nComplete!\n"
    ]
}
验证:
[root@guoxh ~]# ansible client -m shell -a 'rpm -qa | grep lrzsz'
192.168.0.131 | SUCCESS | rc=0 >>
lrzsz-0.12.20-27.1.el6.x86_64

192.168.0.132 | SUCCESS | rc=0 >>
lrzsz-0.12.20-27.1.el6.x86_64
卸载lrzsz
[root@guoxh ~]# ansible client -m yum -a 'name=lrzsz state=removed'
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package lrzsz.x86_64 0:0.12.20-27.1.el6 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package       Arch           Version                     Repository       Size\n================================================================================\nRemoving:\n lrzsz         x86_64         0.12.20-27.1.el6            @server         159 k\n\nTransaction Summary\n================================================================================\nRemove        1 Package(s)\n\nInstalled size: 159 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Erasing    : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\r  Verifying  : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\nRemoved:\n  lrzsz.x86_64 0:0.12.20-27.1.el6                                               \n\nComplete!\n"
    ]
}
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package lrzsz.x86_64 0:0.12.20-27.1.el6 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package       Arch           Version                     Repository       Size\n================================================================================\nRemoving:\n lrzsz         x86_64         0.12.20-27.1.el6            @server         159 k\n\nTransaction Summary\n================================================================================\nRemove        1 Package(s)\n\nInstalled size: 159 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Erasing    : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\r  Verifying  : lrzsz-0.12.20-27.1.el6.x86_64                                1/1 \n\nRemoved:\n  lrzsz.x86_64 0:0.12.20-27.1.el6                                               \n\nComplete!\n"
    ]
}

9.service模块

   管理服务

name:指定服务名称
enabled:是否开机启动
state:
   started:启动服务
   stopped:停止服务
   restarted:重启服务
   reloaded:重新加载配置文件
   
安装httpd服务,设置开机启动,并启动服务
[root@guoxh ~]# ansible client -m yum -a 'name=httpd state=present'
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed\n---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package             Arch         Version                    Repository    Size\n================================================================================\nInstalling:\n httpd               x86_64       2.2.15-29.el6.centos       server       821 k\nInstalling for dependencies:\n apr                 x86_64       1.3.9-5.el6_2              server       123 k\n apr-util            x86_64       1.3.9-3.el6_0.1            server        87 k\n apr-util-ldap       x86_64       1.3.9-3.el6_0.1            server        15 k\n httpd-tools         x86_64       2.2.15-29.el6.centos       server        73 k\n mailcap             noarch       2.1.31-2.el6               server        27 k\n\nTransaction Summary\n================================================================================\nInstall       6 Package(s)\n\nTotal download size: 1.1 M\nInstalled size: 3.6 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           2.9 MB/s | 1.1 MB     00:00     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : apr-1.3.9-5.el6_2.x86_64                                     1/6 \n\r  Installing : apr-util-1.3.9-3.el6_0.1.x86_64                              2/6 \n\r  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/6 \n\r  Installing : httpd-tools-2.2.15-29.el6.centos.x86_64                      4/6 \n\r  Installing : mailcap-2.1.31-2.el6.noarch                                  5/6 \n\r  Installing : httpd-2.2.15-29.el6.centos.x86_64                            6/6 \n\r  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                            1/6 \n\r  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/6 \n\r  Verifying  : httpd-tools-2.2.15-29.el6.centos.x86_64                      3/6 \n\r  Verifying  : apr-1.3.9-5.el6_2.x86_64                                     4/6 \n\r  Verifying  : mailcap-2.1.31-2.el6.noarch                                  5/6 \n\r  Verifying  : apr-util-1.3.9-3.el6_0.1.x86_64                              6/6 \n\nInstalled:\n  httpd.x86_64 0:2.2.15-29.el6.centos                                           \n\nDependency Installed:\n  apr.x86_64 0:1.3.9-5.el6_2                                                    \n  apr-util.x86_64 0:1.3.9-3.el6_0.1                                             \n  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        \n  httpd-tools.x86_64 0:2.2.15-29.el6.centos                                     \n  mailcap.noarch 0:2.1.31-2.el6                                                 \n\nComplete!\n"
    ]
}
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed\n---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package             Arch         Version                    Repository    Size\n================================================================================\nInstalling:\n httpd               x86_64       2.2.15-29.el6.centos       server       821 k\nInstalling for dependencies:\n apr                 x86_64       1.3.9-5.el6_2              server       123 k\n apr-util            x86_64       1.3.9-3.el6_0.1            server        87 k\n apr-util-ldap       x86_64       1.3.9-3.el6_0.1            server        15 k\n httpd-tools         x86_64       2.2.15-29.el6.centos       server        73 k\n mailcap             noarch       2.1.31-2.el6               server        27 k\n\nTransaction Summary\n================================================================================\nInstall       6 Package(s)\n\nTotal download size: 1.1 M\nInstalled size: 3.6 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           2.9 MB/s | 1.1 MB     00:00     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : apr-1.3.9-5.el6_2.x86_64                                     1/6 \n\r  Installing : apr-util-1.3.9-3.el6_0.1.x86_64                              2/6 \n\r  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/6 \n\r  Installing : httpd-tools-2.2.15-29.el6.centos.x86_64                      4/6 \n\r  Installing : mailcap-2.1.31-2.el6.noarch                                  5/6 \n\r  Installing : httpd-2.2.15-29.el6.centos.x86_64                            6/6 \n\r  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                            1/6 \n\r  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/6 \n\r  Verifying  : httpd-tools-2.2.15-29.el6.centos.x86_64                      3/6 \n\r  Verifying  : apr-1.3.9-5.el6_2.x86_64                                     4/6 \n\r  Verifying  : mailcap-2.1.31-2.el6.noarch                                  5/6 \n\r  Verifying  : apr-util-1.3.9-3.el6_0.1.x86_64                              6/6 \n\nInstalled:\n  httpd.x86_64 0:2.2.15-29.el6.centos                                           \n\nDependency Installed:\n  apr.x86_64 0:1.3.9-5.el6_2                                                    \n  apr-util.x86_64 0:1.3.9-3.el6_0.1                                             \n  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        \n  httpd-tools.x86_64 0:2.2.15-29.el6.centos                                     \n  mailcap.noarch 0:2.1.31-2.el6                                                 \n\nComplete!\n"
    ]
}
[root@guoxh ~]# ansible client -m service -a 'name=httpd enabled=yes state=started'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}
验证:
[root@guoxh ~]# ansible client -m shell -a 'chkconfig --list | grep httpd'
192.168.0.132 | SUCCESS | rc=0 >>
httpd          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭

192.168.0.131 | SUCCESS | rc=0 >>
httpd          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭

[root@guoxh ~]# ansible client -m shell -a 'service httpd status'
192.168.0.131 | SUCCESS | rc=0 >>
httpd (pid  4542) is running...

192.168.0.132 | SUCCESS | rc=0 >>
httpd (pid  5128) is running...

10.cron模块

   管理计划任务

name:指定计划任务描述,必填
job:要执行的任务
user:运行计划任务的用户
执行时间:
  minute:0-59,默认为*
  hour:0-23,默认为*
  day:1-31,默认为*
  month:1-12,默认为*
  weekday:1-7,默认为*
state:
  present:添加计划任务
  absent:删除计划任务

添加一个计划任务测试一下
[root@guoxh ~]# ansible client -m cron -a 'name=test user=guoxh minute=*/2 job="echo test >> /tmp/guoxh.txt" state=present'
192.168.0.131 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test"
    ]
}
192.168.0.132 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test"
    ]
}
验证:
[root@guoxh ~]# ansible client -m shell -a 'crontab -l -u guoxh'
192.168.0.131 | SUCCESS | rc=0 >>
#Ansible: test
*/2 * * * * echo test >> /tmp/guoxh.txt

192.168.0.132 | SUCCESS | rc=0 >>
#Ansible: test
*/2 * * * * echo test >> /tmp/guoxh.txt

[root@guoxh ~]# ansible client -m shell -a 'cat /tmp/guoxh.txt'
192.168.0.132 | SUCCESS | rc=0 >>
test

192.168.0.131 | SUCCESS | rc=0 >>
test



到这里,Ansible常用的十个模块介绍完毕!