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
• 7,340 points
10,943 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,890 points
0 votes
Use some other variable instead of PATH.
answered Apr 23, 2019 by Vismaya

Related Questions In Ansible

0 votes
1 answer

bash: ansible: command not found. Failed to search for file: Cannot update read-only repo

Hi@akhtar, By default, Ansible is not part of ...READ MORE

answered Jul 28, 2020 in Ansible by MD
• 95,460 points
10,996 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

Ansible playbook for a shall script

You can convert a shell script to ...READ MORE

answered Aug 26, 2019 in Ansible by Anjali
1,576 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,063 views
0 votes
1 answer
+1 vote
2 answers

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 14, 2018 in Ansible by DareDev
• 6,890 points
2,484 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,890 points
919 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP