0
点赞
收藏
分享

微信扫一扫

Ansible的shell模块

写心之所想 2023-03-24 阅读 93

shell模块功能:在远程节点上执行命令(复杂的命令)

也就是等于你在Linux上直接执行的所有任何复杂的命令都可以

Shell练习

#远程过滤ssh进程信息
nfs -m shell -a "ps -ef |grep ssh"
172.16.1.31 | CHANGED | rc=0 >>
root       1474      1  0 20:41 ?        00:00:00 /usr/sbin/sshd -D
root       2200   1474  0 20:48 ?        00:00:00 sshd: root@pts/0
root       5031   1474  1 22:02 ?        00:00:00 sshd: root@pts/1
root       5186   5181  0 22:02 pts/1    00:00:00 /bin/sh -c ps -ef |grep ssh
root       5188   5186  0 22:02 pts/1    00:00:00 grep ssh

#使用重定向符号,创建文件
#远程获取时间信息,且写入到文件中
ansible nfs -m shell -a "date > /tmp/date.log"
ansible nfs -m shell -a "date '+%F%T' > /tmp/date.log"

远程执行复杂Linux命令

这个命令无法在command中执行

通过一条命令,执行如下操作:

•创建文件夹

•生成sh脚本文件(查看主机名)

•赋予脚本执行权限

•执行脚本

•忽略warning信息

ansible nfs -m shell -a "mkdir /0224/;\
echo 'hostname' > /0224/hostname.sh;chmod +x /0224/hostname.sh;/0224/hostname.sh; warn=false"


举报

相关推荐

0 条评论