Ansible學習:(二)Ansible常用模塊詳解


常用模塊:

     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常用的十個模塊介紹完畢!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章