ansible模塊應用語法格式: ansible 主機名稱/主機組名稱/主機地址信息/all -m(指定應用的模塊信息) -a(指定動做信息)
html
command模塊(默認模塊)
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a hostname
192.168.0.102 | SUCCESS | rc=0 >>
linux-node1.localdomainnode
chdir 切換目錄
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "chdir=/tmp touch ansible.txt"
[WARNING]: Consider using file module with state=touch rather than running touchpython
192.168.0.102 | SUCCESS | rc=0 >>linux
creates 若是指定文件存在,就不執行命令
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "creates=/tmp touch ansible.txt"
192.168.0.102 | SUCCESS | rc=0 >>
skipped, since /tmp exists #若是ansible.txt文件已經存在 ,就跳過 不執行shell
removes 若是文件存在,就執行
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "removes=/tmp touch ansible.txt"
[WARNING]: Consider using file module with state=touch rather than running touchbash
192.168.0.102 | SUCCESS | rc=0 >> #若是 ansible.txt文件不存在,就不執行服務器
shell模塊(萬能模塊): 若是要用特殊符號 > < | & ‘ ‘ 則要用shell 模塊, command是不支持管道符之類的dom
ansible 192.168.0.102 -m shell -a "hostname"
ansible 192.168.0.102 -m shell -a "echo 123 > haha.txt"
ansible 192.168.0.102 -m shell -a "netstat -anptu | grep xxx"ide
實踐使用 利用shell執行腳本
第一個步驟:編寫一個腳本
第二個步驟:將腳本發送到遠程主機
第三個步驟:將腳本權限進行修改(添加執行權限)
第四個步驟:運行ansible命令執行腳本oop
一、第一個步驟:編寫一個腳本
eg:執行檢查磁盤腳本
#!/bin/bash basedir=$(cd `dirname $0`;pwd) echo $basedir diskmax=10 # 磁盤的閾值 function check_max(){ local disk_size=$1 if [ $disk_size -ge $diskmax ] then echo "unhealth" else echo "health" fi } function check_disk_info(){ #grep -v 取反 # cut -d '%' -f 1 以%爲分隔符選擇第一個 #while read disk_size 循環讀入每行數據 賦值給disk_size df -h | grep -v /dev/loop0 | grep -v /dev/sr0 | grep dev | awk 'NR > 1 {print $5}' | cut -d '%' -f 1 | while read disk_size do echo "" echo "disk_size=$disk_size%" check_max $disk_size done } check_disk_info
二、第二個步驟:將腳本發送到遠程主機
拷貝腳本以前,先遠程建立好準備拷貝過去的文件目錄 ,其中遠程建立文件目錄有兩種方式,操做以下:
方式一:shell命令方式
gota@gota-linux61:~$ ansible cluster -m shell -a "mkdir -p /work/ansible" [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of thi
s message. 192.168.1.21 | CHANGED | rc=0 >> 192.168.1.20 | CHANGED | rc=0 >> 192.168.1.63 | CHANGED | rc=0
方式二:file命令執行建立文件夾
gota@gota-linux61:~$ ansible cluster -m file -a "path=/work/file state=directory mode=0700" 192.168.1.21 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0700", "owner": "root", "path": "/work/file", "size": 4096, "state": "directory", "uid": 0 }
三、第三個步驟:將腳本權限進行修改(添加執行權限)
批量拷貝shell腳本到各臺服務器
ansible 主機組 -m copy -a "src=拷貝文件路徑 dest=拷貝目前文件路徑 mode=0755
gota@gota-linux61:~$ ansible cluster -m copy -a "src=/home/gota/test.sh dest=/work/ansible mode=0755" 192.168.1.21 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "dcaa896be5c669bbfcc657cc25452d46962aedb6", "dest": "/work/ansible/test.sh", "gid": 0, "group": "root", "md5sum": "bf53066b75afbc91cd8917d43e10880d", "mode": "0755", "owner": "root", "size": 637, "src": "/root/.ansible/tmp/ansible-tmp-1564985837.03-26369088965074/source", "state": "file", "uid": 0
四、第四個步驟:運行ansible命令執行腳本
gota@gota-linux61:~$ ansible cluster -m shell -a "/work/ansible/test.sh" 192.168.1.21 | CHANGED | rc=0 >> /work/ansible disk_size=10% unhealth disk_size=9% health disk_size=98% unhealth 192.168.1.20 | CHANGED | rc=0 >> /work/ansible disk_size=5% health disk_size=1% health disk_size=96% unhealth
script模塊 跟上面相似 執行腳本只要兩步:
1.第一個步驟:編寫一個腳本
2.第二個步驟:運行ansible命令執行腳本
gota@gota-linux61:~$ ansible cluster -m script -a "/home/gota/test.sh" 192.168.1.21 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.21 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.1.21 closed." ], "stdout": "/root/.ansible/tmp/ansible-tmp-1564988286.84-276931002259842\r\n\r\ndisk_size=10%\r\nunhealth\r\n\r\ndisk_size=9%\r\nhealth\r\n\r\ndisk_size=98%\r\nunhealth\r\n", "stdout_lines": [ "/root/.ansible/tmp/ansible-tmp-1564988286.84-276931002259842", "", "disk_size=10%", "unhealth", "", "disk_size=9%", "health", "", "disk_size=98%", "unhealth" ] }