This is my main.yml file in task:
- name: Use npm
shell: >
/bin/bash -c "source $HOME/.nvm/nvm.sh && nvm use 16.16.0"
become: yes
become_user: root
- name: Run build-dev
shell: |
cd /home/ec2-user/ofiii
npm install
npm run build-dev
become: yes
become_user: root
when: platform == "dev"
And the output when running the script:
fatal: [172.31.200.13]: FAILED! => {
"changed": true,
"cmd": "cd /home/ec2-user/ofiii\nnpm install\nnpm run build-stag\n",
"delta": "0:00:00.061363",
"end": "2022-11-09 09:45:17.917829",
"msg": "non-zero return code",
"rc": 127,
"start": "2022-11-09 09:45:17.856466",
"stderr": "/bin/sh: line 1: npm:命令找不到\n/bin/sh: line 2: npm:命令找不到",
"stderr_lines": ["/bin/sh: line 1: npm:命令找不到", "/bin/sh: line 2: npm:命令找不到"],
"stdout": "",
"stdout_lines": []
}
the error is "npm:command not found" but I am really sure about the installation and the path to be set appropriatelly in the machine, the thing that I doubting is the script
I don't know how to modify my script,I tried to use npm module, but I failed
CodePudding user response:
The problem is that each task environment is separate and you are setting nvm
environment in a separate task.
The "Run build-dev" knows nothing about the paths set up from "Use npm"
I'd suggest combining these two tasks, with a few additional changes explained below:
- name: Run build-dev
shell: |
source $HOME/.nvm/nvm.sh
nvm use 16.16.0
npm install
npm run build-dev
args:
executable: /bin/bash
chdir: /home/ec2-user/ofiii
become: yes
become_user: root
when: platform == "dev"
Additional changes:
- Using
bash -c "..."
in a shell module would result/bin/sh -c "/bin/bash -c '...'"
, it's better to useexecutable: /bin/bash
instead - Shell module has
chdir
argument to specify the directory the script to run at
Check shell module documentation for other arguments and examples.