常用模塊
Ansible默認提供了很多模塊來供我們使用。在Linux中,我們可以通過 ansible-doc -l 命令查看到當前Ansible支持哪些模塊,通過 ansible-doc -s [模塊名] 又可以查看該模塊有哪些參數可以使用。
ansible [主機或組] -m [模塊名] -a ['模塊參數'] [ansible參數]
ansible-doc -l # 命令查看到當前 ansible 都支持哪些模塊
ansible-doc -s [模塊名] # 查看該模塊有哪些參數可以使用
自動化運維工具Ansible常用模塊按功能可分為:
雲模塊、集群模塊、 命令模塊、數據庫模塊、文件模塊、資產模塊、消息模塊、監控模塊、網絡模塊、通知模塊、包管理模塊、源碼控制模塊、系統模塊、單元模塊、web設施模塊、windows模塊。
具體模塊可參考官網(http://docs.ansible.com/ansible/latest/list_of_all_modules.html)。
這里從官方分類的模塊里選擇最常用的一些模塊進行介紹。
一、command模塊
1、簡介
- command模塊用於在給的的節點上運行系統命令,比如echo hello
- 該模塊通過-a跟上要執行的命令可以直接執行,它不會通過shell進行處理,因此諸如$ HOME和諸如“<”,“>”,“|”,“;”和“&”之類的操作將不起作用,也就是在command模塊中無法使用管道符(如果需要這些功能,請使用shell模塊)
- 對於Windows目標,請改用win_command模塊
2、參數
chdir:運行command命令前先cd到這個目錄
creates:如果這個參數對應的文件存在,就不運行command
removes:如果這個參數對應的文件不存在,就不運行command,與creates參數的作用相反
free_form:需要執行的腳本(沒有真正的參數為free_form)
stdin(2.4后新增):將命令的stdin設置為指定的值
warn(1.8后新增):如果command_warnings在ansible.cfg中打開,如果設置為,則不要警告有關此特定行no
3、示例
(1)列出指定目錄下的文件
# 在遠程主機上創建test.sh腳本
[root@Client ~]# cat /root/test.sh
#!/bin/bash
i=0
echo $((i+1))
[root@Ansible ~]# ansible web -m command -a 'ls /root'
192.168.8.66 | SUCCESS | rc=0 >>
anaconda-ks.cfg
test.sh
# 執行遠程主機上的test.sh腳本
[root@Ansible ~]# ansible web -m command -a 'ls /root creates=test.sh'
192.168.8.66 | SUCCESS | rc=0 >>
skipped, since test.sh exists
[root@Ansible ~]# ansible web -m command -a 'ls /root removes=test.sh'
192.168.8.66 | SUCCESS | rc=0 >>
anaconda-ks.cfg
test.sh
說明:首先切換目錄到/root 目錄中,然后查看test.sh是否存在,如果存在,那么命令不會執行;如果不存在,那么執行命令。
在這里也可以看到,命令是必須存在的,但是沒有參數名為free_form參數。
(2)切換目錄執行命令
# 查看遠程主機上的test.sh腳本
[root@Ansible ~]# ansible web -m command -a 'cat test.sh chdir=/root'
192.168.8.66 | SUCCESS | rc=0 >>
#!/bin/bash
i=0
echo $((i+1))
[root@Ansible ~]# ansible web -m command -a 'sh test.sh chdir=/root'
192.168.8.66 | SUCCESS | rc=0 >>
1
(3)無法使用管道符
[root@Ansible ~]# ansible web -m command -a 'ls /root | grep test.sh'
192.168.8.66 | FAILED | rc=2 >>
test.sh
/root:
anaconda-ks.cfg
test.shls: 無法訪問|: 沒有那個文件或目錄
ls: 無法訪問grep: 沒有那個文件或目錄non-zero return code
4、注意事項
- 若要通過shell運行一個命令,比如<, >, |等,實際上我們需要shell模塊
- command模塊更安全,因為它不受用戶環境的影響
- 從版本2.4開始,executable參數被刪除。如果你需要此參數,請改用shell模塊
二、raw模塊
1、簡介
- raw模塊執行一個原始的命令,而不是通過模塊子系統。這對我們來說很有用,而且只能在兩種情況下完成。第一種情況是在較老的(Python 2.4和之前的版本)主機上安裝python-simplejson,因為幾乎所有核心模塊都需要它,作為運行模塊的依賴項。另一種是對任何沒有安裝任何Python的設備(如路由器)進行對話。在任何其他情況下,使用shell或命令模塊更合適。給raw的參數直接通過配置的遠程shell運行。在可用時返回標准輸出、錯誤輸出和返回代碼。該模塊沒有更改處理程序支持
- 用法和shell 模塊一樣 ,也可以執行任意命令,就像在本機執行一樣
- raw和command模塊類似,兩個模塊都是調用遠程主機的指令,但是raw支持管道(|)命令
- 該模塊不需要遠程系統上的python,就像腳本模塊一樣。該模塊也支持Windows目標
2、參數
executable:改變用來執行命令的shell,應該是可執行文件的絕對路徑
free_form:需要執行的腳本(沒有真正的參數為free_form)
3、示例
[root@Ansible ~]# ansible web -m raw -a "hostname"
192.168.8.55 | SUCCESS | rc=0 >>
Ansible
Shared connection to 192.168.8.55 closed.
192.168.8.66 | SUCCESS | rc=0 >>
Client
Shared connection to 192.168.8.66 closed.
[root@Ansible ~]# ansible web -m raw -a "ifconfig ens33|sed -n 2p|cut -d' ' -f10"
192.168.8.55 | SUCCESS | rc=0 >>
192.168.8.55
Shared connection to 192.168.8.55 closed.
192.168.8.66 | SUCCESS | rc=0 >>
192.168.8.66
Shared connection to 192.168.8.66 closed.
4、注意事項
- 如果要安全可靠地執行命令,最好使用shell或command模塊來代替
- 如果從playbook中使用raw,則可能需要使用gather_facts: no禁用事實收集
三、shell模塊
1、簡介
- shell模塊讓遠程主機在shell模塊下執行任何命令,從而支持shell的特性,如管道等
- 與command模塊幾乎相同,就像在本機執行一樣,但通過遠程節點上的shel(/bin/sh)運行命令
2、參數
chdir:運行shell命令前先cd到這個目錄
creates:如果這個參數對應的文件存在,就不運行shell
removes:如果這個參數對應的文件不存在,就不運行shell,與creates參數的作用相反
executable:改變用來執行命令的shell,應該是可執行文件的絕對路徑
free_form:需要執行的腳本(沒有真正的參數為free_form)
stdin(2.4后新增):將命令的stdin設置為指定的值
warn(1.8后新增):如果command_warnings在ansible.cfg中打開,如果設置為,則不要警告有關此特定行no
3、示例
切換目錄,執行命令並保持輸出
# 在遠程主機上創建test.sh腳本
[root@Client ~]# cat /root/test.sh
#!/bin/bash
i=0
echo $((i+1))
# 執行遠程主機上的test.sh腳本
[root@Ansible ~]# ansible web -m shell -a "sh test.sh > result chdir=/root"
192.168.8.66 | SUCCESS | rc=0 >>
[root@Ansible ~]# ansible web -m shell -a "cat result chdir=/root"
192.168.8.66 | SUCCESS | rc=0 >>
1
4、注意事項
- 如果你想安全可靠的執行命令,請使用command模塊,這也是編寫playbook的最佳實踐
四、script模塊
1、簡介
- script模塊的作用是將Ansible服務機上的script傳送到遠程主機之后再執行,原理類似於raw模塊
- 給定的腳本將通過遠程節點上的shell環境進行處理
- 該模塊在遠程系統上不需要python的支持
- 該模塊也支持Windows目標
2、參數
chdir(2.4后新增):運行script命令前先cd到這個目錄
creates(1.5后新增):如果這個參數對應的文件存在,就不運行script
removes(1.5后新增):如果這個參數對應的文件不存在,就不運行script,與creates參數的作用相反
decrypt(2.4后新增):此選項控制使用保管庫對源文件進行自動解密
free_form:需要執行腳本的本地文件路徑(沒有真正的參數為free_form)
3、示例
# 在Ansible服務器上創建一個腳本並賦予可執行權限
[root@Ansible ~]# cat script.sh
#!/bin/bash
a='Hello World'
echo $a
echo "這是我的Ansible服務器腳本"
touch test.txt
[root@Ansible ~]# chmod +x script.sh
[root@Ansible ~]# ansible web -m script -a "script.sh chdir=/tmp"
192.168.8.66 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.8.66 closed.\r\n",
"stdout": "Hello World\r\n這是我的Ansible服務器腳本\r\n",
"stdout_lines": [
"Hello World",
"這是我的Ansible服務器腳本"
]
}
# 在遠程主機上創建成功test.txt
[root@Ansible ~]# ansible web -m command -a "ls /tmp"
192.168.8.66 | SUCCESS | rc=0 >>
ansible_G9sPom
test.txt
4、注意事項
- 當腳本執行時,ssh連接插件將通過-tt強制偽tty分配。偽ttys沒有stderr通道,所有stderr被發送到標准輸出。如果需要標准輸出和標准錯誤分離,我們需要使用到copy模塊
五、copy模塊
1、簡介
- copy模塊是將本地或遠程機器上的文件拷貝到遠程主機上的某個位置
- 對於Windows目標,請改用win_copy模塊
2、參數
attributes(2.3后新增):文件或目錄應具有的屬性,該字符串應該包含與lsattr顯示的順序相同順序的屬性
backup:在覆蓋之前將原文件備份,備份文件包含時間信息,有兩個選項:yes|no
checksum(2.5后新增):正在傳輸的文件的SHA1校驗和。用於確定文件的副本是否成功
如果沒有提供,那么將使用src文件的本地計算校驗和
content:當用content代替src參數的時候,可以把文檔的內容直接設定指定文件的值
decrypt(2.4后新增):此選項控制使用保管庫對源文件進行自動解密
dest:必選項。要將源文件復制到的遠程主機的絕對路徑,如果源文件是一個目錄,那么該路徑也必須是個目錄
directory_mode(1.5后新增):在進行遞歸復制時,請設置目錄的模式。如果沒有設置,我們將使用系統默認值。該模式僅在新創建的目錄中設置,不會影響已存在的目錄
follow(1.8后新增):表示是否遵循目標機器中的文件系統鏈接(如果存在)
force:如果遠程主機包含該文件,但內容不同,如果設置為yes,則強制覆蓋,如果為no,則只有當遠程主機的目標位置不存在該文件時,才復制。默認為yes
group:設置文件或目錄的所屬組
owner:設置文件或目錄的所屬用戶
local_follow(2.4后新增):是否遵循本地機器中的文件系統鏈接(如果存在)
mode:設置文件權限,模式實際上是八進制數字(如0644),少了前面的零可能會有意想不到的結果。從版本1.8開始,可以將模式指定為符號模式(例如u+rwx或u=rw,g=r,o=r)
remote_src(2.0后新增):如果是no,它將在原始主機上搜索src;如果是yes,它會去src的目標機子上搜索src。默認是no
目前remote_src不支持遞歸復制
src:將本地路徑復制到遠程主機,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它將遞歸復制。在這種情況下,如果路徑使用"/"來結尾,則只復制目錄里的內容,如果沒有使用"/"來結尾,則包含目錄在內的整個內容全部復制,類似於rsync
unsafe_writes(2.2后新增):通常情況下,該模塊使用原子操作來防止數據損壞或從目標文件讀取不一致,有時系統會以防止這種情況的方式進行配置或破壞
validate:復制前是否檢驗需要復制目的地的路徑
others:所有的file模塊里的選項都可以在這里使用
3、示例
(1)backup復制前備份
復制本地文件到遠程主機並對原文件進行備份(第一次復制之后,對本地文件稍作修改,第二次復制時,就能進行遠程主機的備份)
[root@Ansible ~]# ansible web -m copy -a "src=/root/test.sh backup=yes dest=/root"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum": "e42a8fe379671479fbdad43a3b8737e3ada2f8be",
"dest": "/root/test.sh",
"gid": 0,
"group": "root",
"md5sum": "5636a7c4398fc86b919631387fc712e5",
"mode": "0644",
"owner": "root",
"size": 30,
"src": "/root/.ansible/tmp/ansible-tmp-1524816562.347795-100129918473420/source",
"state": "file",
"uid": 0
}
[root@Ansible ~]# echo "echo 'Hello world'" >> test.sh
[root@Ansible ~]# ansible web -m copy -a "src=/root/test.sh backup=yes dest=/root"
192.168.8.66 | SUCCESS => {
"backup_file": "/root/test.sh.25144.2018-04-27@16:10:37~", # 復制前的備份文件路徑
"changed": true,
"checksum": "4d8e92c604221322f38cc04d8d6e87e84799153e",
"dest": "/root/test.sh",
"gid": 0,
"group": "root",
"md5sum": "0418be97c2fd733a9fb2aaec5977b559",
"mode": "0644",
"owner": "root",
"size": 49,
"src": "/root/.ansible/tmp/ansible-tmp-1524816636.447237-270228680719764/source",
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible web -m command -a "ls -l /root"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 12
-rw-------. 1 root root 1382 4月 3 22:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 49 4月 27 16:10 test.sh
-rw-r--r-- 1 root root 30 4月 27 16:09 test.sh.25144.2018-04-27@16:10:37~ # 復制前的備份文件
(2)src和dest都是文件
[root@Ansible ~]# mkdir -p /root/dest/test
[root@Ansible ~]# touch /root/dest/test.sh
[root@Ansible ~]# ansible web -m copy -a "src=test.sh dest=/root/dest/test" # dest文件的父目錄不存在將報錯
192.168.8.66 | FAILED! => {
"changed": false,
"checksum": "e42a8fe379671479fbdad43a3b8737e3ada2f8be",
"msg": "Destination directory /root/dest does not exist"
}
[root@Ansible ~]# ansible web -m copy -a "src=test.sh dest=/root/dest/"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum": "e42a8fe379671479fbdad43a3b8737e3ada2f8be",
"dest": "/root/dest/test.sh",
"gid": 0,
"group": "root",
"md5sum": "5636a7c4398fc86b919631387fc712e5",
"mode": "0644",
"owner": "root",
"size": 30,
"src": "/root/.ansible/tmp/ansible-tmp-1524812376.162108-152354561278684/source",
"state": "file",
"uid": 0
}
(3)src是目錄
A、源目錄以/結尾,只拷貝了目錄下的內容
[root@Ansible ~]# ls /root/test/
123.txt test.sh
[root@Ansible ~]# ansible web -m copy -a "src=/root/test/ dest=/tmp/"
192.168.8.66 | SUCCESS => {
"changed": true,
"dest": "/tmp/",
"src": "/root/test/"
}
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 4
-rw-r--r-- 1 root root 0 4月 27 17:04 123.txt
drwx------ 2 root root 65 4月 27 17:05 ansible_ejRWFz
-rw-r--r-- 1 root root 49 4月 27 17:04 test.sh
B、源目錄未以/結尾,直接將src目錄本身拷貝到目標主機
[root@Ansible ~]# ls /root/test/
123.txt test.sh
[root@Ansible ~]# ansible web -m copy -a "src=/root/test dest=/tmp/"
192.168.8.66 | SUCCESS => {
"changed": true,
"dest": "/tmp/",
"src": "/root/test"
}
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 4
-rw-r--r-- 1 root root 0 4月 27 17:04 123.txt
drwx------ 2 root root 65 4月 27 17:07 ansible_ownGJc
drwxr-xr-x 2 root root 36 4月 27 17:07 test # 這是一個目錄
-rw-r--r-- 1 root root 49 4月 27 17:04 test.sh
(4)設置文件權限(owner/group/mode)
[root@Ansible ~]# ansible web -m copy -a "src=/root/test.sh dest=/root dest=/tmp owner=test group=test mode=0644"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum": "4d8e92c604221322f38cc04d8d6e87e84799153e",
"dest": "/tmp/test.sh",
"gid": 1001,
"group": "test",
"md5sum": "0418be97c2fd733a9fb2aaec5977b559",
"mode": "0644",
"owner": "test",
"size": 49,
"src": "/root/.ansible/tmp/ansible-tmp-1524817478.2402651-108214522667207/source",
"state": "file",
"uid": 1001
}
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 4
drwx------ 2 root root 65 4月 27 16:26 ansible_zczZtp
-rw-r--r-- 1 test test 49 4月 27 16:24 test.sh
(5)content參數
把目錄層次設置到特定的值,並輸出到指定文件中。
[root@Ansible ~]# ansible web -m copy -a "content='root \n dest \n' dest=/root/dest"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum": "87984f77e2ad345c677b5191d78d5caeb7d41316",
"dest": "/root/dest",
"gid": 0,
"group": "root",
"md5sum": "dc18f79caca390e17c89e1c850f9f8a5",
"mode": "0644",
"owner": "root",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1524818107.6488795-679046311890/source",
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible web -m command -a "cat /root/dest"
192.168.8.66 | SUCCESS | rc=0 >>
root
dest
(6)force參數
如果遠程主機包含該文件,但內容不同,如果設置為yes,則強制覆蓋,如果為no,則只有當遠程主機的目標位置不存在該文件時,才復制。
[root@Ansible ~]# ansible web -m copy -a "src=test.sh dest=/root/xztest force=no"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum": "4d8e92c604221322f38cc04d8d6e87e84799153e",
"dest": "/root/xztest",
"gid": 0,
"group": "root",
"md5sum": "0418be97c2fd733a9fb2aaec5977b559",
"mode": "0644",
"owner": "root",
"size": 49,
"src": "/root/.ansible/tmp/ansible-tmp-1524818671.0156403-161213944315961/source",
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible web -m copy -a "src=test.sh dest=/root/xztest force=no"
192.168.8.66 | SUCCESS => {
"changed": false,
"dest": "/root/xztest",
"src": "/root/test.sh"
}
[root@Ansible ~]# ansible web -m command -a "ls -l /root"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 8
-rw-------. 1 root root 1382 4月 3 22:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 49 4月 27 16:44 xztest
六、file模塊
1、簡介
- file模塊,設置文件、符號鏈接和目錄的屬性,或刪除文件、符號鏈接、目錄
- 許多其他模塊支持與file模塊相同的選項(包括復制、模板和匯編)
- 對於Windows目標,請改用win_file模塊
2、參數
attributes(2.3后新增):文件或目錄應具有的屬性。要獲得支持的標志,請查看目標系統上chattr的手冊頁。該字符串應該包含與lsattr顯示的順序相同順序的屬性
follow(1.8后新增):是否遵循目的機器中的文件系統鏈接,(如果存在)。在Ansible 2.5之前,默認是no
force:強制創建軟鏈接
group:設置文件或目錄的所屬組
owner:設置文件或目錄的所屬用戶
mode:設置文件權限,模式實際上是八進制數字(如0644),少了前面的零可能會有意想不到的結果。從版本1.8開始,可以將模式指定為符號模式(例如u+rwx或u=rw,g=r,o=r)
path:定義目標文件/目錄的路徑,也可以用dest、name代替
recurse:是否遞歸設置文件的屬性,只對目錄有效(僅適用於state=directory)
selevel:要被鏈接的源文件的路徑(僅適用於state=link)
dest:被鏈接到的路徑(僅適用於state=link)
src:要鏈接到的文件路徑(僅適用於state=link)
state:若果是directory,所有的子目錄將被創建(如果它們不存在);若是file,文件將不會被創建(如果文件不存在);link表示符號鏈接;若是absent,目錄、文件或鏈接會被遞歸刪除;touch代表生成一個空文件;hard代表硬鏈接
touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最后修改時間
unsafe_writes(2.2后新增):是否以不安全的方式進行,可能導致數據損壞
3、示例
(1)設置文件權限(owner/group/mode)
[root@Ansible ~]# ansible web -m command -a "ls -l /root/test.sh"
192.168.8.66 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 root root 31 4月 28 10:41 /root/test.sh
[root@Ansible ~]# ansible web -m file -a "path=/root/test.sh owner=test group=test mode=0777"
192.168.8.66 | SUCCESS => {
"changed": true,
"gid": 1001,
"group": "test",
"mode": "0777",
"owner": "test",
"path": "/root/test.sh",
"size": 31,
"state": "file",
"uid": 1001
}
[root@Ansible ~]# ansible web -m command -a "ls -l /root/test.sh"
192.168.8.66 | SUCCESS | rc=0 >>
-rwxrwxrwx 1 test test 31 4月 28 10:41 /root/test.sh
(2)創建空文件(state=touch)
[root@Ansible ~]# ansible web -m file -a "path=/tmp/xz_test state=touch mode=0644"
192.168.8.66 | SUCCESS => {
"changed": true,
"dest": "/tmp/xz_test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp/xz_test"
192.168.8.66 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 4月 28 10:49 /tmp/xz_test
(3)創建目錄(state=directory)
A、目錄是遞歸創建的
[root@Ansible ~]# ansible web -m file -a "path=/tmp/test/xzxs/ state=directory mode=0755"
192.168.8.66 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test/xzxs/",
"size": 6,
"state": "directory",
"uid": 0
}
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp/"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 0
drwx------ 2 root root 65 4月 28 10:56 ansible_iy9v52
drwxr-xr-x 3 root root 18 4月 28 10:56 test
-rw-r--r-- 1 root root 0 4月 28 10:49 xz_test
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp/test/"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 0
drwxr-xr-x 2 root root 6 4月 28 10:56 xzxs
B、目標文件不存在
state=file時會報錯,但是state=absent不會報錯,執行狀態也不會變化
[root@Ansible ~]# ansible web -m file -a "path=/tmp/test/xzxs/1.txt state=file mode=0644"
192.168.8.66 | FAILED! => {
"changed": false,
"msg": "file (/tmp/test/xzxs/1.txt) is absent, cannot continue",
"path": "/tmp/test/xzxs/1.txt",
"state": "absent"
}
[root@Ansible ~]# ansible web -m file -a "path=/tmp/test/xzxs/1.txt state=absent mode=0644"
192.168.8.66 | SUCCESS => {
"changed": false,
"path": "/tmp/test/xzxs/1.txt",
"state": "absent"
}
(4)創建軟鏈接(state=link)
[root@Ansible ~]# ansible web -m file -a "src=/etc/fstab dest=/tmp/123.txt state=link"
192.168.8.66 | SUCCESS => {
"changed": true,
"dest": "/tmp/123.txt",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 10,
"src": "/etc/fstab",
"state": "link",
"uid": 0
}
[root@Ansible ~]# ansible web -m command -a "ls -l /tmp/"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 0
lrwxrwxrwx 1 root root 10 4月 28 11:06 123.txt -> /etc/fstab # 創建軟鏈接
drwx------ 2 root root 65 4月 28 11:06 ansible_pfisoW
drwxr-xr-x 3 root root 18 4月 28 10:56 test
-rw-r--r-- 1 root root 0 4月 28 10:49 xz_test
(5)刪除文件、目錄或者軟鏈接(state=absent)
[root@Ansible ~]# ansible web -m file -a "path=/tmp/test/ state=absent"
192.168.8.66 | SUCCESS => {
"changed": true,
"path": "/tmp/test/",
"state": "absent"
}
[root@Ansible ~]# ansible web -m file -a "path=/tmp/xz_test state=absent"
192.168.8.66 | SUCCESS => {
"changed": true,
"path": "/tmp/xz_test",
"state": "absent"
}
[root@Ansible ~]# ansible web -m file -a "path=/tmp/123.txt state=absent"
192.168.8.66 | SUCCESS => {
"changed": true,
"path": "/tmp/123.txt",
"state": "absent"
}
七、ping模塊
1、簡介
- ping模塊用於確認和對象機器之間是否能夠ping通,正常情況會返回pong
- ping模塊在劇本中沒有任何意義,但從/usr/bin/ansible或者/ansible-2.5.0/bin/ansible可以驗證主機是否可以登錄
- 這不是ICMP ping,這只是一個簡單的測試模塊,需要遠程節點上的Python
- 對於Windows目標,請改用win_ping模塊
- 對於網絡目標,請改用net_ping模塊
2、參數
data:要返回的數據為ping返回值。如果此參數設置為crash,模塊將導致異常
3、示例
# 默認返回是pong
[root@Ansible ~]# ansible web -m ping
192.168.8.66 | SUCCESS => {
"changed": false,
"ping": "pong"
}
# 設置返回值是hello
[root@Ansible ~]# ansible web -m ping -a "data=hello"
192.168.8.66 | SUCCESS => {
"changed": false,
"ping": "hello"
}
# 設置返回值是crash,模塊導致異常
[root@Ansible ~]# ansible web -m ping -a "data=crash"
192.168.8.66 | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to 192.168.8.66 closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/tmp/ansible_oNemnu/ansible_module_ping.py\", line 84, in <module>\r\n main()\r\n File \"/tmp/ansible_oNemnu/ansible_module_ping.py\", line 74, in main\r\n raise Exception(\"boom\")\r\nException: boom\r\n",
"msg": "MODULE FAILURE",
"rc": 1
}
八、service模塊
1、簡介
- service模塊用於控制遠程主機的服務,說白了,就是Linux下的service命令
- 支持的init系統包括BSD init,OpenRC,SysV,Solaris SMF,systemd,upstart
- 對於Windows目標,請改用win_service模塊
2、參數
arguments:如果打開這個標記,backrefs會改變模塊的一些操作:insertbefore和insertafter參數會被忽略。當regexp不匹配文件中的任何行時,文件不會做任何修改,否則 使用擴展的line參數 替換 最后一個匹配正則表達式的行
enabled:服務是否開機自動啟動yes|no。enabled和state至少要有一個被定義
name:必選項,服務名稱
pattern:如果通過status指令來查看服務的狀態時,沒有響應,就會通過ps指令在進程中根據該模式進行查找,如果匹配到,則認為該服務依然在運
runlevel:運行級別
sleep(1.3后新增):如果服務被重新啟動,則睡眠多少秒再執行停止和啟動命令
state:對當前服務執行啟動,停止、重啟、重新加載等操作(started,stopped,restarted,reloaded)
use(2.2后新增):以哪個用戶的身份執行
3、示例
(1)啟動、停止、重啟或重載服務
# 啟動(started)
[root@Ansible ~]# ansible web -m service -a "name=httpd state=started"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
# 停止(stopped)
[root@Ansible ~]# ansible web -m service -a "name=httpd state=stopped"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "stopped",
"status": {
……
}
}
# 重啟(restarted)
[root@Ansible ~]# ansible web -m service -a "name=httpd state=restarted"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
# 重載(reloaded)
[root@Ansible ~]# ansible web -m service -a "name=httpd state=reloaded"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
**(2)設置服務開機自啟動**
[root@Ansible ~]# ansible web -m service -a "name=httpd enabled=yes"
192.168.8.66 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"status": {
……
}
}
九、systemd模塊
1、簡介
- systemd模塊用於控制遠程主機的systemd服務,說白了,就是Linux下的systemd命令。需要遠程主機支持systemd
- 用法和service模塊基本相同
2、參數
daemon_reload:在執行任何其他操作之前運行守護進程重新加載,以確保systemd已經讀取其他更改
enabled:服務是否開機自動啟動yes|no。enabled和state至少要有一個被定義
masked:是否將服務設置為masked狀態,被mask的服務是無法啟動的
name:必選項,服務名稱
no_block(2.3后新增):不要同步等待操作請求完成
state:對當前服務執行啟動,停止、重啟、重新加載等操作(started,stopped,restarted,reloaded)
user:使用服務的調用者運行systemctl,而不是系統的服務管理者
3、示例
(1)啟動、停止、重啟或重載服務
# 啟動(started)
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=started"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
# 停止(stopped)
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=stopped"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "stopped",
"status": {
……
}
}
# 重啟(restarted)
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=restarted"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
# 重載(reloaded)
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=reloaded"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
(2)設置服務masked狀態
# 先將服務停止
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=stopped"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "stopped",
"status": {
……
}
}
[root@Ansible ~]# ansible web -m systemd -a "name=httpd masked=yes"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"status": {
……
}
}
# 服務已無法啟動
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=started"
192.168.8.66 | FAILED! => {
"changed": false,
"msg": "Unable to start service httpd: Failed to start httpd.service: Unit is masked.\n"
}
# 撤銷mask
[root@Ansible ~]# ansible web -m systemd -a "name=httpd masked=no"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"status": {
……
}
}
# 可以啟動成功
[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=started"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
……
}
}
十、lineinfile模塊
1、簡介
- lineinfile模塊用於確保一個特定的行在一個文件中,或使用一個正則表達式替換現有的行
- 如果想要改變文件中相似的多行,可以使用replace模塊。如果想要插入、更新、刪除一個行塊,可以使用blockinfile模塊
2、參數
attributes(2.3后增加):文件或目錄應具有的屬性。要獲得支持的標志,請查看目標系統上chattr的手冊頁。該字符串應該包含與lsattr顯示的順序相同順序的屬性
backrefs:如果打開這個標記,backrefs會改變模塊的一些操作:insertbefore和insertafter參數會被忽略。當regexp不匹配文件中的任何行時,文件不會做任何修改,否則 使用擴展的line參數 替換 最后一個匹配正則表達式的行
backup:用於創建一個包含時間戳信息的備份文件。以便在錯誤的修改了文件的時候,能夠找回原始的文件
create:與state=present一起使用。如果指定了這個參數,當要修改的文件不存在的時候,會創建它。否則會報錯
firstmatch(2.5后增加):用於insertafter或insertbefore。如果設置,insertafter並inserbefore找到第一行有正則表達式匹配
group:設置文件/目錄的所屬組
insertafter:當regexp不匹配文件中的任何行的時候,會將新行插入到其所指定的正則表達式匹配的行中的最后一行的后面。insertafter也支持一個特殊的值:EOF(代表文件的末尾)。若沒有匹配的行,那么就會插入EOF
insertbefore:當regexp不匹配文件中的任何行的時候,會將line參數所指定的行,插入到insertbefore所指定的正則表達式匹配的行中的最后一行的前面,當insertbefore所指定的正則表達式不匹配任何行時,會插入到文件的末尾,同時insertbefore還可以是一個特殊的值:BOF(代表文件的開始);否則,會使用line參數所指定的行替換regexp所匹配的行中的最后一行
line:要插入或者替換的行。如果設置了backrefs參數,那么line中可以包含位置分組或命名分組,lineinfile模塊會使用regexp捕獲的分組填充它們
mode:設置文件權限,模式實際上是八進制數字(如0644),少了前面的零可能會有意想不到的結果。從版本1.8開始,可以將模式指定為符號模式(例如u+rwx或u=rw,g=r,o=r)
others:file模塊的其他參數可以在這里使用
owner:設置文件或目錄的所屬用戶
path:要修改的文件,也可以使用dest、destfile、name
regexp(1.7后增加):用於搜索文件中的每一行的正則表達式。對於state=present,這個正則表達式所匹配的行中的最后一行會被替換;對於state=present,會刪除所有匹配的行
state:用於設置 新增或替換一行,還是刪除行(present、absent)
unsafe_writes(2.2后增加):是否以不安全的方式進行,可能導致數據損壞
validate:復制前是否檢驗需要復制目的地的路徑
3、示例
(1)文本替換
將/etc/selinux/config文件中所有匹配^SELINUX=正則表達式的行中的最后一行使用SELINUX=disabled替換
如果regexp不匹配文件中的任何一行,則將line所指定的行插入到文件的末尾
[root@Ansible ~]# ansible web -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
# 查看結果
[root@Ansible ~]# ansible web -m command -a "cat /etc/selinux/config"
192.168.8.66 | SUCCESS | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled # 修改成功
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
(2)刪除行
將/tmp/test.sh文件中所有匹配^pwd的行刪除
# 刪除前內容
[root@Ansible ~]# ansible web -m command -a "cat /tmp/test.sh"
192.168.8.66 | SUCCESS | rc=0 >>
#!/bin/bash
echo "Hello world"
pwd
pwd1=123456789
pwd2=test
# 刪除行
[root@Ansible ~]# ansible web -m lineinfile -a "path=/tmp/test.sh regexp='^pwd' state=absent"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"found": 3,
"msg": "3 line(s) removed"
}
# 再次運行,沒有匹配
[root@Ansible ~]# ansible web -m lineinfile -a "path=/tmp/test.sh regexp='^pwd' state=absent"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": false,
"found": 0,
"msg": ""
}
# 刪除后
[root@Ansible ~]# ansible web -m command -a "cat /tmp/test.sh"
192.168.8.66 | SUCCESS | rc=0 >>
#!/bin/bash
echo "Hello world"
(3)替換行並設置文件權限
# 執行前
[root@Ansible ~]# ansible web -m command -a "cat /etc/hosts"
192.168.8.66 | SUCCESS | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.8.55 Ansible
192.168.8.66 Client
# 執行中
[root@Ansible ~]# ansible web -m lineinfile -a "path=/etc/hosts regexp='^127.0.0.1' line='127.0.0.1 localhost' owner=root group=root mode=0644"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
# 執行后
[root@Ansible ~]# ansible web -m command -a "cat /etc/hosts"
192.168.8.66 | SUCCESS | rc=0 >>
127.0.0.1 localhost
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.8.55 Ansible
192.168.8.66 Client
(4)insertafter和insertbefore
當文件中沒有匹配正則表達式^Listen80的行時,會將Listen 80插入到^#Listen所匹配的最后一行的后面
[root@Ansible ~]# ansible web -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen80' insertafter='^#Listen' line='Listen 80'"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line added"
}
# insertbefore的使用方法類似
[root@Ansible ~]# ansible web -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^#Listen80' insertbefore='^Listen 80' line='#Listen 80'"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line added"
}
(5)為文件新增一行
直接在文件中新增一行(如果line不存在則會插入),而不通過正則表達式進行匹配
[root@Ansible ~]# ansible web -m lineinfile -a "path=/root/test.sh line='xiaozuo test'"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line added"
}
# 再次執行
[root@Ansible ~]# ansible web -m lineinfile -a "path=/root/test.sh line='xiaozuo test'"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": false,
"msg": ""
}
# 執行后的文件
[root@Ansible ~]# ansible web -m command -a "cat /root/test.sh"
192.168.8.66 | SUCCESS | rc=0 >>
#!/bin/bash
echo 'Hello world'
xiaozuo test # 新增的行
(6)backrefs用法
backrefs為no時,如果沒有匹配,則添加一行line。如果匹配了,則把匹配內容替被換為line內容。
backrefs為yes時,如果沒有匹配,則文件保持不變。如果匹配了,把匹配內容替被換為line內容。
# backrefs為yes時,匹配到了,並把匹配內容替被換為line內容
[root@Ansible ~]# ansible web -m lineinfile -a "path=/root/test.sh line='xiaozuo xiansen' regexp='^xiaozuo' backrefs=yes"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
# backrefs為yes時,沒有匹配到,則文件保持不變
[root@Ansible ~]# ansible web -m lineinfile -a "path=/root/test.sh line='xiaozuo xiansen' regexp='^xiaozuo1' backrefs=yes"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": false,
"msg": ""
}
# backrefs為no時,沒有匹配到,則添加一行line
[root@Ansible ~]# ansible web -m lineinfile -a "path=/root/test.sh line='xiaozuo xiansen' regexp='^xiaozuo1' backrefs=no"
192.168.8.66 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line added"
}
# 執行后的文件
[root@Ansible ~]# ansible web -m command -a "cat /root/test.sh"
192.168.8.66 | SUCCESS | rc=0 >>
#!/bin/bash
echo 'Hello world'
xiaozuo xiansen
xiaozuo xiansen
十一、setup模塊
1、簡介
setup模塊被劇本自動調用,以收集有關可用於劇本的遠程主機的有用變量
****也可以直接通過/usr/bin/ansible或者/ansible-2.5.0/bin/ansible來檢查主機可用的變量
該模塊也支持Windows目標
2、參數
fact_path(1.3后增加):用於本地(.fact)的路徑, 如果文件不可執行,則會讀取該目錄中的文件;(如果可執行)並將其結果添加到ansible_local的信息中
filter:過濾串
gather_subset(2.1后增加):將收集的其他信息限制在給定的子集中。可能的值:all,min,hardware,network,virtual,ohai和facter可以指定一個值列表來指定一個更大的子集。值也可以與初始值一起使用,!以指定不應收集該特定子集(如:!hardware, !network, !virtual, !ohai, !facter. if !all)。為避免收集最小子集,請指定!all和!min子集。要僅收集特定信息,請使用!all、!min,並指定特定的信息子集
gather_timeout(2.2后增加):設置單個信息收集的默認超時值,以秒為單位
3、示例
(1)收集fact並進行保存
# 將所有主機的信息輸入到/tmp/facts目錄下
[root@Ansible ~]# ansible web -m setup --tree /tmp/facts
192.168.8.66 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.8.66"
……
]
"module_setup": true
},
"changed": false
}
[root@Ansible ~]# ls /tmp/facts/
192.168.8.66
(2)收集內存信息
[root@Ansible ~]# ansible web -m setup -a "filter=ansible_*_mb"
192.168.8.66 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 119,
"ansible_memory_mb": {
"nocache": {
"free": 484,
"used": 2524
},
"real": {
"free": 119,
"total": 3008,
"used": 2889
},
"swap": {
"cached": 43,
"free": 1980,
"total": 2047,
"used": 67
}
},
"ansible_memtotal_mb": 3008,
"ansible_swapfree_mb": 1980,
"ansible_swaptotal_mb": 2047
},
"changed": false
}
(3)收集主機網卡信息
[root@Ansible ~]# ansible web -m setup -a "filter=ansible_ens33"
192.168.8.66 | SUCCESS => {
"ansible_facts": {
"ansible_ens33": {
"active": true,
"device": "ens33",
……
"ipv4": {
"address": "192.168.8.66",
"broadcast": "192.168.8.255",
"netmask": "255.255.255.0",
"network": "192.168.8.0"
},
"ipv6": [
{
"address": "fe80::118:fa64:e919:e3be",
"prefix": "64",
"scope": "link"
}
],
"type": "ether"
}
},
"changed": false
}
(4)收集fact回報信息
[root@Ansible ~]# ansible web -m setup -a 'gather_subset=!all,!any,facter'
192.168.8.66 | SUCCESS => {
"ansible_facts": {
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-514.el7.x86_64",
"LANG": "zh_CN.UTF-8",
"quiet": true,
"rhgb": true,
"ro": true,
"root": "UUID=edeced73-160f-4b2f-beff-cb5ac7ab9904"
},
"ansible_date_time": {
"date": "2018-04-28",
"day": "28",
……
"module_setup": true
},
"changed": false
}
(5)收集自定義顯示信息
[root@Ansible ~]# ansible web -m setup -a "fact_path='/root/'"
192.168.8.66 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.8.66"
],
……
"module_setup": true
},
"changed": false
}
十二、stat模塊
1、簡介
- stat模塊獲取遠程文件狀態信息,包括atime、ctime、mtime、md5、uid、gid等
2、參數
checksum_algorithm(2.0后增加):確定文件校驗和的算法。如果主機不能使用指定的算法,會拋出一個錯誤(默認為sha1)
follow:是否遵循符號鏈接
get_attributes(2.3后增加):如果存在,使用lsattr工具獲取文件屬性
get_checksum(1.8后增加):是否返回文件的校驗和(默認sha1)
get_md5:是否返回文件的md5總和
get_mime(2.1后增加):使用特殊文件格式並返回有關文件性質的數據
path:文件、對象的完整路徑以獲取信息
3、示例
(1)顯示文件的所有信息
[root@Ansible ~]# ansible web -m stat -a "path=/etc/sysctl.conf"
192.168.8.66 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1524903484.3457322,
……
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "174e13595eb0138cb67ba86af0ee090277d5a86a",
"ctime": 1523506659.1721358,
"dev": 2051,
……
"gid": 0,
"gr_name": "root",
"inode": 16973829,
……
"mimetype": "text/plain",
"mode": "0644",
"mtime": 1523506659.1651359,
"nlink": 1,
"path": "/etc/sysctl.conf",
"pw_name": "root",
……
"size": 473,
"uid": 0,
"version": "18446744073344397267",
……
}
}
(2)顯示MD5值
[root@Ansible ~]# ansible web -m stat -a "path=/etc/sysctl.conf get_md5=yes"
192.168.8.66 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1524903484.3457322,
……
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "174e13595eb0138cb67ba86af0ee090277d5a86a",
"ctime": 1523506659.1721358,
"dev": 2051,
……
"gid": 0,
"gr_name": "root",
"inode": 16973829,
……
"md5": "6683269958dbb7ad3a91435b1f83424e", # md5值
"mimetype": "text/plain",
"mode": "0644",
"mtime": 1523506659.1651359,
"nlink": 1,
"path": "/etc/sysctl.conf",
"pw_name": "root",
……
"size": 473,
"uid": 0,
"version": "18446744073344397267",
……
}
}
十三、cron模塊
1、簡介
- cron模塊用於遠程主機crontab配置,管理計划任務
2、參數
backup:對遠程主機上的原任務計划內容修改之前創建crontab的備份
cron_file:如果指定該選項,則用該文件替換遠程主機上的cron.d目錄下的用戶的任務計划
day:日(1-31,,/2,……)
hour:小時(0-23,,/2,……)
minute:分鍾(0-59,,/2,……)
month:月(1-12,,/2,……)
weekday:周(0-7,,……)
disabled(2.0后增加)
env(2.1后增加)
insertafter(2.1后增加):和state=present和一起使用env。如果指定,則在聲明指定的環境變量之后將插入環境變量
insertbefore(2.1后增加):和state=present和一起使用env。如果指定,則將在聲明指定的環境變量之前插入環境變量
job:要執行的任務,依賴於state=present
name:該任務的描述,如果state=absent,則為必需。請注意,如果名稱未設置且state=present,則將始終創建新的crontab項,而不管現有項是什么
reboot:如果作業應該在重新啟動時運行。此選項已棄用。用戶應該使用special_time
special_time(1.3后增加):指定什么時候執行,參數:reboot、yearly、annually、monthly、weekly、daily、hourly
state:確認該任務計划是創建還是刪除
user:以哪個用戶的身份執行crontab
3、示例
(1)新增一個重啟任務
[root@Ansible ~]# ansible web -m cron -a "name='a job for reboot' special_time=reboot job='/some/job.sh'"
192.168.8.66 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"a job for reboot"
]
}
(2)新增一個定時任務
# 在指定節點上定義一個計划任務,每隔3分鍾到主控端更新一次時間
[root@Ansible ~]# ansible all -m cron -a "name='custom job' minute=*/3 hour=* day=* month=* weekday=* job='/usr/sbin/ntpdate 192.168.8.66'"
192.168.8.66 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"a job for reboot",
"custom job"
]
}
[root@Ansible ~]# ansible all -m command -a "crontab -l"
192.168.8.66 | SUCCESS | rc=0 >>
#Ansible: a job for reboot
@reboot /some/job.sh
#Ansible: custom job
*/3 * * * * /usr/sbin/ntpdate 192.168.8.66
(3)刪除定時任務
[root@Ansible ~]# ansible all -m cron -a "name='custom job' state=absent"
192.168.8.66 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"a job for reboot"
]
}
[root@Ansible ~]# ansible all -m command -a "crontab -l"
192.168.8.66 | SUCCESS | rc=0 >>
#Ansible: a job for reboot
@reboot /some/job.sh
十四、yum模塊
1、簡介
- yum模塊是使用yum軟件包管理器安裝,升級,降級,刪除和列出軟件包和組
2、參數
allow_downgrade(2.4后新增):是否允許指定的軟件包和版本降級,可能已安裝的該軟件包為更高的版本。設置allow_downgrade = True可以使此模塊以非冪等方式運行
conf_file:yum的配置文件
disable_gpg_check:是否禁用正在安裝的軟件包簽名的GPG檢查。只有當狀態存在或最新時才有效
disable_plugin(2.5后新增):要為安裝/更新操作禁用的插件名稱。禁用的插件不會在事務之外持續存在
enable_plugin(2.5后新增):用於安裝/更新操作的插件名稱。啟用的插件不會在事務之外持續存在
disablerepo:重新定位存儲庫以禁用安裝/更新操作,不啟用某個源
enablerepo:重新定位存儲庫以啟用安裝/更新操作,啟用某個源
exclude (2.0后新增):要在狀態=存在或最新時排除的包名稱
installroot(2.3后新增):指定一個替代的installroot,相對於它將安裝所有軟件包
list:包名相對於yum list運行相當於。除了列出的軟件包,使用還可以列出如下:installed,updates,available和repos
name:指定要安裝的包,如果有多個版本需要指定版本,否則安裝最新的包。當使用state=latest時,這可以是'*',這意味着運行yum -y update
security(2.4后新增):如果設置為yes,state=latest則只安裝標記為安全相關的更新
skip_broken(2.3后新增):通過從交易中刪除導致問題的軟件包來解決問題
state:安裝(present或installed),安裝最新版(latest),卸載程序包(absent)
update_cache(1.9后新增) :強制yum檢查緩存是否過期並在需要時重新下載。只有當狀態存在或最新時才有效
update_only(2.5后新增):使用最新的,僅更新安裝的軟件包。不要安裝軟件包。只有在狀態最新時才有效
validate_certs(2.1后新增):這僅適用於使用https url作為rpm的來源。例如用於本地安裝。如果設置為no,SSL證書將不會被驗證。
這應該只設置為no在使用自簽名證書的個人控制站點上使用,因為它可以避免驗證源站點。
2.1之前的代碼工作,如果這是設置yes
3、示例
(1)安裝Apache軟件包
[root@Ansible ~]# ansible all -m yum -a "state=present name=httpd"
192.168.8.66 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-45.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-45.el7.centos CentOS 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-45.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-45.el7.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-45.el7.centos \n\nComplete!\n"
]
}
(2)刪除Apache軟件包
[root@Ansible ~]# ansible all -m yum -a "state=absent name=httpd"
192.168.8.66 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加載插件:fastestmirror\n正在解決依賴關系\n--> 正在檢查事務\n---> 軟件包 httpd.x86_64.0.2.4.6-45.el7.centos 將被 刪除\n--> 解決依賴關系完成\n\n依賴關系解決\n\n================================================================================\n Package 架構 版本 源 大小\n================================================================================\n正在刪除:\n httpd x86_64 2.4.6-45.el7.centos @CentOS 9.4 M\n\n事務概要\n================================================================================\n移除 1 軟件包\n\n安裝大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在刪除 : httpd-2.4.6-45.el7.centos.x86_64 1/1 \n 驗證中 : httpd-2.4.6-45.el7.centos.x86_64 1/1 \n\n刪除:\n httpd.x86_64 0:2.4.6-45.el7.centos \n\n完畢!\n"
]
}
(3)升級所有軟件包
[root@Ansible ~]# ansible all -m yum -a "state=latest name='*'"
192.168.8.66 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"Nothing to do here, all packages are up to date"
]
}
(4)升級所有軟件包,不包括Apache相關軟件包
[root@Ansible ~]# ansible all -m yum -a "state=latest name='*' exclude='httpd'"
192.168.8.66 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"Nothing to do here, all packages are up to date"
]
}
(5)從遠程repo安裝nginx rpm
[root@Ansible ~]# ansible all -m yum -a "state=present name=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm"
(6)從本地文件安裝nginx rpm
[root@Ansible ~]# ansible all -m yum -a "state=present name=/usr/local/nginx-1.14.0-1.el7_4.ngx.x86_64.rpm"
(7)安裝'Development tools'軟件包組
[root@Ansible ~]# ansible all -m yum -a "state=present name='@Development tools'"
十五、user、group模塊
1、簡介
- user模塊是請求的是useradd, userdel, usermod三個指令
- group模塊請求的是groupadd, groupdel, groupmod 三個指令
- user、group模塊管理遠程主機用戶帳戶和用戶屬性
2、參數
(1)user模塊參數
append:如果yes僅添加組,則不會將它們設置為組中的列表
comment:(可選)設置用戶帳戶的描述(又名GECOS)
create_home:除非設置為no,否則在創建帳戶或主目錄不存在時,將為用戶創建主目錄。在版本2.5中更改createhome為create_home
expires(1.9后增加):用戶在epoch中的到期時間,在不支持此操作的平台上會被忽略。目前支持Linux,FreeBSD和DragonFlyBSD
force:與starte一起使用時state=absent,行為如同userdel --force
generate_ssh_key:是否為有問題的用戶生成SSH密鑰。這不會覆蓋現有的SSH密鑰
group:(可選)設置用戶的主要組(采用組名)
groups:指定用戶的屬組。將用戶放在組列表中。當設置為空字符串('groups =')時,用戶將從除主組以外的所有組中移除
home:指定用戶的家目錄,需要與createhome配合使用
login_class:可以選擇設置FreeBSD,DragonFlyBSD,OpenBSD和NetBSD系統的用戶登錄類
move_home:如果設置為yes使用時home=,嘗試將用戶的主目錄移動到指定的目錄(如果它尚未存在)
name:要創建,刪除或修改的用戶的名稱
password:可選擇將用戶的密碼設置為加密值。在Darwin / OS X系統上,這個值必須是明文。謹防安全問題。有關生成這些密碼值的各種方法的詳細信息,請參閱http://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module
remove:當state=absent時,remove=yes則表示連同家目錄一起刪除,等價於userdel -r
seuser(2.1后增加):(可選)在啟用selinux的系統上設置選擇器類型(user_u)
shell:指定用戶的shell環境
skeleton(2.0后增加):可選擇設置家庭骨架目錄。需要create_home選項!
ssh_key_bits:可以指定要創建的SSH密鑰中的位數
ssh_key_comment:(可選)為SSH密鑰定義注釋
ssh_key_file:可以指定SSH密鑰文件名。如果這是一個相對文件名,那么它將相對於用戶的主目錄
ssh_key_passphrase:為SSH密鑰設置密碼。如果未提供密碼,則SSH密鑰將默認為不含密碼
ssh_key_type:可以指定要生成的SSH密鑰的類型。可用的SSH密鑰類型將取決於目標主機上的實現
state:是創建還是刪除
system:創建帳戶時,如果設置為yes,則成為系統帳戶。此設置無法在現有用戶上更改
uid:(可選)設置用戶的UID
update_password(1.3后增加):always將會更新密碼,如果它們不同。on_create將只為新創建的用戶設置密碼
(2)group模塊參數
gid:(可選)設置用戶的GID
name:要管理的組的名稱
state:該組是否應該存在於遠程主機上,創建(present),刪除(absent)
system:如果是,則表示所創建的組是系統組,默認no
3、示例
(1)將用戶xiaozuo添加到特定的uid和admin組中
[root@Ansible ~]# ansible all -m user -a "name=xiaozuo comment=test uid=1040 group=root"
192.168.8.66 | SUCCESS => {
"changed": true,
"comment": "test",
"create_home": true,
"group": 0,
"home": "/home/xiaozuo",
"name": "xiaozuo",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1040
}
[root@Ansible ~]# ansible all -m command -a "id xiaozuo"
192.168.8.66 | SUCCESS | rc=0 >>
uid=1040(xiaozuo) gid=0(root) 組=0(root)
(2)刪除xiaozuo用戶
[root@Ansible ~]# ansible all -m user -a "name=xiaozuo state=absent remove=yes"
192.168.8.66 | SUCCESS => {
"changed": true,
"force": false,
"name": "xiaozuo",
"remove": true,
"state": "absent"
}
[root@Ansible ~]# ansible all -m command -a "id xiaozuo"
192.168.8.66 | FAILED | rc=1 >>
id: xiaozuo: no such usernon-zero return code
(3)創建group組
[root@Ansible ~]# ansible all -m group -a "name=admin"
192.168.8.66 | SUCCESS => {
"changed": true,
"gid": 1002,
"name": "admin",
"state": "present",
"system": false
}
(4)刪除group組
[root@Ansible ~]# ansible all -m group -a "name=admin state=absent"
192.168.8.66 | SUCCESS => {
"changed": true,
"name": "admin",
"state": "absent"
}
(5)為用戶james創建一個2048位SSH密鑰:
[root@Ansible ~]# ansible all -m user -a "name=james generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"
192.168.8.66 | SUCCESS => {
"changed": true,
"comment": "",
"create_home": true,
"group": 1002,
"home": "/home/james",
"name": "james",
"shell": "/bin/bash",
"ssh_fingerprint": "2048 cb:78:0e:12:d7:74:f5:30:fc:23:a4:33:cc:50:1b:57 ansible-generated on Client (RSA)",
"ssh_key_file": "/home/james/.ssh/id_rsa",
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7WBkImiornzorZCzK0cSo2ZWwHHky4hRts8LFuTbgMCH2q7uI6DVyCxBMJSmgFLKLMgiXyTA0TwYckWW08XdfwjZ6X6KAhAGHFpb497eFRkJJiHgP4TM28YpTaoqKLfjHsQmNRqaC84zZCRbxorAZONhhgnz499YectLLS6QtJtCBlOuP3U5ianJrftD5hg/g4R651F23+cSxNHJ8f7A6dpNuc+iFc4wdrz4DiVn4g5WvXyrL6H9MXcUJfhbisgonVif4KD9xKuPNIXLH5yKRggF07Na0RiLVWq9Je4r4dvQFybETdFBtLd4R/UlzOjw6aq9HQO/bPGt3ivHVz2/l ansible-generated on Client",
"state": "present",
"system": false,
"uid": 1002
}
十六、synchronize模塊
1、簡介
- 由於synchronize模塊會調用rsync命令,因此首先要記得提前安裝好rsync軟件包
- synchronize模塊用於將Ansible機器的指定目錄推送(push)到遠程主機的指定目錄下
2、參數
archive: 歸檔,相當於同時開啟recursive(遞歸)、links(鏈接)、perms(權限)、times(時間)、owner(所有者)、group(組標志)和-D選項都為yes,默認該項為開啟
checksum(1.6后增加): 跳過檢測sum值,默認關閉,archive選項默認處於啟用狀態時,checksum選項不會禁用它
compress(1.7后增加): 在傳輸過程中壓縮文件數據。在大多數情況下,請保持啟用狀態
copy_links:復制符號鏈接文件,指向的項目(指示對象)被復制,而不是符號鏈接,默認為no,注意后面還有一個links參數
delete: 刪除不存在的文件,默認no
dest:目標主機上將從源同步的路徑; 路徑可以是絕對的或相對的
dest_port(1.5后增加):默認目錄主機上的ssh端口號,默認是22
dirs:傳速目錄不進行遞歸,默認為no,即進行目錄遞歸
existing_only(1.5后增加):跳過在接收器上創建新文件
group:保留組
link_dest(2.5后增加):在rsync期間添加一個目標到硬鏈接
links:將符號鏈接復制為符號鏈接
owner:保留所有者(僅限超級用戶)
partial(2.0后增加):告訴rsync保留部分文件,該文件應該使文件的其余部分以更快的速度傳輸
perms:保留權限
private_key(1.6后增加):指定用於基於SSH的rsync連接的私鑰(例如~/.ssh/id_rsa)
recursive:遞歸到目錄中
rsync_opts(1.6后增加):通過傳入數組來指定其他rsync選項
rsync_path:指定要在遠程主機上運行的rsync命令
rsync_timeout:在幾秒鍾內為rsync命令指定一個 --timeout
set_remote_user:主要用於/etc/ansible/hosts中定義或默認使用的用戶與rsync使用的用戶不同的情況
src:源主機上將被同步到目標的路徑;路徑可以是絕對的或相對的
times:保留修改時間
use_ssh_args(2.0后增加):使用ansible.cfg中指定的ssh_args
verify_host(2.0后增加):驗證目標主機密鑰
mode: 指定同步的方向。push或pull 模塊,push模的話,一般用於從本機向遠程主機上傳文件,pull 模式用於從遠程主機上取文件
3、示例
(1)使用synchronize模塊時,遠程主機首先要安裝rsync軟件包(我這里已經安裝過了)
[root@Ansible ~]# ansible all -m yum -a "state=present name=rsync"
192.168.8.66 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"rsync-3.0.9-17.el7.x86_64 providing rsync is already installed"
]
}
(2)遠程客戶端安裝好rsync包后就可以在Ansible服務端使用rsync進行同步
[root@Ansible ~]# ansible all -m synchronize -a "src=/test/ dest=/tmp/backup/ compress=yes"
192.168.8.66 | SUCCESS => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /test/ 192.168.8.66:/tmp/backup/",
"msg": "cd+++++++++ ./\n",
"rc": 0,
"stdout_lines": [
"cd+++++++++ ./"
]
}
[root@Ansible ~]# ansible all -m command -a "ls -ld /tmp/backup/"
192.168.8.66 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 6 5月 1 15:38 /tmp/backup/
(3)將Client的/tmp/backup/ 目錄拉取到Ansible的/test 目錄下
[root@Ansible ~]# ansible all -m command -a "ls -l /tmp/backup/"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 0
-rw-r--r-- 1 root root 0 5月 1 15:50 abc.txt
[root@Ansible ~]# ansible 192.168.8.66 -m synchronize -a "mode=pull src=/tmp/backup/ dest=/test/ compress=yes"192.168.8.66 | SUCCESS => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L 192.168.8.66:/tmp/backup/ /test/",
"msg": ".d..t...... ./\n>f+++++++++ abc.txt\n",
"rc": 0,
"stdout_lines": [
".d..t...... ./",
">f+++++++++ abc.txt"
]
}
[root@Ansible ~]# ll /test/
總用量 0
-rw-r--r-- 1 root root 0 5月 1 15:50 abc.txt
十七、mount模塊
1、簡介
- mount模塊控制/etc/fstab中的活動和配置掛載點
2、參數
backup(2.5后增加):創建一個包含時間戳信息的備份文件,這樣如果您以某種方式錯誤地修改了該文件,就可以獲取原始文件
boot(2.2后增加):確定文件系統是否應該在啟動時加載。僅適用於Solaris系統
fstab:如果需要在chroot環境中配置掛載點,這才能使用到
fstype:文件系統類型。當需要的狀態是present或mounted
opts:傳遞給mount命令的參數
path:安裝點的路徑(例如/mnt/files)。在2.3之前,這個選項只能用作dest、destfile和name
name:掛載點。從Ansible 2.3開始,name選項已被更改為path,但name仍然適用
src:要安裝在路徑上的設備。需要時狀態設置為present或mounted
state:必選項。刪除掛載點(absent)、自動創建掛載點並掛載(mounted)、只處理fstab中的配置(present)、卸載(unmounted)
3、示例
(1)把本地的磁盤掛載到遠程主機上
[root@Ansible ~]# ansible all -m mount -a "name=/mnt src=/dev/sda3 fstype=xfs state=mounted opts=rw"
192.168.8.66 | SUCCESS => {
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "xfs",
"name": "/mnt",
"opts": "rw",
"passno": "0",
"src": "/dev/sda3"
}
[root@Ansible ~]# ansible all -a "cat /mnt/etc/fstab"
192.168.8.66 | SUCCESS | rc=0 >>
#
# /etc/fstab
# Created by anaconda on Tue Apr 3 22:19:07 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=edeced73-160f-4b2f-beff-cb5ac7ab9904 / xfs defaults 0 0
UUID=a4f23d30-13d8-423f-b77b-86a78f4dfa2a /boot xfs defaults 0 0
UUID=46bab4fe-7f95-4b24-aff2-bfcf6967ab70 swap swap defaults 0 0
/dev/sda3 /mnt xfs rw 0 0
十八、get_url模塊
1、簡介
- get_url模塊是將文件從HTTP,HTTPS或FTP下載到遠程主機,類似於wget。遠程主機必須能夠直接訪問遠程資源
- HTTP重定向可以從HTTP重定向到HTTPS,因此應該確保這兩種協議的代理環境都是正確的
- 在Ansible 2.4中,當使用-check運行時,它將執行HEAD請求來驗證URL,但不會下載整個文件或對照哈希進行驗證
- 對於Windows目標,請改用win_get_url模塊
2、參數
attributes(2.3后增加):文件或目錄屬性,該字符串應該包含與lsattr顯示的順序相同順序的屬性
backup(2.1后增加):創建一個包含時間戳信息的備份文件,這樣如果您以某種方式錯誤地修改了該文件,就可以獲取原始文件
checksum(2.0后增加):如果將校驗和傳遞給此參數,則目標文件的摘要將在下載后進行計算,以確保其完整性並驗證傳輸是否成功完成。格式:<算法>:<校驗和>,例如checksum="sha256:D98291AC[...]B6DC7B97"
client_cert(2.4后增加):PEM格式的證書鏈文件用於SSL客戶端身份驗證。該文件也可以包含密鑰,如果包含密鑰,client_key則不需要
client_key(2.4后增加):PEM格式的文件,其中包含您的私鑰用於SSL客戶端身份驗證。如果同時client_cert包含證書和密鑰,則不需要此選項
dest:將文件下載到的絕對路徑
force:如果是yes,將每次下載文件,並替換該文件內容;如果是no,該文件只在目標不存在時下載。一般應該yes只適用於小本地文件
force_basic_auth(2.0后增加):httplib2,uri模塊使用的庫僅在web服務響應具有401狀態的初始請求時才發送認證信息。由於某些基本身份驗證服務無法正確發送401,因此登錄將失敗。此選項強制在初始請求時發送基本身份驗證標頭
group:設置文件或目錄的所屬組
owner:設置文件或目錄的所屬用戶
headers(2.0后增加):以“key:value,key:value”格式將自定義HTTP標頭添加到請求中
mode:設置文件或目錄的權限。對於那些習慣於/usr/bin/chmod的記住,模式實際上是八進制數字(如0644or 01777)。離開前導零可能會有意想不到的結果。從版本1.8開始,可以將模式指定為符號模式(例如u+rwx或u=rw,g=r,o=r)
others:file模塊的所有參數也可以在這里使用
sha256sum(1.3后增加):如果將SHA-256校驗和傳遞給此參數,則目標文件的摘要將在下載后進行計算,以確保其完整性並驗證傳輸是否成功完成。此選項已棄用。改為使用checksum
timeout(1.8后增加):URL請求超時(以秒為單位)
tmp_dest(2.1后增加):臨時文件下載到的絕對路徑
url:下載的URL地址
url_password(1.6后增加):用於HTTP基本身份驗證的密碼
url_username(1.6后增加):用於HTTP基本身份驗證的用戶名
use_proxy:如果no,它不使用代理,即使在目標主機上的環境變量中定義了一個代理
validate_certs:如果no,SSL證書不會被驗證。這只能用於使用自簽名證書的個人控制網站
3、示例
(1)下載頁面
[root@Ansible ~]# ansible all -m get_url -a "url=https://www.baidu.com/ dest=/tmp mode=0440"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "77e920ff2d5ce5ac4bb3c399c7f3fa29dd7ced82",
"dest": "/tmp/index.html",
"gid": 0,
"group": "root",
"md5sum": "8f1f3fef541f7dbb36a8755a9f0eff40",
"mode": "0440",
"msg": "OK (227 bytes)",
"owner": "root",
"size": 227,
"src": "/tmp/tmp4yv2WD",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "https://www.baidu.com/"
}
[root@Ansible ~]# ansible all -m command -a "ls -l /tmp/"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 4
drwx------ 2 root root 65 5月 2 09:48 ansible_ngh7w4
-r--r----- 1 root root 227 5月 2 09:48 index.html
(2)下載文件並強制基本認證
[root@Ansible ~]# ansible all -m get_url -a "url=https://www.baidu.com dest=/tmp mode=0440 force_basic_auth=yes"
192.168.8.66 | SUCCESS => {
"changed": false,
"checksum_dest": "77e920ff2d5ce5ac4bb3c399c7f3fa29dd7ced82",
"checksum_src": "77e920ff2d5ce5ac4bb3c399c7f3fa29dd7ced82",
"dest": "/tmp/index.html",
"gid": 0,
"group": "root",
"md5sum": "8f1f3fef541f7dbb36a8755a9f0eff40",
"mode": "0440",
"msg": "OK (227 bytes)",
"owner": "root",
"size": 227,
"src": "/tmp/tmpU6TsG7",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "https://www.baidu.com"
}
(3)從文件路徑下載文件
[root@Ansible ~]# ansible all -m get_url -a "url=http://192.168.8.8/file/file.txt dest=/tmp/test.txt"
192.168.8.66 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/test.txt",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"msg": "OK (0 bytes)",
"owner": "root",
"size": 0,
"src": "/tmp/tmpfdh4qg",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://192.168.8.8/file/file.txt"
}
# 查看下載文件
[root@Ansible ~]# ansible all -m command -a "ls -l /tmp/test.txt"
192.168.8.66 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 5月 2 10:12 /tmp/test.txt
十九、sysctl包管理模塊
1、簡介
- sysctl模塊用於遠程主機sysctl的配置
- sysctl模塊可以在更改配置之后執行/sbin/sysctl –p
2、參數
ignoreerrors:使用此選項可忽略有關未知鍵的錯誤
name:指定sysctl變量的鍵值,使用,分隔多個值
reload:更新sysctl的配置。如果yes,則更新,執行/sbin/sysctl -p sysctl_file。如果no,即使更新了也不重新加載sysctl_file
state:條目在sysctl文件中是否存在
sysctl_file:指定絕對路徑sysctl.conf。如果不是/etc/sysctl.conf,可以使用該參數指定
sysctl_set(1.5后新增):使用sysctl命令和設置,並在必要時使用-w進行設置
value:sysctl密鑰的期望值
3、示例
(1)在/etc/sysctl.conf中將vm.swappiness設置為5
[root@Ansible ~]# ansible all -m sysctl -a "name=vm.swappiness value=5 state=present sysctl_file=/etc/sysctl.conf"
192.168.8.66 | SUCCESS => {
"changed": true
}
# 查看是否替換成功
[root@Ansible ~]# ansible all -m command -a "tail -1 /etc/sysctl.conf"
192.168.8.66 | SUCCESS | rc=0 >>
vm.swappiness=5
(2)從/etc/sysctl.conf中刪除vm.swappiness條目
[root@Ansible ~]# ansible all -m sysctl -a "name=vm.swappiness state=absent sysctl_file=/etc/sysctl.conf"
192.168.8.66 | SUCCESS => {
"changed": true
}
(3)支持ipv4的路由轉發(路徑與Centos版本有關)
# 在文件中設置ip轉發並且不重新加載sysctl文件
[root@Ansible ~]# ansible all -m sysctl -a "name=net.ipv4.ip_forward value=1 sysctl_set=yes sysctl_file=/usr/lib/sysctl.d/50-default.conf"
192.168.8.66 | SUCCESS => {
"changed": true
}
# 在文件中設置ip轉發並在必要時重新加載
[root@Ansible ~]# ansible all -m sysctl -a "name=net.ipv4.ip_forward value=1 sysctl_set=yes state=present reload=yes sysctl_file=/usr/lib/sysctl.d/50-default.conf"
192.168.8.66 | SUCCESS => {
"changed": false
}
二十、unarchive模塊
1、簡介
- unarchive模塊用於解壓文件
- 默認情況下,它將在解包之前將源文件從本地復制到目標主機
- 設置remote_src = yes將解包目標主機上已有的解壓文件
- 對於Windows目標,請改用win_unzip模塊
2、參數
這個模塊有兩種用法:
(1)將Ansible主機上的壓縮包在本地解壓縮后傳到遠程主機上,這種情況下,copy=yes
(2)將遠程主機上的某個壓縮包解壓縮到指定路徑下。這種情況下,需要設置copy=no
具體的參數如下:
attributes(2.3后新增):文件或目錄的屬性
copy:在解壓文件之前,是否先將文件復制到遠程主機,默認為yes。若為no,則要求目標主機上壓縮包必須存在
creates(1.6后新增):指定的絕對路徑(文件或目錄)已經存在時,則解壓指令不執行
decrypt(2.4后新增):控制使用保管庫對源文件進行自動解密
dest:遠程主機上的一個絕對路徑,即文件解壓的路徑
exclude(2.1后新增):列出想要從非歸檔操作中排除的目錄和文件條目
extra_opts(2.1后新增):通過傳入數組來指定其他選項
group:解壓后文件或目錄的所屬組
owner:解壓后文件或目錄的所屬組用戶
keep_newer(2.1后新增):不要替換比歸檔文件更新的現有文件
list_files(2.0后新增):如果設置為yes,則會列出壓縮包里的文件,默認為no
mode:解壓后文件或目錄的權限。對於那些習慣於/usr/bin/chmod的記住,模式實際上是八進制數字(如0644or 01777)。離開前導零可能會有意想不到的結果。從版本1.8開始,可以將模式指定為符號模式(例如u+rwx或u=rw,g=r,o=r)
remote_src(2.2后新增):設置為yes表示歸檔文件已經在遠程系統上,而不是本地的Ansible控制器。這個選項是與之互斥的copy
src:如果copy為yes,則需要指定壓縮文件的源路徑。如果remote_src=no(默認),將歸檔文件的本地路徑復制到目標服務器; 可以是絕對的或相對的。如果remote_src=yes,將目標服務器上的路徑解壓到現有的存檔文件。
如果remote_src=yes並src包含://,遠程機器將首先從URL下載文件。(version_added 2.0)。這僅適用於簡單情況,對於完全下載支持,請使用get_url模塊
validate_certs(2.2后新增):僅適用於使用https URL作為文件的來源
3、示例
(1)將 ansible.tar解壓縮到遠程主機/tmp/ 中
# 創建一個本地tar包
[root@Ansible ~]# cd /etc/
[root@Ansible etc]# tar cf ansible.tar ansible/
# 將ansible.tar解壓到遠程主機上
[root@Ansible etc]# ansible all -m unarchive -a "src=/etc/ansible.tar dest=/tmp/ mode=0755"
192.168.8.66 | SUCCESS => {
"changed": true,
"dest": "/tmp/",
"extract_results": {
"cmd": [
"/usr/bin/gtar",
"--extract",
"-C",
"/tmp/",
"-f",
"/root/.ansible/tmp/ansible-tmp-1525230128.965889-28889483585284/source"
],
"err": "",
"out": "",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "TarArchive",
"mode": "01777",
"owner": "root",
"size": 142,
"src": "/root/.ansible/tmp/ansible-tmp-1525230128.965889-28889483585284/source",
"state": "directory",
"uid": 0
}
# 進行驗證
[root@Ansible etc]# ansible all -m command -a "ls -l /tmp"
192.168.8.66 | SUCCESS | rc=0 >>
總用量 0
drwxr-xr-x 2 root root 38 5月 2 10:12 ansible
drwx------ 2 root root 65 5月 2 11:02 ansible_z_94tQ
(2)將遠程主機test.tar解壓縮到指定目錄/opt/ 中
# 在遠程主機創建一個tar包
[root@Client ~]# mkdir /root/test
[root@Client ~]# tar cf test.tar test/
[root@Client ~]# ll
總用量 12
drwxr-xr-x 2 root root 6 5月 2 11:12 test
-rw-r--r-- 1 root root 10240 5月 2 11:12 test.tar
# 在Ansible服務端進行操作
[root@Ansible ~]# ansible all -m unarchive -a "src=/root/test.tar dest=/opt/ remote_src=yes"
192.168.8.66 | SUCCESS => {
"changed": true,
"dest": "/opt/",
"extract_results": {
"cmd": [
"/usr/bin/gtar",
"--extract",
"-C",
"/opt/",
"-f",
"/root/test.tar"
],
"err": "",
"out": "",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "TarArchive",
"mode": "0755",
"owner": "root",
"size": 18,
"src": "/root/test.tar",
"state": "directory",
"uid": 0
}
以上就是Ansible常用的模塊,如果還需要其它的模塊的話可以查看官方文檔,也可以通過命令來進行查看
1、查看所有的模塊命令: ansible-doc -l
2、查看具體某個模塊用法:ansible-doc -s MODULE_NAME