首页 > 解决方案 > 无法通过 ansible 运行 .sh 文件

问题描述

我有 ubuntu 服务器设置,我想在其中执行 shell 脚本。我尝试了以下代码,但没有运气。

ansible.yml 文件:

- hosts: testserver
  remote_user: ubuntu
  become: yes
  tasks:
  - name: copy files and execute it
    script: /media/anish/linux_files/ansible-terraform/play.sh

play.sh 文件:

#!bin/sh
echo 'hello'

它给出以下错误:

fatal: [13.213.12.27]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 127, "stderr": "Shared connection to 13.213.12.27 closed.\r\n", "stderr_lines": ["Shared connection to 13.213.12.27 closed."], "stdout": "/bin/sh: 1: /home/ubuntu/.ansible/tmp/ansible-tmp-1617116935.2170017-256995291519228/play.sh: not found\r\n", "stdout_lines": ["/bin/sh: 1: /home/ubuntu/.ansible/tmp/ansible-tmp-1617116935.2170017-256995291519228/play.sh: not found"]}

但是,我可以通过其他方式执行它:通过复制命令将文件复制到服务器并在那里执行。但是这种方法有什么问题呢?

标签: ubuntuansible

解决方案


但是,我可以通过其他方式执行它:通过复制命令将文件复制到服务器并在那里执行。但是这种方法有什么问题呢?

如果要在目的主机上运行,​​需要先复制脚本。

如果您宁愿在运行 Ansible 的主机上本地运行它,则需要添加

delegate_to: localhost
- hosts: testserver
  remote_user: ubuntu
  become: yes
  tasks:
  - name: copy files and execute it
    script: /media/anish/linux_files/ansible-terraform/play.sh
    delegate_to: localhost

推荐阅读