我有一个有趣的剧本,其中包含以下代码:
---
- name: This is an example of successfully running a command on a server
hosts: user@myServer
tasks:
- name: create the file ansibleMadeThis
command: touch ansibleMadeThis
我已经设置了ssh键,当我运行此剧本时,会在myServer上创建文件
但是我想在服务器上运行脚本,所以我了解了脚本模块here并写了这本剧本:
---
- name: This is an attempt to run a script called script.sh on a remote server
hosts: user@myServer
tasks:
- name: Run script
script: /home/user/script.sh
在服务器上,我在〜(又名/ home / user)中有一个脚本,称为script.sh。 script.sh包含以下内容:
touch ansibleCalledTheScriptThatMadeThis
当我以用户身份登录服务器时,运行此脚本可以正常工作,但是当我使用以下命令运行剧本时,出现以下错误:
ansible-playbook runScript.yml
错误消息:
fatal: [user@myServer]: FAILED! => {"changed": false, "msg": "Could not find or access '/home/user/script.sh' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
我也尝试运行以下命令:
---
- name: This is an attempt to run a script called script.sh on a remote server
hosts: user@myServer
tasks:
- name: Run script
command: /home/maxdeploy/script.sh
但是它给了我这个错误:
fatal: [user@server]: FAILED! => {"changed": false, "cmd": "/home/user/script.sh", "msg": "[Errno 8] Exec format error", "rc": 8}
请注意,我已将文件script.sh的权限设置为777(我执行过chmod 777 script.sh),所以不会出现任何权限问题。
答案 0 :(得分:1)
我怀疑问题是您的脚本不是以“ shebang”标记开头:
#!/bin/sh
您收到的“ Exec格式错误”消息表示内核不知道如何执行您要运行的程序。我可以复制这样的确切行为:
首先,我们创建一个内容为echo hello world
$ echo "echo hello world" > script.sh
现在我们尝试exec
:
$ python -c 'import os; os.execve("./script.sh", ["script.sh"], {})'
Traceback (most recent call last):
File "<string>", line 1, in <module>
OSError: [Errno 8] Exec format error
因此,在脚本顶部添加#!/bin/sh
标记,使其看起来像这样:
#!/bin/sh
touch ansibleCalledTheScriptThatMadeThis
...它应该可以正常运行。