ansible无法找到或访问脚本

ansible无法找到或访问脚本,ansible,Ansible,我有一本ansible剧本,其中包含以下代码: --- - 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上生成一个文件 但

我有一本ansible剧本,其中包含以下代码:

---
- 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上生成一个文件

但是,我想在服务器上运行脚本,因此我阅读了有关脚本模块的内容,并编写了以下剧本:

---
- 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
在服务器上,我有一个名为script.sh的脚本(aka/home/user)。script.sh包含以下内容:

touch ansibleCalledTheScriptThatMadeThis
---
- 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
当我以用户身份登录到服务器时,运行此脚本可以正常工作,但是当我使用以下命令运行playbook时,会出现以下错误:

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"}
此外,我还尝试运行以下程序:

touch ansibleCalledTheScriptThatMadeThis
---
- 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),因此不应该存在任何权限问题。

我怀疑问题在于您的脚本没有以“”标记开头:

您得到的“Exec format error”消息意味着内核不知道如何执行您试图运行的东西。我可以像这样复制这种行为:

#!/bin/sh
touch ansibleCalledTheScriptThatMadeThis
  • 首先,我们创建一个包含内容的文件
    echo hello world

    $ echo "echo hello world" > script.sh
    
  • 现在我们尝试执行它:

    $ 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
    

    …它应该可以正常运行。

    我怀疑问题在于您的脚本没有以“”标记开头:

    您得到的“Exec format error”消息意味着内核不知道如何执行您试图运行的东西。我可以像这样复制这种行为:

    #!/bin/sh
    touch ansibleCalledTheScriptThatMadeThis
    
    • 首先,我们创建一个包含内容的文件
      echo hello world

      $ echo "echo hello world" > script.sh
      
    • 现在我们尝试执行它:

      $ 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
      
      …它应该可以正常运行