Ansible运维监控

【Ansible学习】- 命令相关模块command, shel

2018-01-11  本文已影响66人  hoxis

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

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进程下执行命令,从而支持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

注意事项

script模块

简介

模块参数

名称 必选 默认值 可选值 备注
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"
    ]
}

注意事项

raw模块

简介

模块参数

名称 必选 备注
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.

注意事项

expect模块

简介

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

模块参数

名称 必选 默认值 备注
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

注意事项

telnet模块

简介

模块参数

名称 必选 默认值 备注
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

注意事项

上一篇 下一篇

猜你喜欢

热点阅读