我有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.rn", "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 foundrn", "stdout_lines": ["/bin/sh: 1: /home/ubuntu/.ansible/tmp/ansible-tmp-1617116935.2170017-256995291519228/play.sh: not found"]}
不过,我可以通过其他方式执行它:通过复制命令将文件复制到服务器并在那里执行。但是这种方法有什么问题呢?
但是,我可以通过其他方式执行它:通过复制命令将文件复制到服务器并在那里执行。但是这种方法有什么问题呢?
如果要在目标主机上运行它,则需要首先复制脚本。
如果您更愿意在运行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