Ansible模块介绍——命令模块

简介: Ansible模块介绍——命令模块

@[toc]A

命令模块

  • command
  • shell
  • raw
  • script

应尽量避免使用这三个模块来执行命令,因为其他的模块大部分都具有幂等性,可以自动进行更改跟踪。command、shell、raw不具备幂等性。

**注:**幂等性:简单来讲,就是输入相同,输出相同,无论多少 次比如说,确认接口,如果传入订单号,返回确认OK,如果已经确认过了,再次调用确认接口,返回如 果还是确认OK,那么这个接口就是满足幂等性。

command、shell、raw、script的区别

command、shell模块:

  • 相同点:要求受管主机上安装python
  • 不同点:
    command可以在受管主机上执行 shell 命令,但是不支持环境变量和操作符(例如 ‘|’, ‘<’, ‘>’, ‘&’)
    shell模块调用的/bin/sh指令执行

raw模块:

  • 不需要受管主机上安装Python,直接使用远程 shell 运行命令,通常用于无法安装Python的系统(例如 网络设备等)。

script模块:

  • 在远程主机上执行脚本文件 ,和 raw 模块一样,不要求目标主机上已经装好 python。

command模块参数

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

free_form参数:必须参数,指定需要远程执行的命令。需要说明一点,free_form参数与其他参数(如果想要使用一个参数,那么则需要为这个参数赋值,也就是name=value模式)并不相同。比如,当我们想要在远程主机上执行 ls 命令时,我们并不需要写成free_form=ls这样写反而是错误的,因为并没有任何参数的名字是free_form,当我们想要在远程主机中执行ls命令时,直接写成ls即可。因为command模块的作用是执行命令,所以,任何一个可以在远程主机上执行的命令都可以被称为free_form

列出指定目录下的文件

[root@centos7 ~]# ansible web -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"     当文件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
initial-setup-ks.cfgls: 无法访问'|': 没有那个文件或目录
ls: 无法访问'grep': 没有那个文件或目录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 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 >>

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.

script模块

名称 必选 备注
chdir no 运行command命令前先cd到这个目录
creates no 如果这个参数对应的文件存在,就不运行command
decrypt 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"
   ]
 }

**注:**和shell模块的不同,shell模块是要求客户端上有这个脚本才能执行;script 是要求ansible服务端有这个脚本就可以了,执行的时候是不会拷贝这个脚本到客户端的。4

相关文章
|
12月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
279 0
|
2月前
|
缓存 Shell Linux
[ansible]常用内置模块
[ansible]常用内置模块
|
3月前
|
Shell 应用服务中间件 Linux
Ansible的常用模块
Ansible的常用模块
56 6
|
3月前
|
Shell 数据安全/隐私保护
Ansible Ad-hoc,命令执行模块
Ansible Ad-hoc,命令执行模块
31 1
|
3月前
|
运维 Linux 应用服务中间件
Linux之自动化运维工具ansible、ansible模块(2)
Linux之自动化运维工具ansible、ansible模块(2)
|
3月前
|
运维 Linux Shell
Linux之自动化运维工具ansible、ansible模块(1)
Linux之自动化运维工具ansible、ansible模块(1)
|
5月前
|
算法 安全 Linux
Ansible 中的copy 复制模块应用详解
Ansible 中的copy 复制模块应用详解
307 1
|
12月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
177 0
|
11月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
121 1
|
11月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
88 0