Ansible模块——script

早过忘川 提交于 2020-02-01 16:11:26

在本地写一个脚本,然后在远程服务器上执行。脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。

一、主要参数

free_form:required,指定需要执行的脚本,脚本位于 ansible 管理主机本地。
chdir: 远程主机中执行工作目录。
creates:指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本。
removes:指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本。

二、示例

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

[root@ansible-manager ~]# ansible host1 -m script -a "chdir=/opt /testdir/testscript.sh"

host1 主机中的 /testdir/testfile1文件已经存在,ansible 主机中的 /testdir/testscript.sh 脚本将不会在 host1 主机中执行。

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

host1 主机中的 /testdir/testfile1 文件存在,ansible 主机中的 /testdir/testscript.sh 脚本则会在 host1 主机中执行。

[root@ansible-manager ~]# ansible host1 -m script -a "removes=/testdir/testfile1 /testdir/testscript.sh"
[admin@node1 tmp]$ ansible webserver -m script -a 'a.sh' -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by 
setting deprecation_warnings=False in ansible.cfg.

————Blueicex 2020/2/1 14:40 blueice1980@126.com

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!