自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Ansible常用模塊使用詳解

開發(fā) 前端
ansible常用模塊raw、command、shell的區(qū)別:shell模塊調(diào)用的/bin/sh指令執(zhí)行;command模塊不是調(diào)用的shell的指令 ,所以沒有bash的環(huán)境變量;raw很多地方和shell類似,更多的地方建議使用shell和command模塊。但是如果是使用老版本 python,需要用到raw,又或者是客戶端是路由器,因?yàn)闆]有安裝python模塊,那就需要使用raw模塊了。

一、前 言

昨天跟大家聊了一下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的區(qū)別:

  • shell模塊調(diào)用的/bin/sh指令執(zhí)行;
  • command模塊不是調(diào)用的shell的指令 ,所以沒有bash的環(huán)境變量;
  • raw很多地方和shell類似,更多的地方建議使用shell和command模塊。
    但是如果是使用老版本 python,需要用到raw,又或者是客戶端是路由器,因?yàn)闆]有安裝python模塊,那就需要使用raw模塊了。

二、ansible常用模塊之ping

ping模塊用于檢查指定節(jié)點(diǎn)機(jī)器是否連通 ,用法很簡單 ,不涉及參數(shù) ,主機(jī)如果在線 ,則回復(fù)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模塊用于在遠(yuǎn)程主機(jī)上執(zhí)行命令 ,ansible默認(rèn)就是使用command模塊。

command模塊有一個缺陷就是不能使用管道符和重定向功能。

具體用法:

