經常使用模塊使用linux
首先在使用模塊以前咱們能夠查看一下模塊有多少,不過大多數用不到。下面主要看幾個經常使用模塊redis
[root@soso ~]# ansible-doc -l | wc -l 262
一、setup
shell
##用來查看遠程主機的基本信息apache
[root@soso ~]# ansible test -m setup
二、pingbash
##用來測試遠程主機的運行狀態服務器
[root@soso ~]# ansible test -m ping
三、filessh
##設置文件屬性ide
[root@soso ~]# ansible-doc -s file - name: Sets attributes of files action: file force # 須要在兩種狀況下建立軟鏈接,一種是源文件不存在,但以後創建的狀況下;另外一種是目標軟鏈接已存在,須要先取消以前的軟鏈,而後建立新的軟鏈:有兩個選項:yes|no group # 定義文件/目錄的所屬組 mode # 定義文件/目錄的權限 owner # 定義文件/目錄的所屬主 path= # 必選項,定義文件/目錄的路徑 recurse # 遞歸設置文件的屬性,只對目錄有效 src # 要被連接的源文件路徑,只應用於state=link的狀況有效 state # 狀態 directory #若是目錄不存在則建立目錄 file #即便文件不存在,也不會被建立 link #建立軟鏈接 hard #建立硬連接 touch #若是文件不存在,則會建立一個新的文件;若是文件或目錄存在,則更新其最後的修改時間 absent #刪除目錄、文件或者取消連接文件 (END)
將遠程主機/etc/passwd 文件連接到 /root 目錄下:測試
[root@soso ~]# ls Python-2.7.5 shell soft [root@soso ~]# ansible test -m file -a "src=/etc/passwd dest=/root/passwd state=link" 192.168.1.2 | success >> { "changed": true, "dest": "/root/passwd", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 11, "src": "/etc/passwd", "state": "link", "uid": 0 } [root@soso ~]# ll total 12 drwxr-xr-x 18 1000 1000 4096 Nov 26 11:11 Python-2.7.5 lrwxrwxrwx 1 root root 11 Dec 16 15:35 passwd -> /etc/passwd drwxr-xr-x 3 root root 4096 Dec 7 15:06 shell drwxr-xr-x 12 root root 4096 Dec 16 15:32 soft
刪除建立的軟鏈接ui
[root@soso ~]# ansible test -m file -a 'path=/root/passwd state=absent' 192.168.1.2 | success >> { "changed": true, "path": "/root/passwd", "state": "absent" } [root@soso ~]# ll total 12 drwxr-xr-x 18 1000 1000 4096 Nov 26 11:11 Python-2.7.5 drwxr-xr-x 3 root root 4096 Dec 7 15:06 shell drwxr-xr-x 12 root root 4096 Dec 16 15:32 soft
建立文件
[root@soso ~]# ansible test -m file -a 'path=/root/1 state=touch' 192.168.1.2 | success >> { "changed": true, "dest": "/root/1", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } [root@soso ~]# ls 1 Python-2.7.5 shell soft
四、cope
##複製本地文件到遠程主機,進行統一部署
用法:
backup # 用於複製時,是否備份遠程主機上的目標文件
content # 用於替代 ‘src’ ,能夠直接設定指定文件的值
dest= # 必選項,要將源文件複製到遠程主機的絕對路徑,若是源文件是一個目錄,那麼遠程路徑也必須是一個目錄
directory_mode # 遞歸設定目錄的權限,默認爲系統默認權限
force # 強制覆蓋。若是目標主機包含該文件,但內容不一樣,若是設置爲yes,則強制覆蓋;若是設置爲no,則只有當目錄主機的目錄位置不存在該文件時,才複製。默認爲yes
group # 定義文件/目錄的所屬組
mode # 定義文件/目錄的權限
owner # 定義文件/目錄的所屬主
src # 要複製到遠程主機的文件在本地的地址,能夠是絕對路徑,也能夠是相對路徑。若是路徑是一個目錄,它將遞歸複製。在這種狀況下,若是路徑使用 '/' 結尾,則只複製目錄裏的內容,若是沒有使用 '/' 來結尾,則包含目錄在內的整個內容所有複製。
實例:
講本地文件複製到遠程主機
[root@soso ~]# ansible test -m copy -a "src=/root/1 dest=/tmp/1 mode=600 " 192.168.1.2 | success >> { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/1", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0600", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1450252219.51-80486766978488/source", "state": "file", "uid": 0 } [root@soso ~]# ll /tmp/1 -rw------- 1 root root 0 Dec 16 15:50 /tmp/1
五、command
##遠程主機上執行命令
creates:一個文件名,當該文件存在,則該命令不執行
free_form:要執行的linux指令
chdir:在執行指令以前,先切換到該目錄
removes:一個文件名,當該文件不存在,則該選項不執行
executable:切換shell來執行指令,該執行路徑必須是一個絕對路徑
[root@soso ~]# ansible test -m command -a 'ls /home' 192.168.1.2| success | rc=0 >> redis zabbix
實例:
使用 creates 參數,判斷一個文件是否存在,存在的話,就跳事後面的執行命令
[root@soso ~]# ansible test -m command -a 'creates=/tmp/1 ls -l /etc/passwd' 192.168.1.2 | success | rc=0 >> skipped, since /tmp/1 exists [root@soso ~]# ansible test -m command -a 'creates=/tmp/2 ls -l /etc/passwd' 192.168.1.2 | success | rc=0 >> -rw-r--r-- 1 root root 1882 Dec 14 20:11 /etc/passwd
六、shell
##與command不一樣的是,此模塊能夠支持命令管道,同時還有另外一個模塊也具有此功能:raw
實例1:
[root@soso ~]# ansible test -m shell -a 'cat /etc/passwd| grep root' 192.168.1.2 | success | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
七、synchronize
使用rsync同步文件。使用rsync 模塊,系統必須安裝rsync 包,不然沒法使用這個模塊
用法:
archive # 是否採用歸檔模式同步,即以源文件相同屬性同步到目標地址
checksum # 是否效驗
compress #
copy_links # 同步的時候是否複製鏈接
delete # 刪除源中沒有而目標存在的文件
dest= # 目標地址
dest_port # 目標接受的端口
dirs # 以非遞歸的方式傳輸目錄
existing_only # Skip creating new files on receiver.
group # Preserve group
links # Copy symlinks as symlinks.
mode # 模式,rsync 同步的方式 PUSH\PULL
recursive # 是否遞歸 yes/no
rsync_opts # 使用rsync 的參數
rsync_path # 服務的路徑(源碼編譯時需指定)
rsync_timeout # Specify a --timeout for the rsync command in seconds.
set_remote_user # put user@ for the remote paths. If you have a custom ssh config to define the remote user for
src= # 源,同步的數據源
檢查機器上是否裝過rsync:
[root@soso ~]#which rsync
沒有裝過的主機能夠遠程安裝rsync包:
[root@soso ~]# ansible test -m yum -a 'name=rsync state=latest'
實例:
一、將ansible 服務端/root/ 下的hello 文件同步到test的/tmp:
[root@soso ~]# ansible test -m synchronize -a 'src=/root/hello dest=/tmp/' 192.168.1.2 | success >> { "changed": true, "cmd": "rsync --delay-updates -F --compress --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/root/hello\" \"root@121.42.141.231:/tmp/\"", "msg": "<f+++++++++ hello\n", "rc": 0, "stdout_lines": [ "<f+++++++++ hello" ] } [root@soso ~]# ls /tmp/hello /tmp/hello
八、cron
cron 模塊,用於管理計劃任務
用法:
backup # 在對遠程主機上的原計劃任務修改以前作備份(也就是先備份再修改)
cron_file # 若是指定該選項,則用該文件替換遠程主機上的cron.d目錄下的用戶的任務計劃
day # 天(1-31,*,*/2, ……)
hour # 小時( 0-23, *, */2, ……)
job # T要執行的任務,依賴於state=present
minute # 分鐘(0-59,*,*/2,……)
month # 月(1-12,*,*/2,……)
name # 該任務的描述
reboot # If the job should be run at reboot. This option
special_time #指定何時執行,參數: reboot,yearly,annually,monthly,weekly,daily,hourly
state # 確認該任務計劃是建立仍是刪除
user #以哪一個用戶的身份執行
weekday #周(0-7,*,……)
實例:
一、給test 主機建立一個計劃任務:
[root@soso ~]# ansible test -m cron -a "name='echo ' hour=2 user=root job='echo 1 >> /root/hello' " 192.168.1.2 | success >> { "changed": true, "jobs": [ "echo " ] } [root@soso ~]# ansible test -m shell -a "crontab -l" 192.168.1.2 | success | rc=0 >> * * * * 1 sh /root/shell/apache_log.sh #Ansible: echo * 2 * * * echo 1 >> /root/hello
二、刪除剛纔建立的計劃任務
[root@soso ~]# ansible test -m cron -a "name='echo' hour=2 user=root job='echo 1 >> /root/hello' state=absent" 192.168.1.2 | success >> { "changed": true, "jobs": [] } [root@soso ~]# ansible test -m shell -a "crontab -l" 192.168.1.2 | success | rc=0 >> * * * * 1 sh /root/shell/apache_log.sh
九、service
service 模塊做用於一些服務,好比對某些服務器的啓動、重啓、中止、重載等的管理
arguments # 給命令行提供一些選項
enabled # 是否開機啓動 yes|no
name= # 必選項,服務名稱
pattern # 定義一個模式,若是經過status指令來查看服務的狀態時,沒有響應,就會經過ps指令在進程中根據該模式進行查找,若是匹配到,則認爲該服務依然在運行
runlevel # 運行級別
sleep # 若是執行了restarted,在則stop和start之間沉睡幾秒鐘
state # 對當前服務執行啓動,中止、重啓、從新加載等操做 (started,stopped,restarted,reloaded)
實例:
實例:遠程啓動http
[root@soso ~]# ss -nl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:43131 *:* [root@soso ~]# ansible test -m service -a "name=httpd state=started enabled=yes" 192.168.1.2 | success >> { "changed": true, "enabled": true, "name": "httpd", "state": "started" } [root@soso ~]# ss -nl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:43131 *:*
其餘經常使用的模塊還有 yum user filesystem mount