ansible 常用模块详解
1 主机连通性测试
我们使用ansible web -m ping命令来进行主机连通性测试,效果如下:
[root@jenkins ~]# ansible test -m ping
192.168.64.129 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@jenkins ~]#
这样就说明我们的主机是连通状态的。接下来的操作才可以正常进行。
2 command 模块
这个模块可以直接在远程主机上执行命令,并将结果返回本主机。
举例如下:
[root@jenkins ~]# ansible test -m command -a 'ss -ntl'
192.168.64.129 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 1024 127.0.0.1:9093 0.0.0.0:*
LISTEN 0 1024 127.0.0.1:9100 0.0.0.0:*
LISTEN 0 1024 127.0.0.1:9229 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 1024 127.0.0.1:8080 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9168 0.0.0.0:*
LISTEN 0 128 127.0.0.1:8082 0.0.0.0:*
命令模块接受命令名称,后面是空格分隔的列表参数。指定的命令将在所有选定的节点上执行。它不会通过shell进行处理,比如$HOME和操作如"<",">","|",";","&" 工作(需要使用(shell)模块实现这些功能)。
注意,该命令不支持| 管道命令。
下面来看一看该模块下常用的几个命令:
chdir # 在执行命令之前,先切换到该目录
executable # 切换shell来执行命令,需要使用命令的绝对路径
free_form # 要执行的Linux指令,一般使用Ansible的-a参数代替。
creates # 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
removes # 一个文件名,这个文件不存在,则该命令不执行
下面我们来看看这些命令的执行效果:
#先切换到/mnt/ 目录,再执行“ls”命令
[root@jenkins ~]# ansible test -m command -a 'chdir=/mnt/ ls'
192.168.64.129 | CHANGED | rc=0 >>
hgfs
#如果/mnt/hgts存在,则不执行“ls” 命令
[root@jenkins ~]# ansible test -m command -a 'creates=/mnt/hgfs ls'
192.168.64.129 | SUCCESS | rc=0 >>
skipped, since /mnt/hgfs exists
#如果/mnt/hgfs存在,则执行“cat ls /mnt”命令
[root@jenkins ~]# ansible test -m command -a 'removes=/mnt/hgfs ls /mnt/'
192.168.64.129 | CHANGED | rc=0 >>
Hgfs
3 shell 模块
shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。
[root@jenkins ~]# ansible test -m shell -a 'cat /etc/passwd |grep "sunpengfei"'
192.168.64.129 | CHANGED | rc=0 >>
sunpengfei:x:1000:1000:sunpengfei:/home/sunpengfei:/bin/bash
只要是我们的shell命令,都可以通过这个模块在远程主机上运行,这里就不一一举例了。
4 copy 模块
这个模块用于将文件复制到远程主机,同时支持指定内容生成文件和修改权限等。
其相关选项如下:
src #被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"
content #用于替换"src",可以直接指定文件的值
dest #必选项,将源文件复制到的远程主机的绝对路径
backup #当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
directory_mode #递归设定目录的权限,默认为系统默认权限
force #当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"
others #所有的 file 模块中的选项可以在这里使用
4.1用法举例如下:
4.1.1复制文件:
[root@jenkins ~]# ansible test -m copy -a 'src=~/abc.txt dest=/data/abc.txt'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "03cfd743661f07975fa2f1220c5194cbaff48451",
"dest": "/data/abc.txt",
"gid": 0,
"group": "root",
"md5sum": "0bee89b07a248e27c83fc3d5951213c1",
"mode": "0644",
"owner": "root",
"size": 4,
"src": "/root/.ansible/tmp/ansible-tmp-1620374566.5316975-34707-14765376079630/source",
"state": "file",
"uid": 0
}
4.1.2指定内容生成文件,并制定权限
[root@jenkins ~]# ansible test -m copy -a 'content="I am keer\n" dest=/data/name mode=666'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "0421570938940ea784f9d8598dab87f07685b968",
"dest": "/data/name",
"gid": 0,
"group": "root",
"md5sum": "497fa8386590a5fc89090725b07f175c",
"mode": "0666",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1620374768.1690423-34768-206471036865339/source",
"state": "file",
"uid": 0
}
我们现在可以去查看一下我们生成的文件及其权限:
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data/'
192.168.64.129 | CHANGED | rc=0 >>
总用量 8
-rw-r--r-- 1 root root 4 5月 7 16:02 abc.txt
-rw-rw-rw- 1 root root 10 5月 7 16:06 name
可以看出我们的name文件已经生成,并且权限为666。
4.1.3关于覆盖
我们把文件的内容修改一下,然后选择覆盖备份:
[root@jenkins ~]# ansible test -m copy -a 'content="I am keerya\n" backup=yes dest=/data/name mode=666'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup_file": "/data/name.61216.2021-05-07@16:13:25~",
"changed": true,
"checksum": "064a68908ab9971ee85dbc08ea038387598e3778",
"dest": "/data/name",
"gid": 0,
"group": "root",
"md5sum": "8ca7c11385856155af52e560f608891c",
"mode": "0666",
"owner": "root",
"size": 12,
"src": "/root/.ansible/tmp/ansible-tmp-1620375204.8166783-34875-156887504370617/source",
"state": "file",
"uid": 0
}
现在我们可以去查看一下:
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data/'
192.168.64.129 | CHANGED | rc=0 >>
总用量 12
-rw-r--r-- 1 root root 4 5月 7 16:02 abc.txt
-rw-rw-rw- 1 root root 12 5月 7 16:13 name
-rw-rw-rw- 1 root root 10 5月 7 16:06 name.61216.2021-05-07@16:13:25~
5 file 模块
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
下面是一些常见的命令:
force #需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group #定义文件/目录的属组。后面可以加上mode:定义文件/目录的权限
owner #定义文件/目录的属主。后面必须跟上path:定义文件/目录的路径
recurse #递归设置文件的属性,只对目录有效,后面跟上src:被链接的源文件路径,只应用于state=link的情况
dest #被链接到的路径,只应用于state=link的情况
state #状态,有以下选项:
directory:如果目录不存在,就创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
5.2用法举例如下:
5.2.1创建目录:
[root@jenkins ~]# ansible test -m file -a 'path=/data/app state=directory'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/data/app",
"size": 6,
"state": "directory",
"uid": 0
}
查看一下创建的文件
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data'
192.168.64.129 | CHANGED | rc=0 >>
总用量 12
-rw-r--r-- 1 root root 4 5月 7 16:02 abc.txt
drwxr-xr-x 2 root root 6 5月 7 16:17 app
-rw-rw-rw- 1 root root 12 5月 7 16:13 name
-rw-rw-rw- 1 root root 10 5月 7 16:06 name.61216.2021-05-07@16:13:25~
5.2.2创建链接文件
[root@jenkins ~]# ansible test -m file -a 'path=/data/aaa.txt src=abc.txt state=link'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/data/aaa.txt",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 7,
"src": "abc.txt",
"state": "link",
"uid": 0
}
验证一下
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data'
192.168.64.129 | CHANGED | rc=0 >>
总用量 12
lrwxrwxrwx 1 root root 7 5月 7 16:21 aaa.txt -> abc.txt
-rw-r--r-- 1 root root 4 5月 7 16:02 abc.txt
drwxr-xr-x 2 root root 6 5月 7 16:17 app
-rw-rw-rw- 1 root root 12 5月 7 16:13 name
-rw-rw-rw- 1 root root 10 5月 7 16:06 name.61216.2021-05-07@16:13:25~
5.2.3删除文件
[root@jenkins ~]# ansible test -m file -a 'path=/data/aaa.txt state=absent'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/data/aaa.txt",
"state": "absent"
}
查看一下发现aaa.txt链接文件已被删除
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data'
192.168.64.129 | CHANGED | rc=0 >>
总用量 12
-rw-r--r-- 1 root root 4 5月 7 16:02 abc.txt
drwxr-xr-x 2 root root 6 5月 7 16:17 app
-rw-rw-rw- 1 root root 12 5月 7 16:13 name
-rw-rw-rw- 1 root root 10 5月 7 16:06 name.61216.2021-05-07@16:13:25~
6 fetch 模块
该模块用于从远程某主机获取(复制)文件到本地。
有两个选项:
dest:用来存放文件的目录
src:在远程拉取的文件,并且必须是一个file,不能是目录
具体举例如下:
[root@jenkins ~]# ansible test -m fetch -a 'src=/data/name.61216.2021-05-07@16:13:25~ dest=~/'
192.168.64.129 | CHANGED => {
"changed": true,
"checksum": "0421570938940ea784f9d8598dab87f07685b968",
"dest": "/root/192.168.64.129/data/name.61216.2021-05-07@16:13:25~",
"md5sum": "497fa8386590a5fc89090725b07f175c",
"remote_checksum": "0421570938940ea784f9d8598dab87f07685b968",
"remote_md5sum": null
}
我们可以在本机上查看一下文件是否复制成功。要注意,文件保存的路径是我们设置的接收目录下的被管制主机ip目录下:
[root@jenkins ~]# ll ~/192.168.64.129/data/
总用量 4
-rw-r--r-- 1 root root 10 5月 7 16:31 name.61216.2021-05-07@16:13:25~
7 cron 模块
该模块适用于管理cron计划任务的。
其使用的语法跟我们的crontab文件中的语法一致,同时,可以指定以下选项:
day= #日应该运行的工作( 1-31, *, */2, )
hour= # 小时 ( 0-23, *, */2, )
minute= #分钟( 0-59, *, */2, )
month= # 月( 1-12, *, /2, )
weekday= # 周 ( 0-6 for Sunday-Saturday,, )
job= #指明运行的命令是什么
name= #定时任务描述
reboot # 任务在重启时运行,不建议使用,建议使用special_time
special_time #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务
user # 以哪个用户的身份执行
7.1举例如下:
7.1.1添加计划任务
[root@jenkins ~]# ansible test -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": [
"ntp update every 5 min"
]
}
我们可以去查看一下:
[root@jenkins ~]# ansible test -m shell -a 'crontab -l'
192.168.64.129 | CHANGED | rc=0 >>
#Ansible: ntp update every 5 min
*/5 * * * * /sbin/ntpdate 172.17.0.1 &> /dev/null
7.1.2 删除计划任务
[root@jenkins ~]# ansible test -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null >> /tmp/disk_total &> /dev/null" state=absent'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": []
}
删除完成后,我们再查看一下现有的计划任务确认一下:
[root@jenkins ~]# ansible test -m shell -a 'crontab -l'
192.168.64.129 | CHANGED | rc=0 >>
我们的删除操作已经成功。
8 yum 模块
顾名思义,该模块主要用于软件的安装。
其选项如下:
name= #所安装的包的名称
state= #present--->安装, latest--->安装最新的, absent---> 卸载软件。
update_cache #强制更新yum的缓存
conf_file #指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
disable_pgp_check #是否禁止GPG checking,只用于presentor latest。
disablerepo #临时禁止使用yum库。 只用于安装或更新时。
enablerepo #临时使用的yum库。只用于安装或更新时。
下面我们就来安装一个包试试看:
[root@jenkins ~]# ansible app -m yum -a 'name=httpd state=present'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64",
"Installed: httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fdbbcc.noarch",
"Installed: centos-logos-httpd-80.5-2.el8.noarch",
"Installed: httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
]
}
卸载软件:
[root@jenkins ~]# ansible app -m yum -a 'name=httpd state=absent'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
"Removed: httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64"
]
}
9 service 模块
该模块用于服务程序的管理。
其主要选项如下:
arguments #命令行提供额外的参数
enabled #设置开机启动。
name= #服务名称
runlevel #开机启动的级别,一般不用指定。
sleep #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
state #有四种状态,分别为:started--->启动服务, stopped--->停止服务, restarted--->重启服务, reloaded--->重载配置
例子:
9.1开启服务并设置自启动
[root@jenkins ~]# ansible app -m service -a 'name=nginx state=started enabled=true'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "nginx",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "network.target basic.target nss-lookup.target systemd-journald.socket sysinit.target tmp.mount remote-fs.target -.mount systemd-tmpfiles-setup.service system.slice",
………
我们可以去查看一下80端口是否打开:
[root@jenkins ~]# ansible app -m shell -a 'ss -ntl'
192.168.64.128 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
9.2关闭服务
[root@jenkins ~]# ansible app -m service -a 'name=nginx state=stopped'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "nginx",
"state": "stopped",
……
我们在查看一下80端口:
[root@jenkins ~]# ansible app -m shell -a 'ss -ntl'
192.168.64.128 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
10 user 模块
该模块主要是用来管理用户账号。
其主要选项如下:
comment # 用户的描述信息
createhome # 是否创建家目录
force # 在使用state=absent时, 行为与userdel –force一致.
group # 指定基本组
groups # 指定附加组,如果指定为(groups=)表示删除所有组
home # 指定用户家目录
move_home # 如果设置为home=时, 试图将用户主目录移动到指定的目录
name # 指定用户名
non_unique # 该选项允许改变非唯一的用户ID值
password # 指定用户密码
remove # 在使用state=absent时, 行为是与userdel –remove一致
shell # 指定默认shell
state # 设置帐号状态,不指定为创建,指定值为absent表示删除
system # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid # 指定用户的uid
举例如下:
10.1添加一个用户并指定其 uid
[root@jenkins ~]# ansible all -m user -a 'name=spf uid=11111'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 11111,
"home": "/home/spf",
"name": "spf",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 11111
}
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 11111,
"home": "/home/spf",
"name": "spf",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 11111
}
验证用户是否存在:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/passwd |grep spf'
192.168.64.128 | CHANGED | rc=0 >>
spf:x:11111:11111::/home/spf:/bin/bash
192.168.64.129 | CHANGED | rc=0 >>
spf:x:11111:11111::/home/spf:/bin/bash
10.2删除用户
[root@jenkins ~]# ansible all -m user -a 'name=spf state=absent'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "spf",
"remove": false,
"state": "absent"
}
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "spf",
"remove": false,
"state": "absent"
}
再次验证:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/passwd |grep spf'
192.168.64.128 | FAILED | rc=1 >>
non-zero return code
192.168.64.129 | FAILED | rc=1 >>
non-zero return code
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/passwd |grep root'
192.168.64.128 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
192.168.64.129 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
11 group 模块
该模块主要用于添加或删除组。
常用的选项如下:
gid= #设置组的GID号
name= #指定组的名称
state= #指定组的状态,默认为创建,设置值为absent为删除
system= #设置值为yes,表示创建为系统组
举例如下:
11.1创建组
[root@jenkins ~]# ansible all -m group -a 'name=spfgroup gid=12222'
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 12222,
"name": "spfgroup",
"state": "present",
"system": false
}
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 12222,
"name": "spfgroup",
"state": "present",
"system": false
}
创建过后,我们来查看一下:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/group | grep 12222'
192.168.64.128 | CHANGED | rc=0 >>
spfgroup:x:12222:
192.168.64.129 | CHANGED | rc=0 >>
spfgroup:x:12222:
11.2删除组
[root@jenkins ~]# ansible all -m group -a 'name=spfgroup state=absent'
192.168.64.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "spfgroup",
"state": "absent"
}
192.168.64.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "spfgroup",
"state": "absent"
}
再次验证:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/group | grep 12222'
192.168.64.129 | FAILED | rc=1 >>
non-zero return code
192.168.64.128 | FAILED | rc=1 >>
non-zero return code
12 script 模块
该模块用于将本机的脚本在被管理端的机器上运行。
该模块直接指定脚本的路径即可,我们通过例子来看一看到底如何使用的:
首先,我们写一个脚本,并给其加上执行权限:
[root@jenkins ~]# vim /tmp/df.sh
[root@jenkins ~]# cat /tmp/df.sh
#!/bin/bash
date >> /tmp/disk_total.log
df -lh >> /tmp/disk_total.log
[root@jenkins ~]# chmod +x /tmp/df.sh
然后,我们直接运行命令来实现在被管理端执行该脚本:
[root@jenkins ~]# ansible all -m script -a '/tmp/df.sh'
192.168.64.129 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.64.129 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.64.129 closed."
],
"stdout": "",
"stdout_lines": []
}
192.168.64.128 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.64.128 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.64.128 closed."
],
"stdout": "",
"stdout_lines": []
}
照例查看一下文件内容:
[root@jenkins ~]# ansible all -m shell -a 'cat /tmp/disk_total.log'
192.168.64.128 | CHANGED | rc=0 >>
2021年 05月 07日 星期五 17:35:43 CST
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 866M 0 866M 0% /dev
tmpfs 896M 0 896M 0% /dev/shm
tmpfs 896M 9.7M 886M 2% /run
tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/mapper/cl_leanote-root 64G 5.0G 60G 8% /
/dev/mapper/cl_leanote-home 32G 269M 31G 1% /home
/dev/sda1 1014M 240M 775M 24% /boot
tmpfs 180M 1.2M 178M 1% /run/user/42
tmpfs 180M 28K 179M 1% /run/user/1000
tmpfs 180M 0 180M 0% /run/user/0
192.168.64.129 | CHANGED | rc=0 >>
2021年 05月 07日 星期五 17:35:43 CST
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs 1.9G 12K 1.9G 1% /dev/shm
tmpfs 1.9G 13M 1.8G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/cl_leanote-root 64G 8.5G 56G 14% /
/dev/mapper/cl_leanote-home 32G 269M 31G 1% /home
/dev/sda1 1014M 240M 775M 24% /boot
tmpfs 371M