ansible script 模块

本文介绍了Ansible的script模块,如何通过它在远程主机上执行预先存在于本地的脚本,区别于shell模块,重点讲解了参数如`chdir`、`cmd`、`creates`和`removes`的用法。实例演示了在Ansible中使用script模块进行脚本部署和执行的过程。
摘要由CSDN通过智能技术生成

文章目录

script 模块

指定本地的脚本文件,到远程主机运行一次

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

[root@master ~]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it	# 运行一个本地的脚本在远程的机器上
  script:
      chdir:                 #  此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中
      cmd:                   # Path to the local script to run followed by optional arguments.
      creates:               # 使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
      decrypt:               # This option controls the autodecryption of source files using vault.
      executable:            # Name or path of a executable to invoke the script with.
      free_form:             # Path to the local script file followed by optional arguments.
      removes:               # 使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。

参数含义和 shell 中类似
	free_form= 				# 必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。

  • 测试案例:
# 在本地准备一个测试脚本
[root@master ~]# echo -e "pwd\nhostname" > /root/test.sh
[root@master ~]# cat test.sh 
pwd
hostname

# 使用 ansible 测试使用本地脚本在远端机器上执行,此时的脚本是不需要在远程机器上存在的
[root@master ~]# ansible dong -m script -a "/root/test.sh"
192.168.169.161 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.169.161 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.169.161 closed."
    ], 
    "stdout": "/root\r\nnode01\r\n", 
    "stdout_lines": [
        "/root", 
        "node01"
    ]
}
192.168.169.162 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.169.162 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.169.162 closed."
    ], 
    "stdout": "/root\r\nnode02\r\n", 
    "stdout_lines": [
        "/root", 
        "node02"
    ]
}

 
 
 
 
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值