本文主要介绍Ansible的几个命令模块,包括:

  • command - 在远程节点上执行命令
  • shell - 让远程主机在shell进程下执行命令
  • script - 将本地script传送到远程主机之后执行
  • raw - 执行低级的和脏的SSH命令
  • expect - 执行命令并响应提示
  • telnet - 执行低级的和脏的telnet命令

command模块

简介

  • command模块用于在给的的节点上运行系统命令,比如echo hello。
  • 它不会通过shell处理命令,因此不支持像$HOME这样的变量和,以及<, >, |, ;&等都是无效的。也就是在command模块中无法使用管道符

模块参数

名称 必选 备注
chdir no 运行command命令前先cd到这个目录
creates no 如果这个参数对应的文件存在,就不运行command
free_form yes 需要执行的脚本(没有真正的参数为free_form)
executable no 改变用来执行命令的shell,应该是可执行文件的绝对路径。
removes no 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反
stdin(2.4后新增) no 将命令的stdin设置为指定的值

示例

  • 列出指定目录下的文件
[root@centos7 ~]# ansible test -m command -a "ls /root"
172.20.21.120 | SUCCESS | rc=0 >>
anaconda-ks.cfg
test.sh
whoami.rst [root@centos7 ~]# ansible test -m command -a "ls /root creates=test.sh"
172.20.21.120 | SUCCESS | rc=0 >>
skipped, since test.sh exists [root@centos7 ~]# ansible test -m command -a "ls /root removes=test.sh1"
172.20.21.120 | SUCCESS | rc=0 >>
skipped, since test.sh1 does not exist

在这个里面,首先更换目录到root目录中,然后查看test.sh是否存在,如果存在,那么命令不会执行;如果不存在,那么执行命令。

在这里也可以看到,命令是必须存在的,但是没有参数名为free_form参数

  • 切换目录执行命令
[root@centos7 ~]# ansible test -m command -a "cat test.sh chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>
#!/bin/bash
i=0
echo $((i+1)) [root@centos7 ~]# ansible test -m command -a "sh test.sh chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>
1
  • 无法使用管道符
[root@centos7 ~]# ansible test -m command -a "ls /root | grep test"
172.20.21.120 | FAILED | rc=2 >>
/root:
anaconda-ks.cfg
test.sh
whoami.rstls: 无法访问|: 没有那个文件或目录
ls: 无法访问grep: 没有那个文件或目录
ls: 无法访问test: 没有那个文件或目录non-zero return code

注意事项

  • 若要通过shell运行一个命令,比如<, >, |等,你实际上需要shell模块。
  • command模块更安全,因为它不受用户环境的影响
  • 从版本2.4开始,executable参数被删除。如果您需要此参数,请改用shell模块。
  • 对于Windows节点,请改用win_command模块。

shell模块

简介

让远程主机在shell进程下执行命令,从而支持shell的特性,如管道等。与command模块几乎相同,但在执行命令的时候使用的是/bin/sh

模块参数

名称 必选 备注
chdir no 运行command命令前先cd到这个目录
creates no 如果这个参数对应的文件存在,就不运行command
executable no 改变用来执行命令的shell,应该是可执行文件的绝对路径。
free_form yes 需要执行的脚本(没有真正的参数为free_form)
removes no 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反
stdin(2.4后新增) no 将命令的stdin设置为指定的值

示例

  • 切换目录,执行命令并保持输出
[root@centos7 ~]# ansible test -m shell -a "sh test.sh > result chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >> [root@centos7 ~]# ansible test -m shell -a "cat result chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>
1

注意事项

  • 如果你想安全可靠的执行命令,请使用command模块,这也是编写playbook的最佳实践。

script模块

简介

  • script模块的作用是将本地script传送到远程主机之后执行
  • 给定的脚本将通过远程节点上的shell环境进行处理
  • script模块在远程系统上不需要python的支持

模块参数

名称 必选 默认值 可选值 备注
chdir(2.4后新增) no     运行command命令前先cd到这个目录
creates no     如果这个参数对应的文件存在,就不运行command
decrypt no yes yes/no 此选项控制使用保管库的源文件的自动解密
free_form yes     需要执行脚本的本地文件路径(没有真正的参数为free_form)
removes no     如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反