//查看受控主機(jī)的/tmp目錄內(nèi)容
[ 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

//在受控主機(jī)的/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模塊用于在遠(yuǎn)程主機(jī)上執(zhí)行命令 ,其支持管道符與重定向。

具體用法:

//支持重定向
[ 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模塊用于在受控機(jī)上執(zhí)行受控機(jī)上的腳本 ,亦可直接在受控機(jī)上執(zhí)行命令。

shell模塊亦支持管道與重定向。

具體用法:

//查看受控機(jī)上的腳本
[ 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模塊在受控機(jī)上執(zhí)行受控機(jī)上的腳本
[ 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模塊用于在受控機(jī)上執(zhí)行主控機(jī)上的腳本。

具體用法:

//控制節(jié)點(diǎn)編寫腳本
[ 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 " : []
}
//查看受控機(jī)上的/tmp/yxt文件內(nèi)容
[ root@ansible ansible]# ansible 192 .168 .160 .137 -a 'cat /tmp/yxt ' 192.168.160.137 | CHANGED | rc=0 >>123456789

七、ansible常用模塊之template

template模塊用于生成一個模板 ,并可將其傳輸至遠(yuǎn)程主機(jī)上。

具體用法:

//將控制節(jié)點(diǎn)的源傳到受控主機(jī)


[ 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
}
//查看受控機(jī)是否傳輸成功
[ 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模塊用于在指定節(jié)點(diǎn)機(jī)器上通過yum管理軟件 ,其支持的參數(shù)主要有兩個:

  • name:要管理的包名;
  • state:要進(jìn)行的操作。

state常用的值:

  • latest:安裝軟件;
  • installed:安裝軟件;
  • present:安裝軟件;
  • removed:卸載軟件;
  • absent:卸載軟件。

若想使用yum來管理軟件 ,請確保受控機(jī)上的yum源無異常。

具體用法:

//在ansible主機(jī)上使用yum模塊在受控機(jī)上安裝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 " : []
}

//查看受控機(jī)上是否安裝了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模塊用于復(fù)制文件至遠(yuǎn)程受控機(jī)。

具體用法:

[ 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
}


//查看受控機(jī)上的/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模塊用于在受控機(jī)上添加或刪除組。

  • name用于指定group的組名,string類型,必填項(xiàng)。
  • state用于指定用戶組在遠(yuǎn)程主機(jī)上是否被更改或刪除,string類型。
    有兩個選項(xiàng):absent ,present 。默認(rèn)值為present ,absent為刪除組。
  • gid用于設(shè)定用戶組gid ,int類型,默認(rèn)值為空。
  • system用于指定創(chuàng)建的用戶組是否為系統(tǒng)組,布爾類型,可用選項(xiàng)false ,true ,默認(rèn)為false。

具體用法:

//在受控機(jī)上添加一個系統(tǒng)組,其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:

//刪除受控機(jī)上的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模塊用于管理受控機(jī)的用戶帳號。

  • name參數(shù)
    必須參數(shù),用于指定要操作的用戶名稱,可以使用別名 user。
  • group參數(shù)
    此參數(shù)用于指定用戶所在的基本組。
  • u id參數(shù)
    此參數(shù)用于指定用戶的 u id  號。
  • system參數(shù)
    此參數(shù)用于指定是否創(chuàng)建系統(tǒng)賬號
  • shell參數(shù)
    此參數(shù)用于指定用戶的默認(rèn) shell。
  • state參數(shù)
    此參數(shù)用于指定用戶是否存在于遠(yuǎn)程主機(jī)中,可選值有 present、absent ,默認(rèn)值 為 present ,表示用戶需要存在,當(dāng)設(shè)置為 absent  時(shí)表示刪除用戶。
  • remove參數(shù)
    當(dāng)state的值設(shè)置為 absent時(shí),表示要刪除遠(yuǎn)程主機(jī)中的用戶。但是在刪除用戶時(shí),不會刪除用戶的家目錄等信息,這是因?yàn)?remove參數(shù)的默認(rèn)值為 no ,如果設(shè)置為yes ,在刪除用戶的同時(shí),會刪除用戶的家目錄。當(dāng)state=absent并且 remove=yes時(shí),相當(dāng)于執(zhí)行 “userd el  -- remove”  命令。
  • password參數(shù)
    此參數(shù)用于指定用戶的密碼。但是這個密碼不能是明文的密碼,而是一個對明文密碼,”加密后” 的字符串,相當(dāng)于 /etc/shadow  文件中的密碼字段,是一個對明文密碼進(jìn)行哈希后的字 符串,你可以在 python  的命令提示符下輸入如下命令,生成明文密碼對應(yīng)的加密字符串。

具體用法:

//在受控機(jī)上添加一個系統(tǒng)用戶,用戶名為mysql ,u id為306 ,設(shè)置其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

//刪除受控機(jī)上的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模塊用于管理受控機(jī)上的服務(wù)。

  • name參數(shù)
    此參數(shù)用于指定需要操作的服務(wù)名稱,比如 httpd。
  • state參數(shù)
    此參數(shù)用于指定服務(wù)的狀態(tài),比如,我們想要啟動遠(yuǎn)程主機(jī)中的 nginx,則可以將 state的值設(shè)置為started;如果想要停止遠(yuǎn)程主機(jī)中的服務(wù),則可以將state的值設(shè)置為 stoppe d 。此參數(shù)的可用值有 started、stopped、restarted、reloaded。
  • enabled參數(shù)
    此參數(shù)用于指定是否將服務(wù)設(shè)置為開機(jī)啟動項(xiàng),設(shè)置為 yes  表示將對應(yīng)服務(wù)設(shè)置為開機(jī)啟動,設(shè)置為no表示不會開機(jī)啟動。

具體用法:

//查看受控機(jī)上的httpd服務(wù)是否啟動
[ 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

//啟動受控機(jī)上的httpd服務(wù)
[ 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 " ,
. . . . .省略

//查看受控機(jī)上的httpd服務(wù)是否啟動
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'systemctl is -active httpd '
192.168.160.137   | CHANGED | rc=0  >>
active

//查看受控機(jī)上的httpd服務(wù)是否開機(jī)自動啟動
[ 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
. . . . .省略

//查看受控機(jī)上的httpd服務(wù)是否開機(jī)自動啟動
[ root@ansible ansible]# ansible 192 .168 .160 .137 -m shell -a 'systemctl is -enabled httpd '
192.168.160.137 | CHANGED | rc=0 >>
enabled

//停止受控機(jī)上的httpd服務(wù)
[ 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參數(shù)
    state=directory  在遠(yuǎn)程主機(jī)上創(chuàng)建一個名為 data  的目錄,如果存在則不會做操作;
    state=touch  在遠(yuǎn)程主機(jī)上創(chuàng)建一個名為 testfile1的文件,如果 testfile1文件已經(jīng)存在并且文件內(nèi)有內(nèi)容,則只會更新文件的時(shí)間戳,與 touch命令的作用相同;
    state=link  在遠(yuǎn)程主機(jī)上為 testfile1  文件創(chuàng)建軟鏈接文件;
    state=hard  在遠(yuǎn)程主機(jī)上上為 testfile1  文件創(chuàng)建硬鏈接文件;
    state=absent  刪除文件,刪除時(shí)不用區(qū)分目標(biāo)是文件、 目錄、還是鏈接;
    state=s rc  在state設(shè)置為link或者h(yuǎn)ard時(shí),表示我們想要創(chuàng)建一個軟鏈或者硬鏈,所以,我們必須指明軟鏈或硬鏈鏈接的哪個文件,通過src參數(shù)即可指定鏈接源。
  • path參數(shù)
    指定文件,如果遠(yuǎn)程主機(jī)上沒有該文件,則進(jìn)行創(chuàng)建。
  • mod參數(shù)
    權(quán)限可以在添加時(shí)設(shè)置特殊權(quán)限,前提要有執(zhí)行權(quán)限(  set 粘滯位)。
  • owner和group參數(shù)
    屬主和屬組。

具體用法:

//在遠(yuǎn)程主機(jī)上創(chuàng)建一個名為 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

//在遠(yuǎn)程主機(jī)上創(chuàng)建一個名為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

//在遠(yuǎn)程主機(jī)上為abc文件創(chuàng)建軟鏈接文件,軟鏈接名為 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


//在遠(yuǎn)程主機(jī)上上為 abc文件創(chuàng)建硬鏈接文件,硬鏈接名為 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

注意:在創(chuàng)建鏈接文件時(shí),如果源文件不存在,或者鏈接文件與其他文件同名時(shí),強(qiáng)制覆蓋同名文件或者創(chuàng)建鏈接文件。

//刪除遠(yuǎn)程機(jī)器上的指定文件或目錄
[ 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


// 在創(chuàng)建文件或目錄的時(shí)候指定屬主,或者修改遠(yuǎn)程主機(jī)上的文件或目錄的屬主
[ 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

//在創(chuàng)建文件或目錄的時(shí)候指定權(quán)限,或者修改遠(yuǎn)程主機(jī)上的文件或目錄的權(quán)限
[ 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

十四、擴(kuò)展模塊之 yum_repository模塊**

yum_repository 模塊可以幫助我們管理遠(yuǎn)程主機(jī)上的yum倉庫。

  • name參數(shù)
    必須參數(shù),用于指定要操作的唯一的倉庫ID ,也就是” . repo”配置文件中 每個倉庫對應(yīng)的” 中括號” 內(nèi)的倉庫ID。
  • baseurl參數(shù)
    此參數(shù)用于設(shè)置 yum倉庫的 baseurl。
  • description參數(shù)
    此參數(shù)用于設(shè)置倉庫的注釋信息,也就是” . repo”配置文件中每個倉庫對應(yīng)的” name字段”對應(yīng)的內(nèi)容。
  • file參數(shù)
    此參數(shù)用于設(shè)置倉庫的配置文件名稱,即設(shè)置” . repo”配置文件的文件名前綴,在不使用此參數(shù)的情況下,默認(rèn)以 name參數(shù)的倉庫ID作為” . repo”配置文件的文件名前綴,同一個” . repo” 配置文件中可以存在多個 yum  源。
  • enabled參數(shù)
    此參數(shù)用于設(shè)置是否激活對應(yīng)的 yum源,此參數(shù)默認(rèn)值為 yes,表示啟用對應(yīng)的yum源,設(shè)置為no表示不啟用對應(yīng)的yum源。
  • gpgcheck參數(shù)
    此參數(shù)用于設(shè)置是否開啟 rpm  包驗(yàn)證功能,默認(rèn)值為 no ,表示不啟用包驗(yàn)證,設(shè)置為 yes  表示開啟包驗(yàn)證功能。
  • gpgkey參數(shù)
    當(dāng) gpgcheck  參數(shù)設(shè)置為yes時(shí),需要使用此參數(shù)指定驗(yàn)證包所需的公鑰。
  • state參數(shù)
    默認(rèn)值為 present ,當(dāng)值設(shè)置為 absent時(shí),表示刪除對應(yīng)的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


責(zé)任編輯:武曉燕 來源: IT那活兒
相關(guān)推薦

2022-11-22 13:00:38

2025-01-22 10:04:52

自動化運(yùn)維Ansiblefetch

2022-06-15 10:12:04

Ansible命令Linux

2025-01-08 07:38:39

setup模塊Ansible網(wǎng)絡(luò)

2010-03-22 15:38:46

Python常用模塊

2020-09-04 06:32:20

Pythonshutil函數(shù)

2011-05-23 16:50:12

python

2020-10-29 10:17:24

AnsibleKubernetes容器編排自動化

2010-12-12 21:01:00

Android控件

2018-07-12 15:03:41

2017-06-09 14:22:31

AnsibleTiDB分布式數(shù)據(jù)庫

2021-05-17 12:54:04

AnsiblePodman開源

2020-10-10 20:30:05

Ansible自動化工具系統(tǒng)運(yùn)維

2018-06-12 14:50:35

系統(tǒng)運(yùn)維Ansible運(yùn)行分析工具

2020-01-02 10:44:22

運(yùn)維架構(gòu)技術(shù)

2025-01-16 08:13:43

commandshell模塊Ansible

2011-07-01 15:53:19

Python 模塊

2021-06-10 06:57:39

Nacos配置模塊

2020-04-27 10:00:53

負(fù)載均衡互聯(lián)網(wǎng)架構(gòu)

2010-05-27 11:12:10

SVN目錄結(jié)構(gòu)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號