1. command模块

功能:在远程主机执行 shell 命令;为默认模块,可省略 -m 选项;

注意:不支持管道命令 |

command模块的常用参数如下:

参数 说明
chdir DIR 执行ansible时,切换到指定的目录
creates FILE 如果文件FILE存在,则不执行命令
removes FILE 如果文件FILE存在,则执行命令
  • 示例一:command模块基础用法,在远端主机执行命令:

    [root@xuzhichao ~]# ansible 192.168.20.22 -a "id"
    192.168.20.22 | CHANGED | rc=0 >>
    uid=0(root) gid=0(root) groups=0(root)
  • 示例二:在被控端主机切换到指定目录,执行命令:

    [root@xuzhichao ~]# ansible 192.168.20.22 -a "chdir=/tmp pwd"
    192.168.20.22 | CHANGED | rc=0 >>
    /tmp
  • 示例三:creates:指定的文件存在时,不执行对应命令:

    [root@xuzhichao ~]# ansible 192.168.20.22 -a "creates=/etc/passwd ls"
    192.168.20.22 | SUCCESS | rc=0 >>
    skipped, since /etc/passwd exists <==跳过,未执行;
  • 示例四:removes:与creates相反,指定的文件存在,则执行对应命令:

    [root@xuzhichao ~]# ansible 192.168.20.22 -a "removes=/etc/passwd ls"
    192.168.20.22 | CHANGED | rc=0 >>
    49857.html
    anaconda-ks.cfg
    apache-tomcat-10.0.7.tar.gz
    echo-nginx-module
    favicon.ico
    index.html
    lvs_rs.sh
    nginx-1.20.1
    nginx-1.20.1.tar.gz
    redis-4.2.0.tgz
    route.txt
    valgrind-3.15.0-11.el7.x86_64.rpm
    wordpress-3.1-zh_CN.zip

2. shell模块

功能:在远程主机执行 Shell 命令,支持管道等特殊符号的操作,比command模块使用广泛;

shell模块的常用参数如下:

参数 说明
chdir DIR 执行ansible时,切换到指定的目录
creates FILE 如果文件FILE存在,则不执行命令
removes FILE 如果文件FILE存在,则执行命令
  • shell模块支持管道命令,在被控主机上创建用户和密码:

    [root@xuzhichao ~]# ansible 192.168.20.22 -m shell -a 'useradd xu1'
    192.168.20.22 | CHANGED | rc=0 >> [root@xuzhichao ~]# ansible 192.168.20.22 -m shell -a 'echo 123456 | passwd --stdin xu1'
    192.168.20.22 | CHANGED | rc=0 >>
    Changing password for user xu1.
    passwd: all authentication tokens updated successfully.

注意:调用bash执行命令 类似cat /tmp/stanley.md | awk -F'|' '{print $1,$2}' &> /tmp/example.txt这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本中,copy到远程,执行,再把需要的结果拉回执行命令的机器。

注意:虽然可以使用shell模块完成绝大多数操作,但是shell模块无法很好的保证ansible的幂等性,因此建议使用以下各个专用模块完成特定的功能,可以保证幂等性。

ansible(4)--ansible的command和shell模块的更多相关文章

  1. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  2. ansible执行shell模块和command模块报错| FAILED | rc=127 >> /bin/sh: lsof: command not found和| rc=2 >> [Errno 2] No such file or directory

    命令: ansible -i hosts_20 st  -m shell -a 'service zabbix_agentd star'  -K --become ansible -i hosts_2 ...

  3. ansible模块之command、shell、script、file、copy、fetch

    前戏 ansible 批量在远程主机上执行命令 openpyxl 操作excel表格 puppet ansible slatstack ansible epel源 第一步: 下载epel源 wget ...

  4. ansible模块command、shell、raw、script

    简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...

  5. ansible使用shell模块在受控机上执行命令(ansible2.9.5)

    一,ansible的shell模块和command模块的区别? shell模块:在远程主机上执行主控端发出的shell/python脚本 command模块:不能调用shell指令,没有bash的环境 ...

  6. ansible command和shell的区别

    1.command模块不支持管道符和变量等,如果要使用这些,需要shell模块. 2.在使用ansible中的时候,默认的模块是-m command,从而模块的参数不需要填写,直接使用即可

  7. ansible shell模块

    [root@ftp:/root] > ansible ansible01 -u root -k -m shell -a 'hostname' SSH password: ansible01 | ...

  8. ansible笔记(6):常用模块之命令类模块

    ansible笔记():常用模块之命令类模块 command模块 command模块可以帮助我们在远程主机上执行命令 注意:使用command模块在远程主机中执行命令时,不会经过远程主机的shell处 ...

  9. ansible笔记(5):常用模块之命令类模块

    1.command模块 它的作用是帮助我们在远程主机上执行命令. [注意]使用command模块在远程主机中执行命令时,不会经过远程主机的shell处理,在使用command模块时,如果需要执行的命令 ...

  10. Ansible自动化搭建及工具集和常见模块、命令详情(重点)

    一.ansible介绍 1.ansible简介 官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具. Ansible跟其他IT自动化技术的区别 ...

随机推荐

  1. archlinux 安装后xfce没有声音,声音无法调节

    参照 http://ivo-wang.github.io/2018/02/17/fix/ sudo pacman -S alsa-utils pavucontrol sudo pacman -S pi ...

  2. wordpress自建博客站,在页脚添加网站总运行时间

    wordpress自建博客站,在页脚添加网站总运行时间 笔者使用的主题是 GeneratePress 版本:3.1.3 <span id="momk" style=" ...

  3. echarts 中国地图tooltip实现提示框

    point: 鼠标位置,如 [20, 40]. params: 同 formatter 的参数相同. dom: tooltip 的 dom 对象. rect: 只有鼠标在图形上时有效,是一个用x, y ...

  4. #深搜,期望#CF105B Dark Assembly

    洛谷题目传送门 CODEFORCES传送门 分析 题目强调贿赂要在投票开始前完成说明分糖和成功率可以分开计算 那么分糖考虑直接暴搜,由于题目并没有说糖必须全部分完, 所以每一次分完一颗糖后均要求当前状 ...

  5. 网络组件axios可以在OpenHarmony上使用了

    什么是axios 上古浏览器页面在向服务器请求数据时,因为返回的是整个页面的数据,页面都会强制刷新一下,这对于用户来讲并不是很友好.并且我们只是需要修改页面的部分数据,但是从服务器端发送的却是整个页面 ...

  6. MongoDB命令行交互

    命令行交互 命令行交互一般是学习数据库的第一步,不过这些命令在后续用的比较少,了解即可. 角色命令 创建角色 use admin db.createUser({"user": &q ...

  7. HDC 2022精彩继续,多重亮点进来看!

    原文:https://mp.weixin.qq.com/s/YX5vD4cxM8dA4v2ukFooyA,点击链接查看更多技术内容.  

  8. Launching Teamviewer remotely through SSH

    Launching Teamviewer remotely through SSH When you need to manage your Server remotely, but you can ...

  9. SpringCloud做的微服务项目--外卖订餐系统

    本项目用到的组件技术可以参考我上一篇博客,来学习. 项目需求: 客户端:针对普通用户,用户登录,用户退出,菜品订购,我的订单 后台管理系统:针对管理员,管理员登录,管理员退出,添加菜品,查询菜品,修改 ...

  10. wordcloud 词云Python

    from wordcloud import WordCloud import matplotlib.pyplot as plt def get_word_cloud(words_list): #首先实 ...