ansible學習系列2-ansible常用模塊使用


1. 查看支持的模塊

[root@localhost ~]# ansible-doc -l

這里我們看下ansible的支持的模塊個數

[root@localhost ~]# ansible-doc -l |wc -l   #查看支持的模塊個數
1039
[root@localhost ~]# ansible --version        #查看我們的ansible版本號
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.6.6 (r266:84292, Aug 18 2016, 14:53:48) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

2.獲取模塊的幫助

這里我們使用ansible-doc獲取下command模塊的使用方式。

[root@localhost ~]# ansible-doc command

 

3.1 command模塊

command :作為ansible的默認模塊,可以允許遠程主機范圍內的所有shell命令。

注意: 在command的命令中含有像`$ HOME'這樣的變量和像``<“',`”>“, `“”“”,“”;“”和“”&“'將無法正常工作(如果需要這些功能,請使用[shell]模塊)

[root@localhost ~]# ansible 192.168.168.11* -m command -a 'ip addr show dev eth0'
192.168.168.115 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe29:8de2/64 scope link 
       valid_lft forever preferred_lft forever

192.168.168.111 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:7791/64 scope link 
       valid_lft forever preferred_lft forever

3.2 script模塊

功能:在遠程主機上執行主控端的腳本,相當於scp+shell組合。

[root@localhost ~]# ansible all -m script -a "/home/test.sh 12 34"

3.3 shell模塊

功能:執行遠程主機的shell腳本文件

[root@localhost ~]# ansible all -m shell -a "/home/test.sh"

shell替代command執行

[root@localhost ~]# ansible 192.168.168.11* -m shell -a 'ip addr show dev eth0'
192.168.168.111 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:7791/64 scope link 
       valid_lft forever preferred_lft forever

192.168.168.115 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe29:8de2/64 scope link 
       valid_lft forever preferred_lft forever

3.4 copy模塊

功能: 實現主控端向目標主機copy文件。

[root@localhost ~]# ansible all -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"    
#src 主控端文件位置
#dest 被控端目標位置
#owner 文件復制過去后的所有者
#group 文件復制過去后的所屬組
#mode 文件的權限設定,執行a+x這種方式

3.5 stat模塊

功能: 獲取遠程文件的狀態信息,包括atime,ctime,mtime,md5,uid,gid等信息。

[root@localhost ~]# ansible all -m stat -a "path=/etc/sysctl.conf"

3.6 yum模塊

功能: 安裝軟件包。

[root@localhost ~]# ansible all -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel"
#name 包名
#state (Choices: present, installed, latest, absent, removed)[Default: present]
#disable_gpg_check:禁止gpg檢查
#enablerepo:只啟動指定的repo

3.7 cron模塊

功能:遠程主機crontab配置

[root@localhost ~]# ansible all -m cron -a "name='test' hour='2-5' minute='*/5' day='1' month='3,4' weekday='1' job='ls -l' user=tom"
192.168.168.115 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}
192.168.168.111 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}

我們去被控主機看下生成的crontab作業

[root@localhost ~]# crontab  -l -u tom
#Ansible: test
*/5 2-5 1 3,4 1 ls -l

刪除指定crontab

[root@localhost ~]# ansible all -m cron -a "name=test state=absent"

3.8 mount模塊

功能: 掛載文件系統

[root@localhost ~]# ansible 192.168.168.111 -m mount -a "path=/mnt/data src=/dev/sd0 fstype=ext3 ots=ro state=present"

注:mount已經使用path代替了原來的name參數,但是name參數還是可以使用的。

3.9 service模塊

功能: 服務管理

[root@localhost ~]# ansible all -m service -a "name=httpd state=restarted"    #啟動服務
[root@localhost ~]# ansible all -m service -a "name=httpd state=running"      #查看服務狀態
[root@localhost ~]# ansible all -m service -a "name=httpd state=stoped"       #停止服務

3.10 user模塊

功能: 遠程主機的用戶管理

[root@localhost ~]# ansible all -m user -a "name=jerry comment=' doubi jerry'"   #添加用戶 詳細參數參考ansible-doc user
[root@localhost ~]# ansible all -m user -a "name=jerry state=absent remove=yes"  #刪除用戶

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM