Ansible常用模塊使用詳解
一、前 言
昨天跟大家聊了一下ansible工具的基本使用情況,今天展開跟大家聊一聊它的常用模塊具體用法。
ansible常用模塊有:
1)ping
2)yum
3)template
4)copy
5)user
6)group
7)service
8)raw
9)command
10)shell
11)script
12)file
ansible常用模塊raw、command、shell的區別:
- shell模塊調用的/bin/sh指令執行;
- command模塊不是調用的shell的指令 ,所以沒有bash的環境變量;
- raw很多地方和shell類似,更多的地方建議使用shell和command模塊。
但是如果是使用老版本 python,需要用到raw,又或者是客戶端是路由器,因為沒有安裝python模塊,那就需要使用raw模塊了。
二、ansible常用模塊之ping
ping模塊用于檢查指定節點機器是否連通 ,用法很簡單 ,不涉及參數 ,主機如果在線 ,則回復pong。
具體用法:
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m ping
192.168.160.137 | SUCCESS => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : false ,
"ping " : "pong "}
三、ansible常用模塊之command
command模塊用于在遠程主機上執行命令 ,ansible默認就是使用command模塊。
command模塊有一個缺陷就是不能使用管道符和重定向功能。
具體用法:
//查看受控主機的/tmp目錄內容
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls /tmp '
192.168.160.137 | CHANGED | rc=0 >>
ansible_ansible .legacy .command_payload_5oag98gx vmware- root_912-2697663791
//在受控主機的/tmp目錄下新建一個文件test
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls /tmp '
192.168.160.137 | CHANGED | rc=0 >>
ansible_ansible .legacy .command_payload_5oag98gx vmware- root_912-2697663791
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'touch /tmp/test ' 192.168.160.137 | CHANGED | rc=0 >>
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls /tmp '
192.168.160.137 | CHANGED | rc=0 >>
ansible_ansible .legacy .command_payload_vr17igqu
test
vmware- root_912-2697663791
//command模塊不支持管道符,不支持重定向
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'echo "hello world "> / tmp/test '
192.168.160.137 | CHANGED | rc=0 >>
hello world> /tmp/test
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'cat /tmp/test ' 192.168.160.137 | CHANGED | rc=0 >>
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ps -ef|grep vsftpd ' 192.168.160.137 | FAILED | rc=1 >>
error: unsupported SysV option
Usage:
ps [options]
Try 'ps --help <simple |list|output|threads |misc |all> '
or 'ps --help <s |l|o |t|m |a> '
for additional help text.
For more details see ps (1 ) .non-zero return code
四、ansible常用模塊之raw
raw模塊用于在遠程主機上執行命令 ,其支持管道符與重定向。
具體用法:
//支持重定向
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m raw -a 'echo "hello wo rld "> /tmp/test '
192.168.160.137 | CHANGED | rc=0 >>
Shared connection to 192 .168 .160 .137 closed.
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'cat /tmp/test ' 192.168.160.137 | CHANGED | rc=0 >>
hello world
//支持管道符
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m raw -a 'cat /tmp/test| g rep -Eo hello '
192.168.160.137 | CHANGED | rc=0 >>
hello
Shared connection to 192 .168 .160 .137 closed.
五、ansible常用模塊之shell
shell模塊用于在受控機上執行受控機上的腳本 ,亦可直接在受控機上執行命令。
shell模塊亦支持管道與重定向。
具體用法:
//查看受控機上的腳本
[ root@yxt01 ~]# mkdir /scripts
[ root@yxt01 ~]# vim /scripts/test.sh
#!/bin/bash
for i in $(seq 10) ;do
echo $i
done
[ root@yxt01 ~]# chmod +x /scripts/test.sh
//使用shell模塊在受控機上執行受控機上的腳本
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a /scripts/tes t.sh
192.168.160.137 | CHANGED | rc=0 >>
1
2
3
4
5
6
7
8
9
10
六、ansible常用模塊之script
script模塊用于在受控機上執行主控機上的腳本。
具體用法:
//控制節點編寫腳本
[ root@ansible ansible]# mkdir scripts
[ root@ansible ansible]# vim scripts/a .sh
#!/bin/bash
echo "123456789 " > /tmp/yxt
[ root@ansible ansible]# chmod +x scripts/a .sh
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m script -a /etc/ansibl e/scripts/a .sh
192.168.160.137 | CHANGED => {
"changed " : true ,
" rc " : 0 ,
"stderr " : "Shared connection to 192 .168 .160 .137 closed.\ r\n " , "stderr_lines " : [
"Shared connection to 192 .168 .160 .137 closed. "
] ,
"stdout " : "" ,
"stdout_lines " : []
}
//查看受控機上的/tmp/yxt文件內容
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'cat /tmp/yxt ' 192.168.160.137 | CHANGED | rc=0 >>123456789
七、ansible常用模塊之template
template模塊用于生成一個模板 ,并可將其傳輸至遠程主機上。
具體用法:
//將控制節點的源傳到受控主機
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m template -a 's rc=/etc/ yum . repos .d/base . repo dest=/etc/yum . repos .d/yxt. repo '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"checksum " : "560603bdf5025f4792d05af5c847c331021ce0bd " ,
"dest " : "/etc/yum . repos .d/yxt. repo " ,
"gid " : 0 ,
"group " : " root " ,
"md5sum " : "8c363e0c07338b6ac086feb c52347eec " ,
"mode " : "0644 " ,
"owner " : " root " ,
"size " : 363 ,
"s rc " : "/ root/ .ansible/tmp/ansible-tmp-1666347840 .691359-46642-101169
141864363/source " ,
"state " : "file " ,
"u id " : 0
}
//查看受控機是否傳輸成功
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls /etc/yum . repos .d ' 192.168.160.137 | CHANGED | rc=0 >>yxt. repo
八、ansible常用模塊之yum
yum模塊用于在指定節點機器上通過yum管理軟件 ,其支持的參數主要有兩個:
- name:要管理的包名;
- state:要進行的操作。
state常用的值:
- latest:安裝軟件;
- installed:安裝軟件;
- present:安裝軟件;
- removed:卸載軟件;
- absent:卸載軟件。
若想使用yum來管理軟件 ,請確保受控機上的yum源無異常。
具體用法:
//在ansible主機上使用yum模塊在受控機上安裝httpd
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m yum -a 'name=httpd st a te=present '
192.168.160.137 | SUCCESS => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : false ,
"msg " : "Nothing to do " ,
" rc " : 0 ,
" results " : []
}
//查看受控機上是否安裝了vsftpd
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a ' rpm -qa |g rep httpd '
192.168.160.137 | CHANGED | rc=0 >>
centos-logos-httpd-85 .8-2 .el8 .noarch
httpd-tools-2 .4 .37-43 .module_el8 .5 .0+1022+b541f3b1 .x86_64
httpd-filesystem-2 .4 .37-43 .module_el8 .5 .0+1022+b541f3b1 .noarch httpd-2 .4 .37-43 .module_el8 .5 .0+1022+b541f3b1 .x86_64
//安裝多個軟件包
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m yum -a 'name=httpd ,vi m ,unzip state=present '
九、ansible常用模塊之copy
copy模塊用于復制文件至遠程受控機。
具體用法:
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m copy -a 's rc=/etc/ansi ble/scripts/a .sh dest=/tmp/ '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"checksum " : "ab84c988002f9a200bb48f94998796fc4ec4f08f " ,
"dest " : "/tmp/a .sh " ,
"gid " : 0 ,
"group " : " root " ,
"md5sum " : "598a8c03922c68043f9a641e9be ba08e " ,
"mode " : "0644 " ,
"owner " : " root " ,
"size " : 41 ,
"s rc " : "/ root/ .ansible/tmp/ansible-tmp-1666350011 .7698014-47306-23315
2996046928/source " ,
"state " : "file " ,
"u id " : 0
}
//查看受控機上的/tmp
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls /tmp/ '
192.168.160.137 | CHANGED | rc=0 >>
ansible_ansible .legacy .command_payload_xnu2k8sp a .sh
test
vmware- root_912-2697663791 yxt
十、ansible常用模塊之group
group模塊用于在受控機上添加或刪除組。
- name用于指定group的組名,string類型,必填項。
- state用于指定用戶組在遠程主機上是否被更改或刪除,string類型。
有兩個選項:absent ,present 。默認值為present ,absent為刪除組。 - gid用于設定用戶組gid ,int類型,默認值為空。
- system用于指定創建的用戶組是否為系統組,布爾類型,可用選項false ,true ,默認為false。
具體用法:
//在受控機上添加一個系統組,其gid為306 ,組名為mysql
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m group -a 'name=mysql g id=306 state=present '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"gid " : 306 ,
"name " : "mysql " ,
"state " : "present " ,
"system " : false
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'g rep mysql / etc/group '
192.168.160.137 | CHANGED | rc=0 >>
mysql :x:306:
//刪除受控機上的mysql組
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m group -a 'name=mysql s tate=absent '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"gid " : 306 ,
"name " : "mysql " ,
"state " : "absent " ,
"system " : false
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'g rep mysql / etc/group '
192.168.160.137 | FAILED | rc=1 >>
non-zero return code
十一、 ansible常用模塊之user
user模塊用于管理受控機的用戶帳號。
- name參數
必須參數,用于指定要操作的用戶名稱,可以使用別名 user。 - group參數
此參數用于指定用戶所在的基本組。 - u id參數
此參數用于指定用戶的 u id 號。 - system參數
此參數用于指定是否創建系統賬號 - shell參數
此參數用于指定用戶的默認 shell。 - state參數
此參數用于指定用戶是否存在于遠程主機中,可選值有 present、absent ,默認值 為 present ,表示用戶需要存在,當設置為 absent 時表示刪除用戶。 - remove參數
當state的值設置為 absent時,表示要刪除遠程主機中的用戶。但是在刪除用戶時,不會刪除用戶的家目錄等信息,這是因為 remove參數的默認值為 no ,如果設置為yes ,在刪除用戶的同時,會刪除用戶的家目錄。當state=absent并且 remove=yes時,相當于執行 “userd el -- remove” 命令。 - password參數
此參數用于指定用戶的密碼。但是這個密碼不能是明文的密碼,而是一個對明文密碼,”加密后” 的字符串,相當于 /etc/shadow 文件中的密碼字段,是一個對明文密碼進行哈希后的字 符串,你可以在 python 的命令提示符下輸入如下命令,生成明文密碼對應的加密字符串。
具體用法:
//在受控機上添加一個系統用戶,用戶名為mysql ,u id為306 ,設置其shell為/sbin/nologi n ,無家目錄
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m user -a 'name=mysql ui d=306 system=yes create_home=no shell=/sbin/nologin state=present '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python "
} ,
"changed " :
"comment " :
"create_home " : false ,
"group " : 100 ,
"home " : "/home/mysql " ,
"name " : "mysql " ,
"shell " : "/sbin/nologin " ,
"state " : "present " ,
"system " : true ,
"u id " : 306
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'g rep mysql / etc/passwd '
192.168.160.137 | CHANGED | rc=0 >>
mysql :x:306:100::/home/mysql :/sbin/nologin
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'ls /home '
192.168.160.137 | CHANGED | rc=0 >>
yexiaotian
//修改mysql用戶的u id為366
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m user -a 'name=mysql ui d=366 '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"append " : false ,
"changed " :
"comment " :
"group " : 100 ,
"home " : "/home/mysql " ,
"move_home " : false ,
"name " : "mysql " ,
"shell " : "/sbin/nologin " ,
"state " : "present " ,
"u id " : 366
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'g rep mysql / etc/passwd '
192.168.160.137 | CHANGED | rc=0 >>
mysql :x:366:100::/home/mysql :/sbin/nologin
//刪除受控機上的mysql用戶
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m user -a 'name=mysql st ate=absent '
true
""
true
""
,
,
,
,
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"force " : false ,
"name " : "mysql " ,
" remove " : false ,
"state " : "absent "
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'g rep mysql / etc/passwd '
192.168.160.137 | FAILED | rc=1 >>
non-zero return code
十二、ansible常用模塊之service
service模塊用于管理受控機上的服務。
- name參數
此參數用于指定需要操作的服務名稱,比如 httpd。 - state參數
此參數用于指定服務的狀態,比如,我們想要啟動遠程主機中的 nginx,則可以將 state的值設置為started;如果想要停止遠程主機中的服務,則可以將state的值設置為 stoppe d 。此參數的可用值有 started、stopped、restarted、reloaded。 - enabled參數
此參數用于指定是否將服務設置為開機啟動項,設置為 yes 表示將對應服務設置為開機啟動,設置為no表示不會開機啟動。
具體用法:
//查看受控機上的httpd服務是否啟動
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'systemctl is -active httpd '
192.168.160.137 | FAILED | rc=3 >>
inactivenon-zero return code
//啟動受控機上的httpd服務
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m service -a 'name=httpd state=started '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"name " : "httpd " ,
"state " : "started " ,
"status " : {
"ActiveEnterTimestamp " : " Fri 2022-10-21 19:59:40 CST " ,
"ActiveEnterTimestampMonotonic " : "37169140809 " ,
. . . . .省略
//查看受控機上的httpd服務是否啟動
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'systemctl is -active httpd '
192.168.160.137 | CHANGED | rc=0 >>
active
//查看受控機上的httpd服務是否開機自動啟動
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m service -a 'name=httpd enabled=yes '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"enabled " : true ,
"name " : "httpd " ,
"status " : {
"ActiveEnterTimestamp " : " Fri 2022-10-21 20:01:35 CST " , "ActiveEnterTimestampMonotonic " : "37284140964 " ,
"ActiveExitTimestamp " : " Fri 2022-10-21 19:59:49 CS
. . . . .省略
//查看受控機上的httpd服務是否開機自動啟動
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'systemctl is -enabled httpd '
192.168.160.137 | CHANGED | rc=0 >>
enabled
//停止受控機上的httpd服務
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m service -a 'name=httpd state=stopped '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"name " : "httpd " ,
"state " : "stopped " ,
"status " : {
"ActiveEnterTimestamp " : " Fri 2022-10-21 20:01:35 CST " ,
"ActiveEnterTimestampMonotonic " : "37284140964 " ,
. . . . .省略
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'systemctl is -active httpd '
192.168.160.137 | FAILED | rc=3 >>
inactivenon-zero return code
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ss -anlt ' 192.168.160.137 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0 .0 .0 .0:22 0 .0 .0 .0:*LISTEN 0 128 [ ::] :22 [ ::] :*
十三、ansible常用模塊之file
file 模塊可以幫助我們完成一些對文件的基本操作。
- state參數
state=directory 在遠程主機上創建一個名為 data 的目錄,如果存在則不會做操作;
state=touch 在遠程主機上創建一個名為 testfile1的文件,如果 testfile1文件已經存在并且文件內有內容,則只會更新文件的時間戳,與 touch命令的作用相同;
state=link 在遠程主機上為 testfile1 文件創建軟鏈接文件;
state=hard 在遠程主機上上為 testfile1 文件創建硬鏈接文件;
state=absent 刪除文件,刪除時不用區分目標是文件、 目錄、還是鏈接;
state=s rc 在state設置為link或者hard時,表示我們想要創建一個軟鏈或者硬鏈,所以,我們必須指明軟鏈或硬鏈鏈接的哪個文件,通過src參數即可指定鏈接源。 - path參數
指定文件,如果遠程主機上沒有該文件,則進行創建。 - mod參數
權限可以在添加時設置特殊權限,前提要有執行權限( set 粘滯位)。 - owner和group參數
屬主和屬組。
具體用法:
//在遠程主機上創建一個名為 data 的目錄
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/da ta state=directory '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"gid " : 0 ,
"group " : " root " ,
"mode " : "0755 " ,
"owner " : " root " ,
"path " : "/ root/data " ,
"size " : 6 ,
"state " : "directory " ,
"u id " : 0
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root ' 192.168.160.137 | CHANGED | rc=0 >>
total 0
drwxr-xr-x 2 root root 6 Oct 21 20:17 data
//在遠程主機上創建一個名為abc 的文件
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/ab c state=touch '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"dest " : "/ root/abc " ,
"gid " : 0 ,
"group " : " root " ,
"mode " : "0644 " ,
"owner " : " root " ,
"size " : 0 ,
"state " : "file " ,
"u id " : 0
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root ' 192.168.160.137 | CHANGED | rc=0 >>
total 0
- rw- r-- r-- 1 root root 0 Oct 21 20:21 abc
drwxr-xr-x 2 root root 6 Oct 21 20:17 data
//在遠程主機上為abc文件創建軟鏈接文件,軟鏈接名為 1 .link
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/1 . link state=link s rc=/ root/abc '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"dest " : "/ root/1 .link " ,
"gid " : 0 ,
"group " : " root " ,
"mode " : "0777 " ,
"owner " : " root " ,
"size " : 9 ,
"s rc " : "/ root/abc " ,
"state " : "link " ,
"u id " : 0
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root '
192.168.160.137 |
total 0
l rwx rwx rwx 1 root - rw- r-- r-- 1 root drwxr-xr-x 2 root
CHANGED | rc=0 >>
root 9 Oct 21 20:23 root 0 Oct 21 20:21 root 6 Oct 21 20:17
1 .link -> / root/abc
abc
data
//在遠程主機上上為 abc文件創建硬鏈接文件,硬鏈接名為 1 .hard
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/1 . hard state=hard s rc=/ root/abc '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"dest " : "/ root/1 .hard " ,
"gid " : 0 ,
"group " : " root " ,
"mode " : "0644 " ,
"owner " : " root " ,
"size " : 0 ,
"s rc " : "/ root/abc " ,
"state " : "hard " ,
"u id " : 0
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root '
192.168.160.137 |
total 0
- rw- r-- r--
2
1
2
2
root root root root
l rwx rwx rwx
- rw- r-- r--
drwxr-xr-x
CHANGED | rc=0 >>
root 0 Oct 21 20:21 root 9 Oct 21 20:23 root 0 Oct 21 20:21 root 6 Oct 21 20:17
1 .hard
1 .link -> / root/abc
abcdata
注意:在創建鏈接文件時,如果源文件不存在,或者鏈接文件與其他文件同名時,強制覆蓋同名文件或者創建鏈接文件。
//刪除遠程機器上的指定文件或目錄
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/da ta state=absent '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"path " : "/ root/data " ,
"state " : "absent "
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root '
192.168.160.137 | total 0
- rw- r-- r-- 2 root l rwx rwx rwx 1 root - rw- r-- r-- 2 root
CHANGED | rc=0 >>
root 0 Oct 21 20:21 root 9 Oct 21 20:23 root 0 Oct 21 20:21
1 .hard
1 .link -> / root/abc
abc
// 在創建文件或目錄的時候指定屬主,或者修改遠程主機上的文件或目錄的屬主
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/ab c state=touch owner=yexiaotian group=apache '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"dest " : "/ root/abc " ,
"gid " : 48 ,
"group " : "apache " ,
"mode " : "0644 " ,
"owner " : "yexiaotian " ,
"size " : 0 ,
"state " : "hard " ,
"u id " : 4000
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root '
192.168.160.137 | CHANGED | rc=0 >>
total 0
- rw- r-- r-- 2 yexiaotian apache 0 Oct 21 20:30 1 .hard
l rwx rwx rwx 1 root root 9 Oct 21 20:23 1 .link -> / root/abc
- rw- r-- r-- 2 yexiaotian apache 0 Oct 21 20:30 abc
//在創建文件或目錄的時候指定權限,或者修改遠程主機上的文件或目錄的權限
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m file -a 'path=/ root/ab c state=touch mode=755 '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
"dest " : "/ root/abc " ,
"gid " : 48 ,
"group " : "apache " ,
"mode " : "0755 " ,
"owner " : "yexiaotian " ,
"size " : 0 ,
"state " : "hard " ,
"u id " : 4000
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'ls -l /root ' 192.168.160.137 | CHANGED | rc=0 >>
total 0
- rwxr-xr-x 2 yexiaotian apache 0 Oct 21 20:31 1 .hard
l rwx rwx rwx 1 root root 9 Oct 21 20:23 1 .link -> / root/abc
- rwxr-xr-x 2 yexiaotian apache 0 Oct 21 20:31 abc
十四、擴展模塊之 yum_repository模塊**
yum_repository 模塊可以幫助我們管理遠程主機上的yum倉庫。
- name參數
必須參數,用于指定要操作的唯一的倉庫ID ,也就是” . repo”配置文件中 每個倉庫對應的” 中括號” 內的倉庫ID。 - baseurl參數
此參數用于設置 yum倉庫的 baseurl。 - description參數
此參數用于設置倉庫的注釋信息,也就是” . repo”配置文件中每個倉庫對應的” name字段”對應的內容。 - file參數
此參數用于設置倉庫的配置文件名稱,即設置” . repo”配置文件的文件名前綴,在不使用此參數的情況下,默認以 name參數的倉庫ID作為” . repo”配置文件的文件名前綴,同一個” . repo” 配置文件中可以存在多個 yum 源。 - enabled參數
此參數用于設置是否激活對應的 yum源,此參數默認值為 yes,表示啟用對應的yum源,設置為no表示不啟用對應的yum源。 - gpgcheck參數
此參數用于設置是否開啟 rpm 包驗證功能,默認值為 no ,表示不啟用包驗證,設置為 yes 表示開啟包驗證功能。 - gpgkey參數
當 gpgcheck 參數設置為yes時,需要使用此參數指定驗證包所需的公鑰。 - state參數
默認值為 present ,當值設置為 absent時,表示刪除對應的yum源。
具體用法:
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m yum_ repository -a 'fil e=yxt. repo name= "BaseOS " description=BaseOS baseurl= "http://mirrors .aliyu n .com/centos-vault/8 .5 .2111/BaseOS/$basearch/os/ " gpgcheck=no enabled=ye s '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
" repo " : "BaseOS " ,
"state " : "present "
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m yum_ repository -a 'fil e=yxt. repo name= "AppStream " description=AppStream baseurl= "http://mirror s .aliyun .com/centos-vault/8 .5 .2111/AppStream/$basearch/os/ " gpgcheck=no e nabled=yes '
192.168.160.137 | CHANGED => {
"ansible_facts " : {
"discovered_interpreter_python " : "/us r/libexec/platform-python " } ,
"changed " : true ,
" repo " : "AppStream " ,
"state " : "present "
}
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'cat /etc/yum . repos .d/ yxt. repo . repo '
192.168.160.137 | CHANGED | rc=0 >>
[BaseOS]
async = 1
baseurl = http://mirrors .aliyun .com/centos-vault/8 .5 .2111/BaseOS/$basearc
h/os/
enabled = 1
gpgcheck = 0
name = BaseOS
[AppStream]
async = 1
baseurl = http://mirrors .aliyun .com/centos-vault/8 .5 .2111/AppStream/$base arch/os/
enabled = 1
gpgcheck = 0
name = AppStream