问题
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
Script file:
#!/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;
When I tried to run pm2
command on the remote machine, It's working. But, not running from ansible script file.
Error:
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"]}
回答1:
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)).
Short answer is to use a different name for PATH
in your script and use absolute paths for commands in your script.
来源:https://stackoverflow.com/questions/47356230/ansible-inside-script-command-not-found