Execute npm and node using ansible

I am facing a challenge with the script execution on a machine that has zsh installed. Here is what I have set up:

Software Installed

# Install NVM
sudo curl https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
## Reload shell to start using nvm
. ~/.zshrc
. ~/.nvm/nvm.sh
nvm install 0.12

My next step is to connect to the instance and run an ansible-playbook which will execute the following script:

sh-script.sh

npm install aws-sdk


node create-queue.js $machine_name
node create-queue.js $machine_name

However, upon running the playbook, I encounter the following errors: line 28: npm: command not found -- line 32: node: command not found -- line 33: node: command not found.

Interestingly, when I SSH into the instance and directly run "node" or "npm", I receive the expected responses without any issues. The ansible-playbook structure is quite simple:

ansible

- hosts: tag_Name_TestInstance
  tasks:
    - name: Run Script
      shell: /home/ubuntu/sh-script.sh '{{ machine_name }}'

Answer №1

To overcome this challenge, I recommend including an extra bin path in the ansible environment.

  - name: Setup aws-sdk
    command: /opt/node/bin/npm "something"
    environment:
      PATH: "{{ ansible_env.PATH }}:/opt/node/bin"

Alternatively, you can utilize the built-in ansible npm function.

   description: Install node.js package "aws-sdk".
  - npm: name=aws-sdk path=/app/location
    environment:
      PATH: "{{ ansible_env.PATH }}:/opt/node/bin"

Include two additional tasks to execute the node js script for a better understanding of the process.

Best of luck!

Answer №2

One possible reason for this issue could be that npm and node are not included in your system's $PATH. In such cases, the shell module may default to using /bin/sh to execute commands.

To resolve this and run the script in zsh, consider including a shebang line with the path to your zsh installation. For example: !#/usr/local/bin/zsh

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Strategies for extracting targeted information from my mondoDB records

I am seeking guidance on selecting specific data to transmit to my state, as currently it is sending all information when I only require 'firstname', 'lastname', email, and so forth. Below is the NodeJS code snippet for my backend call ...

What are the steps to fix a timeout error with React.js and socket.io acknowledgements?

My setup includes a Node.js server and a React.js client application. Data is exchanged between them using socket.io, but I'm running into an issue with implementing acknowledgment. Whenever I try to implement acknowledgment, I receive a timeout error ...

What is the best way to monitor the status of the mongoose connection in Node

Is there a way to continuously monitor the status of the mongoose package connection string after starting an instance, similar to when running 'npm start'? I attempted the following approach: setInterval(function(){ if(mongoose.connection.re ...

Discovering the appropriate node version for a project

Is it possible to determine the appropriate node version for a repository without resorting to trial and error? As web frameworks continue to evolve rapidly, revisiting projects from months or even years ago has become a common occurrence. I've foun ...

Executing the eslint command upon every save action

My package.json script is configured as follows: "scripts": { "lint": "eslint src webpack.config.js || exit 0" }, Is there a way to automate the execution of this lint command each time I save a file, eliminating the ...

Is it necessary to have both Express and express-generator in my toolbox?

Recently delving into the world of node.js and stumbled upon express; While browsing the npm repository site https://www.npmjs.com/package/express, I noticed that the installation process is clearly stated as: $ npm install express However, further down ...

Unforeseen behavior of React Router on the server side

In the code snippet below, my server.js file is configured to handle server-side rendering with React and Express: import express from 'express'; import path from 'path'; import React from 'react'; import { renderToString } f ...

Leverage dockerfile for constructing npm project

Here is my Dockerfile: FROM node:10-alpine RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app WORKDIR /home/node/app COPY package*.json ./ USER node RUN npm install COPY --chown=node:node . . RUN npm run build I ...

Express npm dependency fails to start globally

I have recently reinstalled my operating system from Windows 8.1 to Windows 8.1, and I have been using npm for quite some time. Previously, it was working fine as mentioned here. After the reinstallation, I tried installing npm i -g express, but it does n ...

Aggregating data with multiple arguments in MongoDB is a powerful

I'm attempting to retrieve multiple arguments from my MongoDB database. Currently, my code fetches the distinct values for dates and counts them. However, I am unsure how to include another argument to fetch a different set of distinct values and coun ...

Combining a web-based login system with a duo device management client library

Seeking advice on integrating Duo device management portal (DMP) libraries into a website for baking. Currently, we have a functioning web page with ldap authentication served through Apache on a Linux server. Users must input their ldap credentials to ac ...

Necessary firewall rules for connecting to Node applications within a local network

How can I make an Express REST API accessible over a LAN? I believe I need to adjust my firewall settings to enable this access. By investigating the mentions of Node.js: Server-side Javascript in Windows Defender, I was able to resolve this issue. After ...

Is there a way to push the modifications I've made in node_modules back to git?

At times, maintaining a fork of a node package for your module can be more convenient. I am looking to make edits to a module that is located in node_modules, which I installed using npm install githubaccount/myrepo.git. Currently, any changes I make to a ...

What is the reason for not displaying the various li elements on my webpage?

Here is the code snippet export default function DisplaySearchResults({ results }) { var arr = Object.entries(results) console.log(arr) return ( <div> Here are the search results : <ol> {arr.map((va ...

"Discovering an issue where a search query returns empty results in nodejs and mongodb when parameters are utilized in the

Searching for users in close proximity with specific criteria, I can't seem to get the desired results when using field1=No and field2=No (which is what cat represents as field2). The query returns empty. function (currentLoc, radius, cat, db, callba ...

Alert: Github Dependabot has flagged Babel as vulnerable to arbitrary code execution when compiling meticulously designed malicious code

My Github Repository's Security section is flagging this issue as Critical for both my Frontend and Backend code. I'm having trouble grasping the nature of this alert. Can someone explain what flaw it represents? After updating my dependencies, ...

"Encountering errors during npm installation due to a failed preinstall

Having identified security vulnerabilities for knexnest > knex > minimist, I encountered an issue where the minimist version did not update using npm audit fix or a simple npm update. Following the steps outlined in this informative article resolved the vu ...

Make sure that JSON.stringify is set to automatically encode the forward slash character as `/`

In my current project, I am developing a service using nodejs to replace an old system written in .NET. This new service exposes a JSON API, and one of the API calls returns a date. In the Microsoft date format for JSON, the timestamp is represented as 159 ...

Update an existing JSON document

Looking for a solution to update the json file while retaining specific strings and values? const fs = require('fs'); var logPath = 'log.json' var logRead = fs.readFileSync(logPath) var logFile = JSON.parse(logRead) LogChannel = &apo ...

Combining the power of Node.js and Node-MySQL with Express 4 and the versatile Mustache

Currently, I am delving into the world of Node.js and encountering a bit of a roadblock. My focus is on passing a query to Mustache. Index.js // Incorporate Express Framework var express = require('express'); // Integrate Mustache Template En ...