Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]
本文主要介绍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的设备(如路由器)。 在任何其他情况下,使用shell
或command
模块更为合适。- 就像
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.
注意事项
- 如果要安全可靠地执行命令,最好使用
shell
或command
模块来代替。 - 如果从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
模块设计用于简单场景,对于更复杂的需求,应该考虑在shell
或script
模块中使用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
模块设计用于简单场景,对于更复杂的需求,应该考虑在shell
或script
模块中使用expect代码
作者:hoxis
链接:https://www.jianshu.com/p/8661c107448d
來源:简书
Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]的更多相关文章
- ansible的安装及命令相关模块
ansible 第一步:下载epel源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos- ...
- ansible命令执行模块使用
ansible命令执行模块使用 1.命令执行模块-command 在远程节点上运行命令. 命令模块使用命令名称,接上空格-的分割符作为参数使用,但是不支持管道符和变量等,如果要使用这些,那么可以使用s ...
- ansible命令及模块
ping 命令 #测试单个主机 [root@node1 opt]# ansible -m ping 10.0.0.22 #获取多个主机 [root@node1 opt]# ansible 10.0.0 ...
- ansible软件相关模块丶计划任务,剧本
软件相关模块 yum rpm 和yum 的区别 rpm:redhat package manager yum可以解决依赖关系 yum 源配置 [epel] name=Extra Packages fo ...
- ansible 软件相关模块,剧本
软件相关模块 yum rpm和yum的区别 rpm:redhat package manager yum 可以解决依赖关系 yum 源配置 使用yum下载时需要先下载epel [epel] name= ...
- 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&shell&script©
大纲 1.系统安装与机器克隆 2.ansible介绍和host-pattern格式 3.command模块 4.shell模块 5.script模块 6.copy模块
- ansible笔记(6):常用模块之命令类模块
ansible笔记():常用模块之命令类模块 command模块 command模块可以帮助我们在远程主机上执行命令 注意:使用command模块在远程主机中执行命令时,不会经过远程主机的shell处 ...
- Ansible4:Ad-hoc与命令执行模块
目录 Ad-hoc 命令说明 后台执行 命令执行模块 command模块 script模块 Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbo ...
- ansible模块command、shell、raw、script
简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...
随机推荐
- C#中return的两个作用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- PLSQL Developer连接远程Oracle
注:内容来网络 (一)不安装客户端的解决办法. 第一种方法: 1.在安装ORACLE服务器的机器上搜索下列文件, oci.dll ocijdbc10.dll ociw32.dll orannzsbb1 ...
- OOP1(定义基类和派生类)
面向对象程序设计基于三个基本概念:数据抽象,继承和动态绑定 数据抽象是一种依赖于接口和实现分离的编程技术.继承和动态绑定对程序的编号有两方面的影响:一是我们可以更容易地定义与其它类相似但不完全相同的类 ...
- django 重写User表增加字段设置
models中: from django.contrib.auth.models import AbstractUser lass User(AbstractUser): mobile = model ...
- Python之路Python文件操作
Python之路Python文件操作 一.文件的操作 文件句柄 = open('文件路径+文件名', '模式') 例子 f = open("test.txt","r&qu ...
- 帝国cms刷新内容页提示.phome_ecms_news_data_' doesn't exist
帝国cms后台刷新提示.phome_ecms_news_data_' doesn't exist解决方法: 刷新所有信息内容页面时提示“Table '*.phome_ecms_article_data ...
- 通用动态树(Link-Cut Tree)模板
一个没有维护任何东西的动态树模板 忘了怎么写可以直接来粘 int ch[300010][2], fa[300010], st[300010]; bool lazy[300010]; bool nroo ...
- 使用webpack开发ES6程序的正确姿势
1.cnpm install babel-loader babel-core babel-preset-es2015 -D 2.cnpm install babel-plugin-transform- ...
- Vue-think脚手架
准备重构的项目,原来的后台是thinkPHP写的,刚刚摸VUE,不知道里面数据调用原理,想先安装vuethink学习一下. 结果安装半天,npm run dev的时候报错,尝试了很多方法,各种重装,看 ...
- SQL SERVER下有序GUID和无序GUID作为主键&聚集索引的性能表现
背景 前段时间学习<Microsoft SQL Server 2008技术内幕:T-SQL查询>时,看到里面关于无序GUID作为主键与聚集索引的建议,无序GUID作为主键以及作为聚集索引 ...