找不到Ansible内部脚本命令 [英] Ansible inside script command not found
问题描述
Ansible:
- name: Node package manager
npm:
name: pm2
global: yes
notify:
- restart nginx
- name: start the app
script: ../files/common/pm2.sh app_name {{ user }}
tags: test
脚本文件:
#!/bin/bash
APP_NAME=$1
USER=$2
if [ "$USER" != "" ]; then
PATH="/home/$USER/"
else
PATH="/var/www/"
fi
pm2 describe ${APP_NAME} > /dev/null # line no 11
RUNNING=$?
if [ "${RUNNING}" -ne 0 ]; then
cd ${PATH}${APP_NAME}/ && pm2 start npm --name "${APP_NAME}" -- start
else
pm2 restart ${APP_NAME}
fi;
当我尝试在远程计算机上运行 pm2
命令时,它正在工作.但是,不是从ansible脚本文件运行.
When I tried to run pm2
command on the remote machine, It's working. But, not running from ansible script file.
错误:
致命:[网络服务器]:失败!=> {已更改":true,失败":true,"msg":非零返回码","rc":1,"stderr":与xx.xx.xx.xx的共享连接已关闭.\ r \ n," stdout:"/home/ronak/.ansible/tmp/ansible-tmp-1510939424.06-225768915266978/pm2.sh:第11行:pm2:找不到命令\ r \ n127 \ r \ n","stdout_lines":["/home/ronak/.ansible/tmp/ansible-tmp-1510939424.06-225768915266978/pm2.sh:第11行:pm2:找不到命令","127"]}
fatal: [webserver]: FAILED! => {"changed": true, "failed": true, "msg": "non-zero return code", "rc": 1, "stderr": "Shared connection to xx.xx.xx.xx closed.\r\n", "stdout": "/home/ronak/.ansible/tmp/ansible-tmp-1510939424.06-225768915266978/pm2.sh: line 11: pm2: command not found\r\n127\r\n", "stdout_lines": ["/home/ronak/.ansible/tmp/ansible-tmp-1510939424.06-225768915266978/pm2.sh: line 11: pm2: command not found", "127"]}
推荐答案
您正在覆盖脚本中的 PATH
环境变量.这用于确定可执行文件的位置(请参见 https://en.wikipedia.org/wiki/PATH_(变量)).
You're overwriting the PATH
environment variable in your script. This is used to determine where executables are located (see https://en.wikipedia.org/wiki/PATH_(variable)).
简短的答案是为脚本中的 PATH
使用不同的名称,并对脚本中的命令使用绝对路径.
Short answer is to use a different name for PATH
in your script and use absolute paths for commands in your script.
这篇关于找不到Ansible内部脚本命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!