一、前 言
昨天跟大家聊了一下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