08-script模块Ansible常用模块

简介: 08-script模块Ansible常用模块

一、概述
script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。
学习此模块之前,请先学习 command 模块。
二、常用参数
free_form参数 :必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。
chdir参数 : 此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中。
creates参数 :使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
removes参数 :使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。
三、示例

[root@ansible-manager ~]# ansible ansible-demo3 -m script -a "chdir=/opt /testdir/testscript.sh"
ansible-demo3 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to ansible-demo3 closed.\r\n", 
    "stdout": "testscript\r\n", 
    "stdout_lines": [
        "testscript"
    ]
}

其中 testscript.sh 脚本为打印 ‘testscript’ 字符串。

[root@ansible-manager ~]# cat /testdir/testscript.sh
echo 'testscript'

上面命令表示 ansible 主机中的 /testdir/testscript.sh 脚本将在 ansible-demo3 主机中执行,执行此脚本之前,会先进入到 ansible-demo3 主机中的 /opt 目录

[root@ansible-manager ~]# ansible ansible-demo3 -m script -a "creates=/testdir/testfile1 /testdir/testscript.sh"
ansible-demo3 | SKIPPED

上面命令表示,ansible-demo3 主机中的 /testdir/testfile1文件已经存在,ansible 主机中的 /testdir/testscript.sh 脚本将不会在 ansible-demo3 主机中执行。

[root@ansible-demo3 ~]# ls /testdir/
test  testfile1  testfile2

由于 testfile1 已经存在,则 SKIPPED。

[root@ansible-manager ~]# ansible ansible-demo3 -m script -a "removes=/testdir/testfile1 /testdir/testscript.sh"
ansible-demo3 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to ansible-demo3 closed.\r\n", 
    "stdout": "testscript\r\n", 
    "stdout_lines": [
        "testscript"
    ]
}

上面命令表示,ansible-demo3 主机中的 /testdir/testfile1 文件存在,ansible 主机中的 /testdir/testscript.sh 脚本则会在 ansible-demo3 主机中执行。

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