ansible使用shell模块在受控机上执行命令(ansible2.9.5)
一,ansible的shell模块和command模块的区别?
shell模块:在远程主机上执行主控端发出的shell/python脚本
command模块:不能调用shell指令,没有bash的环境变量,也不能使用shell的一些操作,在遇到"<",">","|","&"将会终止。
它不支持变量、重定向、管道符等,这些操作需要用shell模块执行.
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,shell模块执行命令的例子:
1,进入指定的目录后执行指令:
# chdir : 指定一个目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录下
[liuhongdi@centos8 ~]$ ansible yujian -m shell -a "chdir=/usr/local/soft pwd;"
121.122.123.47 | CHANGED | rc=0 >>
/usr/local/soft
2,检查nginx服务是否在进程中?
用shell可以使用管道符,比如查看远程受控端nginx服务是否启动
说明;用command模块就会报错,因为不支持管道。
这是shell模块和command模块的主要区别
#grep -v grep 不看包含有grep字符串的进程
[liuhongdi@centos8 ~]$ ansible yujian -m shell -a "ps auxfww | grep nginx: | grep -v grep"
121.122.123.47 | CHANGED | rc=0 >>
root 7491 0.0 0.0 50412 3340 ? Ss Mar11 0:00 nginx: master process /usr/soft/openresty/nginx/sbin/nginx
nginx 7492 0.0 0.0 82576 7756 ? S Mar11 0:12 \_ nginx: worker process
nginx 7493 0.0 0.0 81892 5940 ? S Mar11 0:00 \_ nginx: worker process
nginx 7494 0.0 0.0 81892 7184 ? S Mar11 0:00 \_ nginx: worker process
nginx 7495 0.0 0.0 81892 5940 ? S Mar11 0:00 \_ nginx: worker process
nginx 7496 0.0 0.0 81892 5940 ? S Mar11 0:00 \_ nginx: worker process
nginx 7497 0.0 0.0 81892 7056 ? S Mar11 0:00 \_ nginx: worker process
nginx 7498 0.0 0.0 81892 5940 ? S Mar11 0:00 \_ nginx: worker process
nginx 7499 0.0 0.0 81892 5940 ? S Mar11 0:00 \_ nginx: worker process
3,把命令执行结果保存到重定向文件
说明:command模块不支持重定向
[liuhongdi@centos8 ~]$ ansible yujian -m shell -a "chdir=/data/site/think_www git status >> /home/webop/work/gitstatus.txt"
121.122.123.47 | CHANGED | rc=0 >>
登录到受控端,查看生成的结果文件
[root@blog ~]$ cd /home/webop/work
[root@blog work]$ more gitstatus.txt
On branch master
Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
4,常用的一个例子:从受控端使用git发布代码
#warn=no 不理会警告信息
[liuhongdi@centos8 ~]$ ansible yujian -m shell -a "chdir=/data/site/think_www warn=no git pull origin master" --become --become-method=sudo --become-user=root
5,常用的一个例子:查看服务器空间使用情况
[liuhongdi@centos8 work]$ ansible yujian -m shell -a "df -h | grep /dev/vd"
121.122.123.47 | CHANGED | rc=0 >>
/dev/vda1 100G 14G 87G 14% /
/dev/vdb1 500G 3.6G 497G 1% /databak
三,查看ansible的版本
[root@centos8 liuhongdi]# ansible --version
ansible 2.9.5
ansible使用shell模块在受控机上执行命令(ansible2.9.5)的更多相关文章
- ansible使用script模块在受控机上执行脚本(ansible2.9.5)
一,ansible的script模块的用途 script 模块用来在远程主机上执行 ansible 管理主机上的脚本, 即:脚本一直存在于 ansible 管理主机本地, 不需要手动拷贝到远程主机后再 ...
- ansible用get_url模块在受控机下载文件(ansible2.9.5)
一,ansible的get_url模块用途: get_url模块可以在受控机下载文件 可以理解成从受控端执行wget 下载的url支持:http | https | ftp 三种协议 说明:刘宏缔 ...
- ansible使用setup模块查看受控机的信息(ansible2.9.5)
一,ansible的setup模块的用途: 用来获取主机的信息 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest 对应的 ...
- ansible用user/group模块管理受控机上的用户和组(ansible2.9.5)
一,ansible的user/group模块的用途: ansible的user模块用来实现:生成用户.删除用户等用户的管理ansible的group模块用来实现:生成组.删除组等组的管理 说明:刘宏缔 ...
- ansible使用file模块管理受控机的目录与文件(ansible2.9.5)
一,ansible的file模块的用途 file 模块实现对文件的基本操作. 例如: 创建文件或目录 删除文件或目录 修改文件权限等 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...
- Ansible快速实战指南----多机自动化执行命令、部署神器
1.需求: 需要在多台主机上,发送文件.执行命令,进行快速部署 2.ansible 远程复制文件 例子:在当前节点(20.88.14 ...
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- shell学习笔记3-后台执行命令
cron 系统调度进程,使用它在每天的某时间或一周.一月的不同时间运行 at 使用它在一个特点的时间允许一些特殊的作业 & 使用它在后台允许一个占用时间不长的进程 Nohup 使用它在后台运行 ...
- ansible通过yum/dnf模块给受控机安装软件(ansible2.9.5)
一,使用yum/dnf模块要注意的地方: 使用dnf软件安装/卸载时,需要有root权限, 所以要使用become参数 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnb ...
随机推荐
- CTF-BugKu-34-
2020.09.15 做得好慢,,,心不在焉的,赶紧的啊,还得做别的呢 做题 第三十四题 很普通的数独(ISCCCTF) https://ctf.bugku.com/challenges#很普通的数独 ...
- Java中读取配置文件中的内容,并将其赋值给静态变量的方法
应用场景 项目开发中某个功能需要抽取成方法写成一个工具类,提供给别人使用.写过工具类的人都知道,工具类中的方法一般都是静态方法,可以直接使用类名点方法名调用, 使用很方便,比如判断某个对象是否为空的方 ...
- SpringCloud实战 | 第三篇:SpringCloud整合Nacos实现配置中心
前言 随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus ...
- Docker介绍及常用操作演示(一)
Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互 ...
- Module build failed: TypeError: this.getResolve is not a function at Object.loader 使用vue-cli 创建项目 使用sass时报错 -- 等其他sass 报错 ./node_modules/css-loader?{"sourceMap":true}!./node_modules/vue-loader/lib
已经安装了 sass相关依赖包 npm install sass-loader --save-devnpm install node-sass --sava-dev 并且在build文件下webpa ...
- springboot:druid 404
druid配置是在servlet中添加,所以需要servlet扫描 aplication里添加@ServletComponentScan("com")
- 实现队列的基本操作(数据结构)-python版
class Queue: def __init__(self): self.entries = [] self.length = 0 self.front = 0 def put(self, item ...
- Vulkan在Android使用Compute shader
oeip 相关功能只能运行在window平台,想移植到android平台,暂时选择vulkan做为图像处理,主要一是里面有单独的计算管线且支持好,二是熟悉下最新的渲染技术思路. 这个 demo(git ...
- Java List 常用集合 ArrayList、LinkedList、Vector
Java 中的 List 是非常常用的数据类型.List 是有序的 Collection,Java List 一共有三个实现类,分别是:ArrayList.Vector.LinkedList 本文分析 ...
- 自定义带边框TextView--边框粗细不一的问题
自定义带边框TextView 给textview加边框 最low的做法.textview外层套一层布局,然后给布局加边框样式(这么弱的做法,不能这么干) 自定义控件 canvas.drawLines ...