ansible-command not found

0 votes

Here's my Ansible playbook:

-   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

and here's the 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;

But when I try to run pm2 command on the remote machine, it works, but if I do the same using ansible script this comes up

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"]}

Apr 30, 2018 in Ansible by shubham
• 6,890 points
4,115 views

2 answers to this question.

0 votes
You've been overwriting PATH env variable in your script file. Use a different variable name for PATH in your script file and also use absolute paths for commands
answered Apr 30, 2018 by DareDev
• 6,810 points
0 votes
Use some other variable instead of PATH.
answered Apr 23 by Vismaya

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer

When conditional not working in Ansible

Your identation is wrong and when is ...READ MORE

answered Apr 27, 2018 in Ansible by ajs3033
• 7,280 points
870 views
0 votes
1 answer
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
215 views
0 votes
1 answer
0 votes
1 answer

Running ansible command on a single server when it is deployed to multiple servers

You can try the run_once attribute: Example from ...READ MORE

answered Jun 13, 2018 in Ansible by DareDev
• 6,810 points
218 views
0 votes
1 answer

How can I create a new user using ansible ploybook?

You're switching from the root user. Root ...READ MORE

answered Mar 29, 2018 in Ansible by DareDev
• 6,810 points
132 views