l ansible是一個基於Python開發的自動化運維工具!(saltstack) python語言是運維人員必會的語言!html
l 其功能實現基於SSH遠程鏈接服務!python
l ansible能夠實現批量系統配置、批量軟件部署、批量文件拷貝、批量運行命令等功能linux
http://docs.ansible.com/ansible/intro_installation.htmlweb
http://www.ansible.com.cn/shell
http://docs.ansible.com/modules_by_category.htmlvim
http://www.ansible.cn/docs/centos
1. 不須要單獨安裝客戶端(no agents),基於系統自帶的sshd服務,sshd就至關於ansible的客戶端。bash
2. 不須要服務端(no servers)。服務器
3. 須要依靠大量的模塊實現批量管理。網絡
4. 配置文件/etc/ansible/ansible.cfg,不用配置
yum install -y sshpass
[root@m01 scripts]# vim fenfa.sh 1 #!/bin/bash 2 3 # create key pair 4 \rm /root/.ssh/id_rsa* -f 5 ssh-keygen -t rsa -f /root/.ssh/id_rsa -P "" &>/dev/null 6 7 # fenfa 8 for ip in 7 8 31 41 9 do 10 echo =====================172.16.1.$ip fenfa info========================== 11 sshpass -p123456 ssh-copy-id -i /root/.ssh/id_rsa.pub "172.16.1.$ip -o StrictHostKeyChecking=no" 12 echo =====================172.16.1.$ip fenfa end=========================== 13 echo "" 14 done |
[root@m01 scripts]# sh fenfa.sh =====================172.16.1.7 fenfa info========================== Warning: Permanently added '172.16.1.7' (RSA) to the list of known hosts. Now try logging into the machine, with "ssh '172.16.1.7 -o StrictHostKeyChecking=no'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
=====================172.16.1.7 fenfa end===========================
=====================172.16.1.8 fenfa info========================== Now try logging into the machine, with "ssh '172.16.1.8 -o StrictHostKeyChecking=no'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting. =====================172.16.1.8 fenfa end===========================
=====================172.16.1.31 fenfa info========================== Warning: Permanently added '172.16.1.31' (RSA) to the list of known hosts. Now try logging into the machine, with "ssh '172.16.1.31 -o StrictHostKeyChecking=no'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
=====================172.16.1.31 fenfa end===========================
=====================172.16.1.41 fenfa info========================== Warning: Permanently added '172.16.1.41' (RSA) to the list of known hosts. Now try logging into the machine, with "ssh '172.16.1.41 -o StrictHostKeyChecking=no'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
=====================172.16.1.41 fenfa end=========================== |
[root@m01 scripts]# ssh 172.16.1.31 hostname nfs01 |
安裝ansible軟件: yum install -y ansible
yum install libselinux-python -y --- 若是selinux關閉的,能夠不安裝(建議安裝)
至此:ansible軟件在centos系統上安裝完畢
[root@m01 scripts]# vim /etc/ansible/hosts ----定義ansible可管理的主機 [wuhuang] 172.16.1.7 172.16.1.8 172.16.1.31 172.16.1.41 |
[root@m01 scripts]# ansible wuhuang -m command -a "uptime" ----查看wuhuang主機組的負載信息 172.16.1.7 | SUCCESS | rc=0 >> 14:24:06 up 4:31, 2 users, load average: 0.08, 0.02, 0.01
172.16.1.31 | SUCCESS | rc=0 >> 14:24:06 up 4:37, 2 users, load average: 0.00, 0.00, 0.00
172.16.1.41 | SUCCESS | rc=0 >> 14:24:06 up 4:32, 2 users, load average: 0.00, 0.00, 0.00
172.16.1.8 | SUCCESS | rc=0 >> 14:24:06 up 4:30, 3 users, load average: 0.00, 0.00, 0.00 |
參數說明: wuhuang -----主機組 -m -----指定模塊參數(command爲默認模塊,不寫也能夠) command -----模塊名稱 -a -----指定利用模塊執行的動做參數,-a後面的是要執行的命令 uptime -----批量執行的命令 |
[root@m01 scripts]# vim /etc/ansible/hosts [wuhuang] 172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456 172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=123456 172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=123456 172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=123456 |
說明:後面的用戶和密碼項是非必須的,在配置key認證的狀況下,不使用密碼也能夠直接操做 。
未使用key的,也能夠在ansible經過 -k參數在操做前詢問手動輸入密碼。
[root@m01 scripts]# vim /etc/ansible/hosts [wuhuang] 172.16.1.7 172.16.1.8 172.16.1.31 ansible_ssh_user=root 172.16.1.41 |
[root@m01 scripts]# ansible 172.16.1.31 -m ping -k SSH password: 172.16.1.31 | SUCCESS => { "changed": false, "ping": "pong" } |
說明:以交互方式,輸入密碼信息進行遠程管理 |
小結:ansible軟件命令經常使用參數
-k --ask-pass:以交互方式輸入密碼,進行遠程管理
-a :指定應用模塊的相應參數信息
-m :指定應該什麼模塊(默認爲command模塊)
進行ansible軟件批量管理:
l 熟悉ansible命令參數語法(重要!!!!)
l 指定管理主機的方法
Ø 1)指定單臺主機地址信息
Ø 2)指定主機組信息
Ø 3)管理所有主機(all)
ansible mount -m setup -vvvv --- 主要用於排查ansible批量管理錯誤
ansible-doc -l --- 列出全部可用的模塊信息,
ansible-doc -s cron --- 查看指定模塊的參數信息
綠色: 表示查看信息,對遠程主機未作改動的命令
紅色: 批量管理產生錯誤信息
×××: 對遠程主機作了相應改動
粉色: 對操做提出建議或忠告
官方連接:http://docs.ansible.com/ansible/latest/command_module.html
批量顯示遠程主機的網卡信息
ansible wuhuang -m command -a "ifconfig"
[root@m01 scripts]# ansible wuhuang -m command -a "ifconfig" |
批量切換到遠程主機的/tmp目錄下,建立hosts01這個文件
ansible wuhuang -m command -a "chdir=/tmp touch hosts01"
[root@m01 scripts]# ansible wuhuang -m command -a "chdir=/tmp touch hosts01" [WARNING]: Consider using file module with state=touch rather than running touch
172.16.1.31 | SUCCESS | rc=0 >>
172.16.1.41 | SUCCESS | rc=0 >>
172.16.1.8 | SUCCESS | rc=0 >>
172.16.1.7 | SUCCESS | rc=0 >> |
[root@web01 tmp]# ll total 4 -rw-r--r-- 1 root root 0 Feb 3 15:31 hosts01 |
批量判斷遠程主機/tmp下有沒有hosts這個文件,若是有就skip,沒有就執行後面的命令
ansible wuhuang -m command -a "creates=/tmp/hosts01 touch hosts01"
[root@m01 scripts]# ansible wuhuang -m command -a "creates=/tmp/hosts01 touch hosts01" 172.16.1.31 | SUCCESS | rc=0 >> skipped, since /tmp/hosts01 exists
172.16.1.41 | SUCCESS | rc=0 >> skipped, since /tmp/hosts01 exists
172.16.1.8 | SUCCESS | rc=0 >> skipped, since /tmp/hosts01 exists
172.16.1.7 | SUCCESS | rc=0 >> skipped, since /tmp/hosts01 exists |
批量判斷遠程主機/tmp下有沒有hosts01這個文件,若是有就執行後面的命令, 沒有就skip
ansible wuhuang -m command -a "removes=/tmp/hosts01 touch /tmp/123.txt"
[root@m01 scripts]# ansible wuhuang -m command -a "removes=/tmp/hosts01 touch /tmp/123.txt" [WARNING]: Consider using file module with state=touch rather than running touch
172.16.1.31 | SUCCESS | rc=0 >>
172.16.1.41 | SUCCESS | rc=0 >>
172.16.1.8 | SUCCESS | rc=0 >>
172.16.1.7 | SUCCESS | rc=0 >> |
[root@web01 tmp]# ll total 4 -rw-r--r-- 1 root root 0 Feb 3 15:50 123.txt -rw-r--r-- 1 root root 0 Feb 3 15:31 hosts01 |
[root@m01 scripts]# ansible wuhuang -m command -a "ls -l" 172.16.1.41 | SUCCESS | rc=0 >> total 40 -rw-------. 1 root root 1220 Jan 18 21:29 anaconda-ks.cfg -rw-r--r--. 1 root root 21736 Jan 18 21:29 install.log -rw-r--r--. 1 root root 5890 Jan 18 21:25 install.log.syslog
172.16.1.31 | SUCCESS | rc=0 >> total 48 -rw-------. 1 root root 1220 Jan 18 21:29 anaconda-ks.cfg -rw-r--r--. 1 root root 21736 Jan 18 21:29 install.log -rw-r--r--. 1 root root 5890 Jan 18 21:25 install.log.syslog -rw-r--r-- 1 root root 6 Jan 31 15:35 max_queued_events~ -rw-r--r-- 1 root root 6 Jan 31 15:38 max_queued_eventz~
172.16.1.7 | SUCCESS | rc=0 >> total 40 -rw-------. 1 root root 1220 Jan 18 21:29 anaconda-ks.cfg -rw-r--r--. 1 root root 21736 Jan 18 21:29 install.log -rw-r--r--. 1 root root 5890 Jan 18 21:25 install.log.syslog
172.16.1.8 | SUCCESS | rc=0 >> total 40 -rw-------. 1 root root 1220 Jan 18 21:29 anaconda-ks.cfg -rw-r--r--. 1 root root 21736 Jan 18 21:29 install.log -rw-r--r--. 1 root root 5890 Jan 18 21:25 install.log.syslog |
說明:command模塊爲默認模塊,能夠省略不用輸入
chdir --- 在執行命令以前,經過cd命令進入到指定目錄中
creates --- 定義一個文件是否存在,若是不存在,則運行相應命令;若是存在跳過此步驟
removes --- 定義一個文件是否存在,若是存在運行相應命令;若是不存在跳過此步驟
free_form(默認參數) --- 能夠輸入任何系統命令信息,可是不包含一些特殊環境變量信息和特殊
符號信息"<", ">", "|", ";" and "&" (默認必需要存在的參數)
官方連接:http://docs.ansible.com/ansible/latest/ping_module.html
ansible wuhuang -m ping
[root@m01 scripts]# ansible wuhuang -m ping 172.16.1.41 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.31 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.8 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.7 | SUCCESS => { "changed": false, "ping": "pong" } |
說明: 返回pong,說明能夠登陸SSH鏈接,這裏ping不是測試網絡連通性的,用於驗證可否登陸SSH鏈接,在查 看是否知足python的支持, 屬於system模塊 |
官方連接:http://docs.ansible.com/ansible/latest/copy_module.html
將本機/etc/sysconfig/iptables文件複製到遠程主機的/tmp目錄下(若是/tmp下已有同名文件,則會被覆蓋)
ansible wuhuang -m copy -a "src=/etc/sysconfig/iptables dest=/tmp"
[root@m01 scripts]# ansible wuhuang -m copy -a "src=/etc/sysconfig/iptables dest=/tmp" 172.16.1.31 | SUCCESS => { "changed": true, "checksum": "b0abfab2d50ce1648e54b9cb7d2fcddcb2089852", "dest": "/tmp/iptables", "gid": 0, "group": "root", "md5sum": "5ee09def44bd598f82e3717df846e2fc", "mode": "0644", "owner": "root", "size": 476, "src": "/root/.ansible/tmp/ansible-tmp-1517648150.54-133195812780831/source", "state": "file", "uid": 0 } …… |
若是遠程主機沒有dir目錄,那麼會建立wuhuang_dir目錄
ansible wuhuang -m copy -a "src=/etc/hosts dest=/tmp/wuhuang_dir/"
[root@m01 scripts]# ansible wuhuang -m copy -a "src=/etc/hosts dest=/tmp/wuhuang_dir/" 172.16.1.31 | SUCCESS => { "changed": true, "checksum": "74e3f808eb07d3f8193b8fbe91877db4bc5930fd", "dest": "/tmp/wuhuang_dir/hosts", "gid": 0, "group": "root", "md5sum": "757d257f69f5040eaed85c14229dc86e", "mode": "0644", "owner": "root", "size": 372, "src": "/root/.ansible/tmp/ansible-tmp-1517648947.23-279635150878284/source", "state": "file", "uid": 0 } …… |
傳輸文件時,若是上級目錄不存在,則不會建立,傳輸就沒法成功
ansible wuhuang -m copy -a "src=/etc/hosts dest=/tmp/1/2/3/4/"
傳輸目錄時,若是遠程主機目錄不存在,傳輸時能夠建立多層目錄;
若是傳輸的是目錄自己及下面內容,後面不要加/(相似與rsync);
若是傳輸的是目錄下面的內容,後面必須加/
ansible wuhuang -m copy -a "src=/tmp dest=/tmp/1/2/3/4"
批量操做遠程主機,對他們本機上的文件進行本地操做,設置爲true時(默認爲flase),不支持遞歸複製
ansible wuhuang -m copy -a "remote_src=true src=/etc/hosts dest=/tmp/1/2/"
分發文件時,若是與遠程主機下iptables文件內容不一致,那麼會備份源文件爲"iptables.5714...以時間戳命名",再修改iptables源文件的內容
不輸入默認backup=no,就是不備份,會覆蓋源文件
ansible wuhuang -m copy -a "src=/etc/sysconfig/iptables dest=/tmp backup=yes"
[root@m01 scripts]# ansible wuhuang -m copy -a "src=/etc/sysconfig/iptables dest=/tmp backup=yes" 172.16.1.41 | SUCCESS => { "changed": false, "checksum": "b0abfab2d50ce1648e54b9cb7d2fcddcb2089852", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/tmp/iptables", "size": 476, "state": "file", "uid": 0 } …… |
改變文件的權限爲0600,全部者和屬組爲wuhuang
ansible wuhuang -m copy -a "src=/etc/hosts dest=/tmp owner=wuhuang group=wuhuang mode=600"
[root@m01 scripts]# ansible wuhuang -m copy -a "src=/etc/hosts dest=/tmp owner=wuhuang group=wuhuang mode=600" 172.16.1.41 | SUCCESS => { "changed": true, "checksum": "74e3f808eb07d3f8193b8fbe91877db4bc5930fd", "gid": 502, "group": "wuhuang", "mode": "0600", "owner": "wuhuang", "path": "/tmp/hosts", "size": 372, "state": "file", "uid": 502 } …… |
默認爲forec=yes,若是和遠程主機信息不一致,會覆蓋
若是force=no,那麼遠程主機同名文件不會作改變
ansible wuhuang -m copy -a "src=/etc/sysconfig/iptables dest=/tmp force=no"
[root@m01 scripts]# ansible wuhuang -m copy -a "src=/etc/sysconfig/iptables dest=/tmp force=no" 172.16.1.41 | SUCCESS => { "changed": false, "dest": "/tmp", "src": "/etc/sysconfig/iptables" } 172.16.1.31 | SUCCESS => { "changed": false, "dest": "/tmp", "src": "/etc/sysconfig/iptables" } 172.16.1.8 | SUCCESS => { "changed": false, "dest": "/tmp", "src": "/etc/sysconfig/iptables" } 172.16.1.7 | SUCCESS => { "changed": false, "dest": "/tmp", "src": "/etc/sysconfig/iptables" } |
寫入信息到/tmp/ alex.txt中會把源內容覆蓋掉,謹慎操做,只能添加少許的信息,添加多量的,能夠用template模塊
ansible wuhuang -m copy -a "content='alex.python' dest=/tmp/alex.txt"
[root@m01 scripts]# ansible wuhuang -m copy -a "content='alex.python' dest=/tmp/alex.txt" 172.16.1.31 | SUCCESS => { "changed": true, "checksum": "f017f4745821dc0d2ad6ca28d813f448d2454279", "dest": "/tmp/alex.txt", "gid": 0, "group": "root", "md5sum": "c3c6844b44a0436fe2c9d0a4a4232b27", "mode": "0644", "owner": "root", "size": 11, "src": "/root/.ansible/tmp/ansible-tmp-1517648848.56-260572564303392/source", "state": "file", "uid": 0 } …… |
總結:
若是遠程主機沒有相應的目錄,能夠經過copy模塊傳輸數據時進行建立,而且能夠建立多級目錄
Ø src --- 本地路徑文件複製到遠程服務器;能夠是絕對路徑也能夠是相對路徑
若是路徑是一個目錄,將會遞歸複製。
在這種狀況下,若是路徑以「/」結束,只將在該目錄的內容複製到目的地
不然,若是它不以「/」結束,目錄和目錄下全部內容都會被複制
以上這種複製方式相似於rsync
Ø dest --- 文件應該被拷貝到的遠程絕對路徑信息。若是src是一個目錄,dest也必須是目錄
(默認必需要存在的)
Ø owner --- 定義所擁有文件/目錄的所屬用戶名稱,相似交由chown命令進行處理
Ø group --- 定義所擁有文件/目錄的所屬用戶組名稱,相似交由chown命令進行處理
Ø mode --- 定義文件或目錄的權限信息;就像使用/usr/bin/chmod 設定八進制數(如0644)
取消掉前導零可能會有意想不到的結果。
做爲1,8版本,能夠指定爲符號模式(如 u+rwx or u=rw,g=r,o=r)
Ø backup --- 建立備份文件包含時間戳信息,以便可以還原回原文件,在某種狀況原文件被覆蓋 錯誤時。
Ø force --- 默認爲yes,當遠程文件內容和源文件內容不一樣時,將覆蓋目標文件
若是爲no,文件將只被傳輸,在目標主機不存在此文件時
別名:thirsty
Ø content --- 當使用代替src參數時,將文件的內容直接設置爲指定值。遠端建立有指定內容文 件,這是隻是簡單值,對於任何複雜或者有格式化的內容,請切換到template模塊
Ø remote_src --- 若是爲 False,將搜索源信息在本地/管理及機器上
若是爲True,將到遠程/目標主機的機器上搜索
默認爲false,目前remote_src不支持遞歸拷貝
[root@m01 tmp]# ansible wuhuang -m shell -a "uptime;df -h" 172.16.1.31 | SUCCESS | rc=0 >> 18:01:51 up 8:15, 2 users, load average: 0.00, 0.00, 0.00 Filesystem Size Used Avail Use% Mounted on /dev/sda3 20G 1.5G 17G 9% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot
172.16.1.41 | SUCCESS | rc=0 >> 18:01:51 up 8:09, 2 users, load average: 0.08, 0.02, 0.01 Filesystem Size Used Avail Use% Mounted on /dev/sda3 20G 1.5G 17G 9% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot
172.16.1.7 | SUCCESS | rc=0 >> 18:01:52 up 8:08, 2 users, load average: 0.00, 0.00, 0.00 Filesystem Size Used Avail Use% Mounted on /dev/sda3 20G 1.6G 17G 9% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot 172.16.1.31:/data 20G 1.5G 17G 9% /data
172.16.1.8 | SUCCESS | rc=0 >> 18:01:52 up 8:07, 3 users, load average: 0.08, 0.02, 0.01 Filesystem Size Used Avail Use% Mounted on /dev/sda3 20G 1.6G 17G 9% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot 172.16.1.31:/data 20G 1.5G 17G 9% /data |
說明:支持特殊符號,-a裏面可接多個名,用分號分割 |
1)推送腳本並受權
[root@m01 scripts]# ansible wuhuang -m copy -a "src=./test.sh dest=/tmp mode=+x" |
2)運行腳本
[root@m01 scripts]# ansible wuhuang -m shell -a "/tmp/test.sh" 172.16.1.41 | SUCCESS | rc=0 >> backup
172.16.1.31 | SUCCESS | rc=0 >> nfs01
172.16.1.8 | SUCCESS | rc=0 >> web02
172.16.1.7 | SUCCESS | rc=0 >> web01 |
ansible wuhuang -m script -a "/server/scripts/test.sh"
[root@m01 scripts]# ansible wuhuang -m script -a "/server/scripts/test.sh" 172.16.1.7 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.1.7 closed.\r\n", "stdout": "web01\r\n", "stdout_lines": [ "web01" ] } 172.16.1.31 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.1.31 closed.\r\n", "stdout": "nfs01\r\n", "stdout_lines": [ "nfs01" ] } …… |
經過yum模塊安裝nmap軟件
ansible wuhuang -m yum -a "name=nmap state=installed"
ansible wuhuang -m service -a "name=crond state=stopped enabled=yes"
說明:此處的name是服務名,表示將crond中止,而且取消開機自啓動。
ansible wuhuang -m file "path=/tmp/ state=directory mode=0644"
ansible wuhuang -m file -a "dest=/server/scripts/test.sh mode=0644" ---將test.sh權限改成0644
ansible wuhuang -m file -a "dest=/tmp/wuhuang_01dir/ state=directory" ---建立目錄爲wuhuang_01dir
ansible wuhuang -m file -a "dest=/tmp/wuhuang_01file state=touch" ---建立文件爲wuhuang_01file
ansible wuhuang -m file -a "dest=/tmp/wuhuang_01file state=absent" ---刪除文件wuhuang_01file
ansible wuhuang -m file -a "src=/etc/hosts dest=/tmp/hosts state=link" ---建立連接文件
Ø owner --- 定義所擁有文件/目錄的所屬用戶名稱,相似交由chown命令進行處理
Ø group --- 定義所擁有文件/目錄的所屬用戶組名稱,相似交由chown命令進行處理
Ø mode --- 定義文件或目錄的權限信息;就像使用/usr/bin/chmod 設定八進制數(如0644)
取消掉前導零可能會有意想不到的結果。
做爲1,8版本,能夠指定爲符號模式(如 u+rwx or u=rw,g=r,o=r)
Ø path --- 文件路徑管理:(別名方式:dest,name)
Ø src --- 要連接的文件路徑(只能應用 state=link),接受絕對、相對以及不存在的路徑
相對路徑不能擴展
Ø state --- 若是指定參數爲directory
全部不存在的子目錄將會被建立,而且從1.7開始支持設置目錄權限
--- 若是指定參數爲file
若是文件不存在將不能被建立,若是想建立能夠參考copy和template模塊
--- 若是指定參數爲link,符號連接將被建立或更改。
--- 若是指定參數爲hard,便會建立出硬連接
--- 若是指定參數爲absent
目錄將被遞歸刪除以及文件,而連接將被取消連接。
請注意,定義文件不存在不會失敗,只是輸出沒有發生任何改變的結果
--- 若是指定參數爲touch
若是路徑不存在將建立一個空文件,若是文件或目錄存在將接收更新的文件訪問和修改
時間(相似於「touch」從命令行工做的方式)。
* * * * * /bin/sh /server/scripts/test.sh &>/dev/null
分 時 日 月 周 定時任務要完成什麼工做
l minute --- 定義分鐘信息
l hour --- 定義小時信息
l day --- 定義日期信息
l month --- 定義月份信息
l weekday --- 定義周幾信息
l job --- 定義定時任務要作什麼事
l name --- 對定時任務信息加上備註,從而避免建立多個重複的定時任務(根據定時任務備份判斷
是否生成一個新的定時任務)
l state --- 若設置爲present,表示建立定時任務(默認配置)
若設置爲absent,表示刪除指定定時任務
只能管理本身建立的定時任務,原本有的管理不了
l disabled --- 將定時任務信息,進行臨時關閉(添加註釋)
建立定時任務
ansible wuhuang -m cron -a "minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null'"
[root@m01 scripts]# ansible wuhuang -m cron -a "minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null'" 172.16.1.31 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "None" ] } 172.16.1.41 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "None" ] } …… |
[root@web01 tmp]# crontab -l #tongbu date */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #Ansible: None 0 0 * * * /bin/sh /servser/scripts/test.sh &>/dev/null |
ansible wuhuang -m cron -a "name='backup server02' minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null'"
[root@m01 scripts]# ansible wuhuang -m cron -a "name='backup server02' minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null'" 172.16.1.31 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "backup server02" ] } 172.16.1.41 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "backup server02" ] } …… |
[root@web01 tmp]# crontab -l #tongbu date */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #Ansible: backup server02 0 0 * * * /bin/sh /servser/scripts/test.sh &>/dev/null |
註釋掉定時任務,反之取消註釋
ansible wuhuang -m cron -a "name='backup server' minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null' disabled=yes"
[root@m01 scripts]# ansible wuhuang -m cron -a "name='backup server' minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null' disabled=yes" 172.16.1.41 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "backup server02", "backup server" ] } 172.16.1.31 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "backup server02", "backup server" ] } …… |
[root@web01 tmp]# crontab -l #tongbu date */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #Ansible: backup server02 0 0 * * * /bin/sh /servser/scripts/test.sh &>/dev/null #Ansible: backup server #0 0 * * * /bin/sh /servser/scripts/test.sh &>/dev/null |
刪除定時任務
ansible wuhuang -m cron -a "name='backup server02' minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null' state=absent"
[root@m01 scripts]# ansible wuhuang -m cron -a "name='backup server02' minute=0 hour=0 job='/bin/sh /servser/scripts/test.sh &>/dev/null' state=absent" 172.16.1.31 | SUCCESS => { "changed": false, "envs": [], "jobs": [] } 172.16.1.41 | SUCCESS => { "changed": false, "envs": [], "jobs": [] }
…… |
[root@web01 tmp]# crontab -l #tongbu date */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 已經沒有backup server02這一條 |
mount -t nfs 172.16.1.31:/data /mnt
l fstype --- 指定掛載時的文件系統類型
l opts --- 在掛載時,指定掛載參數信息
l path --- 指定掛載點路徑信息
l src --- 指定將什麼目錄或設備進行掛載
l state --- 若是爲state=mounted,在fstab文件中的設備將被激活掛載或適當配置,若是指定
mounted的掛載掛載點不存在,會建立
--- 若是爲state=unmounted,設備將被卸載並不會改變fstab文件信息
--- absent和present只處理fstab,但將不影響目前的掛載,若是指定mounted和掛載點不存在,掛載點將被建立,相似的,指定absent將移除掛載點目錄
ansible mount -m mount -a "state=mounted fstype=nfs src='172.16.1.31:/data/' path=/mnt/"
[root@m01 scripts]# ansible wuhuang -m debug 172.16.1.7 | SUCCESS => { "changed": false, "msg": "Hello world!" } 172.16.1.8 | SUCCESS => { "changed": false, "msg": "Hello world!" } 172.16.1.31 | SUCCESS => { "changed": false, "msg": "Hello world!" } 172.16.1.41 | SUCCESS => { "changed": false, "msg": "Hello world!" } |
說明:此模塊在執行過程當中打印語句,對於調試變量或表達式很是有用,而不必定會中止播放。與「When:」指令一塊兒調試很是有用。 |
ansible wuhuang -m setup
顯示遠程主機的全部信息(後面加-v顯示詳細信息),提取IP、或架構信息等,X86來判斷主機架構,
裝合適的軟件
ansible georhe -m setup -v
主要用於解決一些錯誤:如遠程主機hang住了,ansible會輸出少許信息(最多-vvvv)
規則二:冒號
CMD="echo"
yaml:
mykey:
每一個冒號後面必定要有一個空格(以冒號結尾不須要空格,表示文件路徑的模版能夠不須要空格)
規則三:短橫線
想要表示列表項,使用一個短橫槓加一個空格。多個項使用一樣的縮進級別做爲同一個列表的一部分
核心規則:有效的利用空格進行劇本的編寫,劇本編寫是不支持tab的
1. 劇本的開頭
- hosts: all <- all表示全部主機,能夠寫某個主機組; -(空格)hosts:(空格)all task: <- 劇本所要乾的事情; (空格)(空格)task: - command: echo hello oldboy linux. (空格)(空格)空格)(空格)-(空格)模塊名稱:(空格)模塊中對應的功能 |
2. 劇本任務定義名稱
- hosts: all <- all表示全部主機,能夠寫某個主機組; -(空格)hosts:(空格)all task: <- 劇本所要乾的事情; (空格)(空格)task: - command: echo hello oldboy linux. (空格)(空格)空格)(空格)-(空格)模塊名稱:(空格)模塊中對應的功能 |
3. 劇本任務編寫定時任務
# ansible all -m cron -a "name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'" |
劇本內容 - hosts: all tasks: - name: restart-network cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' |
# ansible-playbook -C /etc/ansible/network-restart.yml -vvvx 說明:測試劇本命令後面能夠跟多個-v進行調試檢查 |
4. 劇本任務編寫多個任務
- hosts: all tasks: - name: restart-network cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' - name: sync time cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1" |
5. 劇本任務編寫多個主機
- hosts: 172.16.1.7 tasks: - name: restart-network cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' - name: sync time cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1" - hosts: 172.16.1.31 tasks: - name: show ip addr to file shell: echo $(hostname -i) >> /tmp/ip.txt |
第一步:編寫劇本
[root@m01 ansible]# cat /etc/ansible/rsync.yml - hosts: 172.16.1.41 <- 處理指定服務器(all <- 處理全部服務器) tasks: <- 劇本所要乾的事情 - name: 1. rsyncd.conf 第一步:將配置文件複製的rsync服務器 copy: src=/etc/rsyncd.conf dest=/etc/ - name: 2. useradd rsync 第二步:建立備份目錄管理用戶 user: name: rsync shell: /sbin/nologin createhome: no state: present - name: 3. mkdir /backup 第三步:建立備份目錄 file: dest=/backup state=directory owner=rsync group=rsync - name: 4. mkdir /etc/rsync.password 第四步:建立認證用戶密碼文件 copy: src=/etc/rsync.password dest=/etc/ mode=0600 - name: 5. rsync --daemon 第五步:啓動 shell: rsync --daemon - name: 6. kaiji zidong run 第六步:開機自啓動 shell: echo "rsync --daemon" >>/etc/rc.local |
第二步:將對應文件放到劇本指定位置
[root@m01 ~]# ll /etc/rsync.password /etc/rsyncd.conf -rw-r--r-- 1 root root 451 Feb 4 11:47 /etc/rsyncd.conf -rw-r--r-- 1 root root 17 Feb 4 11:53 /etc/rsync.password |
第三步:對劇本進行語法檢查
[root@m01 ansible]# ansible-playbook --syntax-check /etc/ansible/rsync.yml
playbook: /etc/ansible/rsync.yml |
第四步:測試劇本
[root@m01 ansible]# ansible-playbook -C /etc/ansible/rsync.yml -vvv |
說明:測試劇本命令後面能夠跟多個-v進行調試檢查 |
第五步:執行劇本
[root@m01 ansible]# ansible-playbook /etc/ansible/rsync.yml
PLAY [172.16.1.41] *********************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************* ok: [172.16.1.41] TASK [1. rsyncd.conf] ******************************************************************************************************** ok: [172.16.1.41] TASK [2. useradd rsync] ****************************************************************************************************** ok: [172.16.1.41] TASK [3. mkdir /backup] ****************************************************************************************************** ok: [172.16.1.41] TASK [4. mkdir /etc/rsync.password] ****************************************************************************************** ok: [172.16.1.41] TASK [5. rsync --daemon] ***************************************************************************************************** changed: [172.16.1.41] TASK [6. kaiji zidong run] *************************************************************************************************** changed: [172.16.1.41] PLAY RECAP ******************************************************************************************************************* 172.16.1.41 : ok=7 changed=2 unreachable=0 failed=0 |