如何在ansible中使用npm

qacovj5a  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(183)

这是我在task中的主.yml文件:

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

以及运行脚本时的输出:

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

错误是“npm:未找到命令”,但我真的很确定安装和路径在机器中设置适当,我怀疑的是脚本
我不知道如何修改我的脚本,我尝试使用npm模块,但我失败了

bmp9r5qi

bmp9r5qi1#

问题是每个任务环境都是独立的,您在单独的任务中设置nvm环境。
Run build-dev”对“Use npm”中设置的路径一无所知
我建议将这两个任务结合起来,并在下面解释一些额外的更改:

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

其他变更:
1.在shell模块中使用bash -c "..."将导致/bin/sh -c "/bin/bash -c '...'",最好使用executable: /bin/bash

  1. Shell模块具有chdir参数,用于指定运行脚本的目录
    检查shell module documentation以了解其他参数和示例。

相关问题