示例

  • 在远程主机上执行脚本
[root@centos7 ~]# ansible test -m script -a "test.sh chdir=/tmp"
172.20.21.120 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.20.21.120 closed.\r\n",
"stdout": "/tmp\r\n",
"stdout_lines": [
"/tmp"
]
}

注意事项

  • 通常来说,使用Ansible模块比推送脚本更好
  • 当脚本执行时,ssh连接插件将通过-tt强制伪tty分配。伪ttys没有stderr通道,所有stderr被发送到标准输出。如果需要标准输出和标准错误分离,请使用到copy模块。

raw模块

简介

  • raw模块主要用于执行一些低级的,脏的SSH命令,而不是通过command模块。 raw模块只适用于下列两种场景,第一种情况是在较老的(Python 2.4和之前的版本)主机上,另一种情况是对任何没有安装Python的设备(如路由器)。 在任何其他情况下,使用shellcommand模块更为合适。
  • 就像script模块一样,raw模块不需要远程系统上的python

模块参数

名称 必选 备注
executable no 改变用来执行命令的shell,应该是可执行文件的绝对路径。
free_form yes 需要执行的脚本(没有真正的参数为free_form)

示例

  • 在远程主机上执行脚本
[root@centos7 ~]# ansible test -m raw -a "pwd"
172.20.21.120 | SUCCESS | rc=0 >>
/root
Shared connection to 172.20.21.120 closed.

注意事项

  • 如果要安全可靠地执行命令,最好使用shellcommand模块来代替。
  • 如果从playbook中使用raw,则可能需要使用gather_facts: no禁用事实收集

expect模块

简介

  • expect模块用于在给的的节点上执行一个命令并响应提示。
  • 它不会通过shell处理命令,因此不支持像$HOME这样的变量和,以及<, >, |, ;&等都是无效的。也就是在command模块中无法使用管道符

使用要求(在执行模块的主机上)

  • python >= 2.6
  • pexpect >= 3.3

模块参数

名称 必选 默认值 备注
chdir no   运行command命令前先cd到这个目录
command yes   命令模块执行命令运行
echo no   是否回显你的回应字符串
responses yes   期望的字符串/正则表达式和字符串的映射来响应。 如果响应是一个列表,则连续的匹配将返回连续的响应。 列表功能是2.1中的新功能。
creates no   如果这个参数对应的文件存在,就不运行command
removes no   如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反
timeout no 30 以秒为单位等待预期时间

示例

  • 在远程主机上执行脚本
- name: Case insensitve password string match
expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word" - name: Generic question with multiple different responses
expect:
command: /path/to/custom/command
responses:
Question:
- response1
- response2
- response3

注意事项

  • 如果你想通过shell运行一个命令(比如你正在使用<,>,|等),你必须在命令中指定一个shell,比如/bin/bash -c "/path/to/something | grep else"
  • responses下关键是一个python正则表达式匹配,不区分大小写的搜索用前缀?i
  • 默认情况下,如果多次遇到问题,则会重复其字符串响应。 如果连续问题匹配需要不同的响应,而不是字符串响应,请使用字符串列表作为响应。
  • expect模块设计用于简单场景,对于更复杂的需求,应该考虑在shellscript模块中使用expect代码

telnet模块

简介

  • expect模块用于执行一些低级的和脏telnet命令,不通过模块子系统。
  • 它不会通过shell处理命令,因此不支持像$HOME这样的变量和,以及<, >, |, ;&等都是无效的。也就是在command模块中无法使用管道符

模块参数

名称 必选 默认值 备注
command yes   在telnet会话中执行的命令
host no remote_addr 要执行命令的主机/目标
password yes   登录密码
pause no 1 每发出一个命令之间的暂停秒
port no 23 远程端口
prompts no [u'$'] 发送下一个命令之前预期的提示列表
timeout no 30 远程操作超时时间
user no remote_user 登录用户

示例

  • 在远程主机上执行脚本
- name: send configuration commands to IOS
telnet:
user: cisco
password: cisco
login_prompt: "Username: "
prompts:
- "[>|#]"
command:
- terminal length 0
- configure terminal
- hostname ios01 - name: run show commands
telnet:
user: cisco
password: cisco
login_prompt: "Username: "
prompts:
- "[>|#]"
command:
- terminal length 0
- show version

