ansible的script模块

功能:把管理机器上的脚本远程的传输到被管理节点上去执行

比起shell模块,script模块更强大,在m01机器本地有一份脚本,就可以在所有被管理节点上去运行

script的模块参数

[root@m01 ~]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it
  script:
      chdir:                 # Change into this directory on the remote node before running
                               the script.
      cmd:                   # Path to the local script to run followed by optional
                               arguments.
      creates:               # A filename on the remote node, when it already exists, this
                               step will *not* be run.
      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:               # A filename on the remote node, when it does not exist, this
                               step will *not* be run.
 

 应用案例

1.在管理节点上创建脚本
[root@m01 ~]#  echo -e "pwd\nhostname">local_hostname.sh
[root@m01 ~]# cat local_hostname.sh 
pwd
hostname
2.授权
[root@m01 ~]# chmod +x local_hostname.sh

远程的批量执行脚本,且在客户端上不需要存在该脚本

[root@m01 ~]# ansible change -m script -a "local_hostname.sh" 
192.168.200.5 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.200.5 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.200.5 closed."
    ], 
    "stdout": "/root\r\nrsnc01\r\n", 
    "stdout_lines": [
        "/root", 
        "rsnc01"
    ]
}
192.168.200.6 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.200.6 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.200.6 closed."
    ], 
    "stdout": "/root\r\nnfs01\r\n", 
    "stdout_lines": [
        "/root", 
        "nfs01"
    ]
}

显示执行成功,输出了工作目录和主机名
"stdout_lines": [
        "/root", 
        "nfs01"
    ]

 利用script模块,可以批量让所有被管理的机器执行脚本,且该脚本不需要在客户端上存在

你可能感兴趣的:(ansible,ansible)