在本地写一个脚本,然后在远程服务器上执行。脚本一直存在于 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
来源:CSDN
作者:blueicex2020
链接:https://blog.csdn.net/blueicex2017/article/details/104133516