這是我參與8月更文挑戰的第7天javascript
@TOChtml
往期Ansible目錄 一、自動化運維工具-Ansible實戰指南 二、自動化運維工具-Ansible的Playbook的使用 三、自動化運維工具-Ansible的Roles的使用 四、B站學習連接java
ansible是新出現的自動化運維工具,基於Python開發,集合了衆多運維工具(puppet、chef、func、fabric)的優勢,實現了批量系統配置、批量程序部署、批量運行命令等功能。node
簡單來講ansible是一種架構,自己沒有批量部署的能力批量部署能力是由模塊來提供的!並且不須要在被控制的主機上安裝任何東西,由於ansible是經過ssh協議來與遠程主機通信的python
參考個人另外一篇連接,經過yum來安裝ansibleblog.csdn.net/qq_45714272…linux
安裝目錄以下(yum安裝): 配置文件目錄:/etc/ansible/ 執行文件目錄:/usr/bin/ Lib庫依賴目錄:/usr/lib/pythonX.X/site-packages/ansible/ Help文檔目錄:/usr/share/doc/ansible-X.X.X/ Man文檔目錄:/usr/share/man/man1/nginx
ansible 的配置文件爲/etc/ansible/ansible.cfg,ansible 有許多參數,下面咱們列出一些常見的參數:shell
inventory = /etc/ansible/hosts #這個參數表示資源清單inventory文件的位置
library = /usr/share/ansible #指向存放Ansible模塊的目錄,支持多個目錄方式,只要用冒號(:)隔開就能夠
forks = 5 #併發鏈接數,默認爲5
sudo_user = root #設置默認執行命令的用戶
remote_port = 22 #指定鏈接被管節點的管理端口,默認爲22端口,建議修改,可以更加安全
host_key_checking = False #設置是否檢查SSH主機的密鑰,值爲True/False。關閉後第一次鏈接不會提示配置實例
timeout = 60 #設置SSH鏈接的超時時間,單位爲秒
log_path = /var/log/ansible.log #指定一個存儲ansible日誌的文件(默認不記錄日誌)
複製代碼
有多種定義方式:apache
1、 直接指明主機地址或主機名:
192.168.100.10
2、定義一個組名,把主機加進去,固然也能夠用通配符來匹配!
[test]
192.168.100.[1:3]0
....
...
...
"/etc/ansible/hosts" 57L, 1169C 1,1
複製代碼
Ansible執行的時候根據結果會顯示爲綠色(成功執行),黃色(成功伴隨狀態改變)和紅色(執行失敗)等顏色,顏色的顯示與changed的狀態相關聯,並能夠在ansible.cfg中進行定製顏色的設定。vim
/usr/bin/ansible Ansibe AD-Hoc 臨時命令執行工具,經常使用於臨時命令的執行 /usr/bin/ansible-doc Ansible 模塊幫助文檔 /usr/bin/ansible-galaxy 下載/上傳優秀代碼或Roles模塊 的官網平臺 /usr/bin/ansible-playbook Ansible 任務集編排工具 /usr/bin/ansible-pull Ansible遠程執行命令的工具,拉取配置而非推送配置(使用較少,海量機器時使用,對運維的架構能力要求較高) /usr/bin/ansible-vault Ansible 文件加密工具 /usr/bin/ansible-console Ansible基於Linux Consoble界面可與用戶交互的命令執行工具
其中,咱們比較經常使用的是/usr/bin/ansible和/usr/bin/ansible-playbook。
[root@ansible ~]# ansible-doc
Usage: ansible-doc [options] [module...]
Options:
-h, --help show this help message and exit # 顯示命令參數API文檔
-l, --list List available modules #列出可用的模塊
-M MODULE_PATH, --module-path=MODULE_PATH #指定模塊的路徑
specify path(s) to module library (default=None) -s, --snippet Show playbook snippet for specified module(s) #顯示playbook制定模塊的用法 -v, --verbose verbose mode (-vvv for more, -vvvv to enable # 顯示ansible-doc的版本號查看模塊列表: connection debugging) --version show program's version number and exit 複製代碼
例如:
[root@ansible ansible]# ansible-doc -l |grep nginx
nginx_status_info Retrieve information on nginx stat...
nginx_status_facts Retrieve nginx status facts
[root@ansible ansible]# ansible-doc -s nginx_status_info
- name: Retrieve information on nginx status.
nginx_status_info:
timeout: # HTTP connection timeout in seconds.
url: # (required) URL of the nginx status.
複製代碼
命令的具體格式以下: ansible host-pattern -m 模塊名 -a ‘參數’
經過ansible -h查看
[root@ansible ansible]# ansible -h
-a MODULE_ARGS #模塊的參數,若是執行默認COMMAND的模塊,便是命令參數,如: 「date」,「pwd」等等
-k,--ask-pass #ask for SSH password。登陸密碼,提示輸入SSH密碼而不是假設基於密鑰的驗證
--ask-su-pass #ask for su password。su切換密碼
-K,--ask-sudo-pass #ask for sudo password。提示密碼使用sudo,sudo表示提權操做
--ask-vault-pass #ask for vault password。假設咱們設定了加密的密碼,則用該選項進行訪問
-B SECONDS #後臺運行超時時間
-C #模擬運行環境並進行預運行,能夠進行查錯測試
-c CONNECTION #鏈接類型使用
-f FORKS #並行任務數,默認爲5
-i INVENTORY #指定主機清單的路徑,默認爲/etc/ansible/hosts
--list-hosts #查看有哪些主機組
-m MODULE_NAME #執行模塊的名字,默認使用 command 模塊,因此若是是隻執行單一命令能夠不用 -m參數
-o #壓縮輸出,嘗試將全部結果在一行輸出,通常針對收集工具使用
-S #用 su 命令
-R SU_USER #指定 su 的用戶,默認爲 root 用戶
-s #用 sudo 命令
-U SUDO_USER #指定 sudo 到哪一個用戶,默認爲 root 用戶
-T TIMEOUT #指定 ssh 默認超時時間,默認爲10s,也可在配置文件中修改
-u REMOTE_USER #遠程用戶,默認爲 root 用戶
-v #查看詳細信息,同時支持-vvv,-vvvv可查看更詳細信息
複製代碼
#1.生成私鑰
[root@ansible ~]# ssh-keygen
#2.向主機分發私鑰
[root@ansible ~]# ssh-copy-id root@192.168.100.10
[root@ansible ~]# ssh-copy-id root@192.168.100.20
複製代碼
[root@ansible ~]# ansible 192.168.100.10 -m ping
192.168.100.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
複製代碼
這樣就說明咱們的主機是連通狀態的。接下來的操做才能夠正常進行。
shell模塊很經常使用,它能夠在遠程主機上調用shell解釋器運行命令
[root@ansible ~]# head -10 /etc/ansible/hosts
[test]
192.168.100.[1:3]0
[wsr]
192.168.100.10 http_port=81
192.168.100.20 http_port=82
[wsr:vars]
nodename=mail
domainname=edu
[root@ansible ~]# ansible wsr -m shell -a 'ls -l'
192.168.100.10 | CHANGED | rc=0 >>
total 188
drwxr-xr-x. 2 root root 6 Aug 11 2020 a
-rw-------. 1 root root 1260 Jun 3 2020 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Aug 11 2020 b
-rw-r--r-- 1 root root 13 Jun 21 04:09 f4
-rw-r--r-- 1 root root 6540 Jun 21 04:22 log.tar.xz
lrwxrwxrwx. 1 root root 17 Jun 20 22:30 selinux -> ../selinux/config
drwxr-xr-x. 2 root root 25 Jun 20 22:08 sh
-rw-r--r-- 1 root root 175412 Jun 22 03:53 vsftpd-3.0.2-25.el7.x86_64.rpm
192.168.100.20 | CHANGED | rc=0 >>
total 188
drwxr-xr-x. 2 root root 6 Aug 11 2020 a
-rw-------. 1 root root 1260 Jun 3 2020 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Aug 11 2020 b
-rw-r--r-- 1 root root 13 Jun 21 04:09 f4
-rw-r--r-- 1 root root 6516 Jun 21 04:22 log.tar.xz
-rw-r--r-- 1 root root 175412 Jun 22 03:53 vsftpd-3.0.2-25.el7.x86_64.rpm
# 總結
<font color=#999AAA >提示:
複製代碼
這個模塊能夠直接在遠程主機上執行命令,並將結果返回本主機。
命令模塊接受命令名稱,後面是空格分隔的列表參數。給定的命令將在全部選定的節點上執行。它不會經過shell進行處理,好比$HOME和操做如"<",">","|",";","&"工做(須要使用(shell)模塊實現這些功能)。
下面來看一看該模塊下經常使用的幾個命令:
用來作判斷
[root@ansible ~]# ansible wsr -m command -a 'chdir=/data ls'
192.168.100.20 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
for3.conf
httpd.conf
192.168.100.10 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
for3.conf
httpd.conf
複製代碼
這個模塊用於將文件複製到遠程主機,同時支持給定內容生成文件和修改權限等。 其相關選項以下:
其中src,dest,backup,mode用的較多
[root@ansible ~]# touch 2021710
[root@ansible ~]# ansible wsr -m copy -a 'src=2021710 dest=/data/2021710'
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/data/2021710",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1625931089.28-8051-80193366038951/source",
"state": "file",
"uid": 0
}
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/data/2021710",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1625931089.28-8052-82580429716175/source",
"state": "file",
"uid": 0
}
複製代碼
[root@ansible ~]# ansible wsr -m copy -a 'content="i am bad boy\n" backup=yes dest=/data/2021710 mode=666'
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/data/2021710.11493.2021-07-10@11:32:39~",
"changed": true,
"checksum": "444281122cde3d31fa394ffe2d29d9a1fa2411f3",
"dest": "/data/2021710",
"gid": 0,
"group": "root",
"md5sum": "e2ea0ae0489f588fd0e7adcdc361ff70",
"mode": "0666",
"owner": "root",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1625931158.27-8105-89064400502466/source",
"state": "file",
"uid": 0
}
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/data/2021710.11427.2021-07-10@11:32:39~",
"changed": true,
"checksum": "444281122cde3d31fa394ffe2d29d9a1fa2411f3",
"dest": "/data/2021710",
"gid": 0,
"group": "root",
"md5sum": "e2ea0ae0489f588fd0e7adcdc361ff70",
"mode": "0666",
"owner": "root",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1625931158.28-8106-201363635505942/source",
"state": "file",
"uid": 0
}
複製代碼
其中一大串名字的就是備份後的名字,也能夠看到權限爲666
[root@ansible ~]# ansible wsr -m shell -a 'ls -l /data'
192.168.100.10 | CHANGED | rc=0 >>
total 32
-rw-rw-rw- 1 root root 13 Jul 10 11:32 2021710
-rw-r--r-- 1 root root 0 Jul 10 11:31 2021710.11493.2021-07-10@11:32:39~
-rw-rw-rw- 1 root root 21 Jul 10 11:20 a.sh
-rw-r--r-- 1 root root 0 Jul 3 23:14 file1
-rw-r--r-- 1 root root 0 Jul 3 23:14 file2
-rw-r--r-- 1 root root 0 Jul 3 23:14 file3
-rw-r--r-- 1 root root 63 Jul 4 11:51 for1.conf
-rw-r--r-- 1 root root 267 Jul 4 12:06 for2.conf
-rw-r--r-- 1 root root 171 Jul 5 04:05 for3.conf
-rw-r--r-- 1 apache root 11753 Jul 10 03:21 httpd.conf
192.168.100.20 | CHANGED | rc=0 >>
total 32
-rw-rw-rw- 1 root root 13 Jul 10 11:32 2021710
-rw-r--r-- 1 root root 0 Jul 10 11:31 2021710.11427.2021-07-10@11:32:39~
-rw-rw-rw- 1 root root 21 Jul 10 11:20 a.sh
-rw-r--r-- 1 root root 0 Jul 3 23:14 file1
-rw-r--r-- 1 root root 0 Jul 3 23:14 file2
-rw-r--r-- 1 root root 0 Jul 3 23:14 file3
-rw-r--r-- 1 root root 63 Jul 4 11:51 for1.conf
-rw-r--r-- 1 root root 267 Jul 4 12:06 for2.conf
-rw-r--r-- 1 root root 171 Jul 5 04:05 for3.conf
-rw-r--r-- 1 apache root 11753 Jul 10 03:21 httpd.conf
複製代碼
經過yum裝包 其選項以下:
state默認爲present
[root@ansible ~]# ansible wsr -m yum -a 'name=vim'
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"vim"
]
},
"msg": "Repository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository epel is listed more than once in the configuration\nRepository epel-debuginfo is listed more than once in the configuration\nRepository epel-source is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n vim-enhanced x86_64 2:7.4.629-8.el7_9 updates 1.1 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.1 M\nInstalled size: 2.2 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n\nInstalled:\n vim-enhanced.x86_64 2:7.4.629-8.el7_9 \n\nComplete!\n"
]
}
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"vim"
]
},
"msg": "Repository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository epel is listed more than once in the configuration\nRepository epel-debuginfo is listed more than once in the configuration\nRepository epel-source is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n vim-enhanced x86_64 2:7.4.629-8.el7_9 updates 1.1 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.1 M\nInstalled size: 2.2 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n\nInstalled:\n vim-enhanced.x86_64 2:7.4.629-8.el7_9 \n\nComplete!\n"
]
}
複製代碼
改變state的狀態爲absent便可
[root@ansible ~]# ansible wsr -m yum -a 'name=vim state=absent'
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"removed": [
"vim"
]
},
"msg": "Repository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository epel is listed more than once in the configuration\nRepository epel-debuginfo is listed more than once in the configuration\nRepository epel-source is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running transaction check\n---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n vim-enhanced x86_64 2:7.4.629-8.el7_9 @updates 2.2 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 2.2 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n\nRemoved:\n vim-enhanced.x86_64 2:7.4.629-8.el7_9 \n\nComplete!\n"
]
}
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"removed": [
"vim"
]
},
"msg": "Repository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nRepository epel is listed more than once in the configuration\nRepository epel-debuginfo is listed more than once in the configuration\nRepository epel-source is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running transaction check\n---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n vim-enhanced x86_64 2:7.4.629-8.el7_9 @updates 2.2 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 2.2 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 1/1 \n\nRemoved:\n vim-enhanced.x86_64 2:7.4.629-8.el7_9 \n\nComplete!\n"
]
}
複製代碼
該模塊主要用於設置文件的屬性,好比建立文件、建立連接文件、刪除文件等。 下面是一些常見的命令:
force #須要在兩種狀況下強制建立軟連接,一種是源文件不存在,但以後會創建的狀況下;另外一種是目標軟連接已存在,須要先取消以前的軟鏈,而後建立新的軟鏈,有兩個選項:yes|no
group #定義文件/目錄的屬組。後面能夠加上mode:定義文件/目錄的權限
owner #定義文件/目錄的屬主。後面必須跟上path:定義文件/目錄的路徑
recurse #遞歸設置文件的屬性,只對目錄有效,後面跟上src:被連接的源文件路徑,只應用於state=link的狀況
dest #被連接到的路徑,只應用於state=link的狀況
state #狀態,有如下選項:
directory:若是目錄不存在,就建立目錄
file:即便文件不存在,也不會被建立
link:建立軟連接
hard:建立硬連接
touch:若是文件不存在,則會建立一個新的文件,若是文件或目錄已存在,則更新其最後修改時間
absent:刪除目錄、文件或者取消連接文件
複製代碼
[root@ansible ~]# ansible wsr -m file -a 'path=/data/2339 state=directory'
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/data/2339",
"size": 6,
"state": "directory",
"uid": 0
}
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/data/2339",
"size": 6,
"state": "directory",
"uid": 0
}
複製代碼
[root@ansible ~]# ansible wsr -m file -a 'path=/data/2340 src=2339 state=link'
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/data/2340",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 4,
"src": "2339",
"state": "link",
"uid": 0
}
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/data/2340",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 4,
"src": "2339",
"state": "link",
"uid": 0
}
測試
[root@ansible ~]# ansible wsr -a 'ls -l /data'
192.168.100.20 | CHANGED | rc=0 >>
total 32
-rw-rw-rw- 1 root root 13 Jul 10 11:32 2021710
-rw-r--r-- 1 root root 0 Jul 10 11:31 2021710.11427.2021-07-10@11:32:39~
drwxr-xr-x 2 root root 6 Jul 10 11:40 2339
lrwxrwxrwx 1 root root 4 Jul 10 11:42 2340 -> 2339
-rw-rw-rw- 1 root root 21 Jul 10 11:20 a.sh
-rw-r--r-- 1 root root 0 Jul 3 23:14 file1
-rw-r--r-- 1 root root 0 Jul 3 23:14 file2
-rw-r--r-- 1 root root 0 Jul 3 23:14 file3
-rw-r--r-- 1 root root 63 Jul 4 11:51 for1.conf
-rw-r--r-- 1 root root 267 Jul 4 12:06 for2.conf
-rw-r--r-- 1 root root 171 Jul 5 04:05 for3.conf
-rw-r--r-- 1 apache root 11753 Jul 10 03:21 httpd.conf
192.168.100.10 | CHANGED | rc=0 >>
total 32
-rw-rw-rw- 1 root root 13 Jul 10 11:32 2021710
-rw-r--r-- 1 root root 0 Jul 10 11:31 2021710.11493.2021-07-10@11:32:39~
drwxr-xr-x 2 root root 6 Jul 10 11:40 2339
lrwxrwxrwx 1 root root 4 Jul 10 11:42 2340 -> 2339
-rw-rw-rw- 1 root root 21 Jul 10 11:20 a.sh
-rw-r--r-- 1 root root 0 Jul 3 23:14 file1
-rw-r--r-- 1 root root 0 Jul 3 23:14 file2
-rw-r--r-- 1 root root 0 Jul 3 23:14 file3
-rw-r--r-- 1 root root 63 Jul 4 11:51 for1.conf
-rw-r--r-- 1 root root 267 Jul 4 12:06 for2.conf
-rw-r--r-- 1 root root 171 Jul 5 04:05 for3.conf
-rw-r--r-- 1 apache root 11753 Jul 10 03:21 httpd.conf
複製代碼
只需把state改成absent便可 [root@ansible ~]# ansible wsr -m file -a 'path=/data/2339 state=absent'
該模塊適用於管理cron計劃任務的。 其使用的語法跟咱們的crontab文件中的語法一致
[root@ansible ~]# ansible wsr -m cron -a 'name="echo words" minute=*/5 job="echo hello world"'
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"warningcron",
"None",
"echo words"
]
}
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"warningcron",
"None",
"echo words"
]
}
測試結果
[root@ansible ~]# ansible wsr -a 'crontab -l'
192.168.100.10 | CHANGED | rc=0 >>
#Ansible: warningcron
#* * * * 1,3,5 /usr/bin/wall FBI warning
#Ansible: None
#* * * * * /usr/bin/wall FBI warning
#Ansible: echo words
*/5 * * * * echo hello world
192.168.100.20 | CHANGED | rc=0 >>
#Ansible: warningcron
#* * * * 1,3,5 /usr/bin/wall FBI warning
#Ansible: None
#* * * * * /usr/bin/wall FBI warning
#Ansible: echo words
*/5 * * * * echo hello world
複製代碼
mission completely!!!
該模塊主要是用來管理帳戶的
[root@ansible ~]# ansible wsr -m user -a 'name=zs uid=66666'
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1005,
"home": "/home/zs",
"name": "zs",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 66666
}
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1005,
"home": "/home/zs",
"name": "zs",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 66666
}
複製代碼
[root@ansible ~]# ansible wsr -m shell -a 'getent passwd|grep zs'
192.168.100.10 | CHANGED | rc=0 >>
zs:x:66666:1005::/home/zs:/bin/bash
192.168.100.20 | CHANGED | rc=0 >>
zs:x:66666:1005::/home/zs:/bin/bash
複製代碼
指定狀態爲absent便可
[root@ansible ~]# ansible wsr -m user -a 'name=zs uid=66666 state=absent'
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "zs",
"remove": false,
"state": "absent"
}
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "zs",
"remove": false,
"state": "absent"
}
複製代碼
[root@ansible ~]# ansible wsr -m shell -a 'getent passwd|grep zs'
192.168.100.10 | FAILED | rc=1 >>
non-zero return code
192.168.100.20 | FAILED | rc=1 >>
non-zero return code
複製代碼
該模塊主要用於添加或刪除組。 經常使用的選項以下:
[root@ansible ~]# ansible wsr -m group -a 'name=ls gid=888888'
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 888888,
"name": "ls",
"state": "present",
"system": false
}
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 888888,
"name": "ls",
"state": "present",
"system": false
}
複製代碼
[root@ansible ~]# ansible wsr -m shell -a 'cat /etc/group |grep 888888'
192.168.100.10 | CHANGED | rc=0 >>
ls:x:888888:
192.168.100.20 | CHANGED | rc=0 >>
ls:x:888888:
複製代碼
指定state爲absent便可刪除
[root@ansible ~]# ansible wsr -m group -a 'name=ls state=absent'
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "ls",
"state": "absent"
}
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "ls",
"state": "absent"
}
複製代碼
該模塊用於將本機的腳本在被管理端的機器上運行。
[root@ansible tmp]# cat df.sh
#!/bin/bash
date >> /tmp/disk_total.log
df -lh >> /tmp/disk_total.log
複製代碼
[root@ansible tmp]# ansible wsr -m script -a '/tmp/df.sh'
192.168.100.10 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.100.10 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.100.10 closed."
],
"stdout": "",
"stdout_lines": []
}
192.168.100.20 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.100.20 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.100.20 closed."
],
"stdout": "",
"stdout_lines": []
}
複製代碼
測試
[root@ansible tmp]# ansible wsr -a 'cat /tmp/disk_total.log'
192.168.100.10 | CHANGED | rc=0 >>
Sat Jul 10 21:24:51 EDT 2021
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 8.0G 1.5G 6.6G 18% /
/dev/sr0 4.4G 4.4G 0 100% /mnt/centos
/dev/sda1 1014M 136M 879M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.100.20 | CHANGED | rc=0 >>
Sat Jul 10 21:24:51 EDT 2021
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 8.0G 1.5G 6.6G 18% /
/dev/sr0 4.4G 4.4G 0 100% /mnt/centos
/dev/sda1 1014M 136M 879M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
複製代碼
經過調用facts(facts就是變量,內建變量)組件來收集主機信息,如vcpu個數,內存大小等.....
能夠使用filter來查看指定信息,調用後返回不少對應主機的信息,在後面的操做中能夠根據不一樣的信息來作不一樣的操做 如redhat系列用yum安裝,而debian系列用apt來安裝軟件。
[root@ansible tmp]# ansible wsr -m setup -a 'filter=*cpu*'
192.168.100.20 | SUCCESS => {
"ansible_facts": {
"ansible_processor_vcpus": 2,
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
192.168.100.10 | SUCCESS => {
"ansible_facts": {
"ansible_processor_vcpus": 2,
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
複製代碼
該模塊用於服務程序的管理。 其主要選項以下:
[root@ansible tmp]# ansible wsr -m service -a 'name=httpd state=restarted enabled=true'
192.168.100.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "basic.target remote-fs.target system.slice systemd-journald.socket network.target tmp.mount nss-lookup.target -.mount",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "18446744073709551615",
"CanIsolate": "no",
"CanReload": "yes",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "18446744073709551615",
"ConditionResult": "no",
"ConditionTimestampMonotonic": "0",
"Conflicts": "shutdown.target",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "The Apache HTTP Server",
"DevicePolicy": "auto",
"Documentation": "man:httpd(8) man:apachectl(8)",
"EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "0",
"ExecMainStartTimestampMonotonic": "0",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/usr/lib/systemd/system/httpd.service",
"GuessMainPID": "yes",
"IOScheduling": "0",
"Id": "httpd.service",
"IgnoreOnIsolate": "no",
"IgnoreOnSnapshot": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestampMonotonic": "0",
"InactiveExitTimestampMonotonic": "0",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "0",
"KillMode": "control-group",
"KillSignal": "18",
"LimitAS": "18446744073709551615",
"LimitCORE": "18446744073709551615",
"LimitCPU": "18446744073709551615",
"LimitDATA": "18446744073709551615",
"LimitFSIZE": "18446744073709551615",
"LimitLOCKS": "18446744073709551615",
"LimitMEMLOCK": "65536",
"LimitMSGQUEUE": "819200",
"LimitNICE": "0",
"LimitNOFILE": "4096",
"LimitNPROC": "3795",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "3795",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "0",
"MemoryAccounting": "no",
"MemoryCurrent": "18446744073709551615",
"MemoryLimit": "18446744073709551615",
"MountFlags": "0",
"Names": "httpd.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "main",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PermissionsStartOnly": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "yes",
"ProtectHome": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"Requires": "-.mount basic.target",
"RequiresMountsFor": "/var/tmp",
"Restart": "no",
"RestartUSec": "100ms",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitInterval": "10000000",
"StartupBlockIOWeight": "18446744073709551615",
"StartupCPUShares": "18446744073709551615",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "dead",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "no",
"TasksCurrent": "18446744073709551615",
"TasksMax": "18446744073709551615",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "notify",
"UMask": "0022",
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"Wants": "system.slice",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
192.168.100.10 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "remote-fs.target nss-lookup.target tmp.mount basic.target -.mount systemd-journald.socket system.slice network.target",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "18446744073709551615",
"CanIsolate": "no",
"CanReload": "yes",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "18446744073709551615",
"ConditionResult": "no",
"ConditionTimestampMonotonic": "0",
"Conflicts": "shutdown.target",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "The Apache HTTP Server",
"DevicePolicy": "auto",
"Documentation": "man:httpd(8) man:apachectl(8)",
"EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "0",
"ExecMainStartTimestampMonotonic": "0",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/usr/lib/systemd/system/httpd.service",
"GuessMainPID": "yes",
"IOScheduling": "0",
"Id": "httpd.service",
"IgnoreOnIsolate": "no",
"IgnoreOnSnapshot": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestampMonotonic": "0",
"InactiveExitTimestampMonotonic": "0",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "0",
"KillMode": "control-group",
"KillSignal": "18",
"LimitAS": "18446744073709551615",
"LimitCORE": "18446744073709551615",
"LimitCPU": "18446744073709551615",
"LimitDATA": "18446744073709551615",
"LimitFSIZE": "18446744073709551615",
"LimitLOCKS": "18446744073709551615",
"LimitMEMLOCK": "65536",
"LimitMSGQUEUE": "819200",
"LimitNICE": "0",
"LimitNOFILE": "4096",
"LimitNPROC": "3795",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "3795",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "0",
"MemoryAccounting": "no",
"MemoryCurrent": "18446744073709551615",
"MemoryLimit": "18446744073709551615",
"MountFlags": "0",
"Names": "httpd.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "main",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PermissionsStartOnly": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "yes",
"ProtectHome": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"Requires": "basic.target -.mount",
"RequiresMountsFor": "/var/tmp",
"Restart": "no",
"RestartUSec": "100ms",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitInterval": "10000000",
"StartupBlockIOWeight": "18446744073709551615",
"StartupCPUShares": "18446744073709551615",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "dead",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "no",
"TasksCurrent": "18446744073709551615",
"TasksMax": "18446744073709551615",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "notify",
"UMask": "0022",
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"Wants": "system.slice",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
複製代碼