Search code examples
ansiblepm2

Ansible error executing pm2 startup command


When executing ansible playbook with command: ansible-playbook 2_installJsReport.yml

CentOS 7.6

Ansible 2.7.10

i get an error saying:

TASK [make jsreport start at system restart] >*****************************************************************************>************************************** fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["pm2", "startup"], >"delta": "0:00:00.601130", "end": "2019-04-24 12:59:33.091819", "msg": "non->zero return code", "rc": 1, "start": "2019-04-24 12:59:32.490689", "stderr": >"", "stderr_lines": [], "stdout": "[PM2] Init System found: systemd\n[PM2] To >setup the Startup Script, copy/paste the following command:\nsudo env >PATH=$PATH:/home/username/.nvm/versions/node/v8.11.3/bin >/home/username/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/bin/pm2 >startup systemd -u username --hp /home/username", "stdout_lines": ["[PM2] >Init System found: systemd", "[PM2] To setup the Startup Script, copy/paste >the following command:", "sudo env >PATH=$PATH:/home/username/.nvm/versions/node/v8.11.3/bin >/home/username/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/bin/pm2 >startup systemd -u username --hp /home/username"]}

Ansible script

---
- hosts: localhost

  tasks:

    - name: make jsreport start at system restart
      command: pm2 startup   

Solution

  • The "error" message contains instructions you are supposed to follow to configure the startup:

    [PM2] Init System found: systemd
    [PM2] To setup the Startup Script, copy/paste the following command: sudo env PATH=$PATH:/home/username/.nvm/versions/node/v8.11.3/bin /home/username/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/bin/pm2 startup systemd -u username --hp /home/username

    If you follow those instructions, it suggests that you should replace your task with something like:

    ---
    - hosts: localhost
    
      tasks:
    
        - name: make jsreport start at system restart
          become: true
          command: pm2 startup systemd -u username --hp /home/username
          environment:
            PATH: "{{ ansible_env.PATH }}"