注意事项

  • 如果你想通过shell运行一个命令(比如你正在使用<,>,|等),你必须在命令中指定一个shell,比如/bin/bash -c "/path/to/something | grep else"
  • responses下关键是一个python正则表达式匹配,不区分大小写的搜索用前缀?i
  • 默认情况下,如果多次遇到问题,则会重复其字符串响应。 如果连续问题匹配需要不同的响应,而不是字符串响应,请使用字符串列表作为响应。
  • expect模块设计用于简单场景,对于更复杂的需求,应该考虑在shellscript模块中使用expect代码

作者:hoxis
链接:https://www.jianshu.com/p/8661c107448d
來源:简书

Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]的更多相关文章

  1. ansible的安装及命令相关模块

    ansible 第一步:下载epel源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos- ...

  2. ansible命令执行模块使用

    ansible命令执行模块使用 1.命令执行模块-command 在远程节点上运行命令. 命令模块使用命令名称,接上空格-的分割符作为参数使用,但是不支持管道符和变量等,如果要使用这些,那么可以使用s ...

  3. ansible命令及模块

    ping 命令 #测试单个主机 [root@node1 opt]# ansible -m ping 10.0.0.22 #获取多个主机 [root@node1 opt]# ansible 10.0.0 ...

  4. ansible软件相关模块丶计划任务,剧本

    软件相关模块 yum rpm 和yum 的区别 rpm:redhat package manager yum可以解决依赖关系 yum 源配置 [epel] name=Extra Packages fo ...

  5. ansible 软件相关模块,剧本

    软件相关模块 yum rpm和yum的区别 rpm:redhat package manager yum 可以解决依赖关系 yum 源配置 使用yum下载时需要先下载epel [epel] name= ...

  6. 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&shell&script&copy

    大纲 1.系统安装与机器克隆 2.ansible介绍和host-pattern格式 3.command模块 4.shell模块 5.script模块 6.copy模块

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

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

  8. Ansible4:Ad-hoc与命令执行模块

    目录 Ad-hoc 命令说明 后台执行 命令执行模块 command模块 script模块 Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbo ...

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

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

随机推荐

  1. clojure-emacs-autocomplete

    1. https://github.com/clojure-emacs/cider#keyboard-shortcuts 2. install emacs 24.5 3. http://clojure ...

  2. LibreOJ 6280 数列分块入门 4(分块区间加区间求和)

    题解:分块的区间求和比起线段树来说实在是太好写了(当然,复杂度也高)但这也是没办法的事情嘛.总之50000的数据跑了75ms左右还是挺优越的. 比起单点询问来说,区间询问和也没有复杂多少,多开一个su ...

  3. springMVC工作原理趣味解析

    springMVC 涉及的人有: 1:浏览器                2:DispatherServlet  3:Handler             4:HandlerAdapter     ...

  4. CSVHelper 导出CSV 格式

    public class CSVHelper { System.Windows.Forms.SaveFileDialog saveFileDialog1;//保存 private string hea ...

  5. 让Fireball CodeEditor控件禁止中文双倍输入

    第一次使用这个控件的时候,输入注释时候, 中文都是双倍输入,很是郁闷,查到资料,在 让Fireball CodeEditor控件支持中文 这篇文章中使用的方法,将代码复制过来发现不适用, 后来再一次偶 ...

  6. Topshelf + Quartz2.5 创建基于windows服务

    1.创建一个定时调度Quartz类 using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; u ...

  7. Oracle下载及安装

    Oracle   下载及安装 一.官方下地址:   http://www.oracle.com/technetwork/database/enterprise-edition/downloads/in ...

  8. 团队内的沟通方式:网络 OR 当面

    4月1日,我和老王稍微聊了聊关于互联网的工作方式,团队的成员不在一个地方,通过远程互联网的方式进行沟通是不是OK.现在确实有很多公司是有多地的分公司,子公司,不同的团队在不同的办公室中,甚至一个大团队 ...

  9. kali linux之防火墙识别

    通过检查回包,可能识别端口是否经过防火墙过滤,设备多种多样,结果存在一定的误差 Send Response   Type SYN   NO    Filtered(先发送syn 如果不给回复 防火墙可 ...

  10. Charles设置可抓https的包

    一.help 二.proxy