zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    制造业接入物联网的4大优势
    一篇文章读懂什么是串口通信及其工作原理
    4G工业路由器在仓储物流中的应用
    4G工业路由器应用于远程医疗设备监控
    串口服务器在饮料自动化生产线的应用
    PLC和串口服务器在数字化工厂中的作用
    LoRa集中器在石油勘探等领域的应用解决方案
    4G模块为基础的物联网在精准农业中的应用
    4G工业路由器在垃圾和废水处理的应用案例
    HDU 6205 2017沈阳网络赛 思维题
  • 原文地址:https://www.cnblogs.com/architectforest/p/12765725.html
Copyright © 2011-2022 走看看