繁体   English   中英

为 Ansible 安装 NodeJS LTS

[英]Installing NodeJS LTS for Ansible

我正在寻找合适的 Ansible 角色或 Ansible YAML 文件,以便在 Ubuntu 16.04.3 xenial 系统上安装 NodeJS LTS。 我尝试了超过 10 个来自 Galaxy 的 Ansible 角色,但没有发现它们中的任何一个工作(抛出错误,例如potentially dangerous to add this PPA etc. 。.

任何人都可以提供任何 Ansible 剧本或建议我在 Ubuntu 16.04 上安装 NodeJS LTS 的角色吗?

这是工作示例:

---
- hosts: all
  gather_facts: yes
  become: yes
  vars:
    NODEJS_VERSION: "8"
    ansible_distribution_release: "xenial" #trusty
  tasks:
    - name: Install the gpg key for nodejs LTS
      apt_key:
        url: "https://deb.nodesource.com/gpgkey/nodesource.gpg.key"
        state: present

    - name: Install the nodejs LTS repos
      apt_repository:
        repo: "deb https://deb.nodesource.com/node_{{ NODEJS_VERSION }}.x {{ ansible_distribution_release }} main"
        state: present
        update_cache: yes

    - name: Install the nodejs
      apt:
        name: nodejs
        state: present

希望它会帮助你

并不是说我真的很高兴不得不这样做,但是......

(环境:Ubuntu 18.04,ansible 2.6.1,主机:macOS

来自https://github.com/nodesource/distributions/blob/master/README.md#debinstall

- name: install node 
  shell: |
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - && sudo apt-get install -y nodejs

结果:

> vagrant@vagrant:~$ node --version
v10.15.2

并且npm一定出现了:

vagrant@vagrant:~$ npm --version
6.4.1

在我运行这个时, https: //www.npmjs.com/package/npm显示6.8.0是最新的, 6.4.1是 6 个月前的。 Node 显示10.15.2是 10.x 系列中的最新版本,日期为 5 天前。

顺便说一句,我也尝试过apt-get但以节点 8.x 而不是 10.x 结束

我没有使用 ansible Galaxy 角色的原因是我没有看到任何似乎来自知名作者并且拥有大量明星和下载的 nodejs 角色(我很谨慎和怀疑)。

更新 npm

我的开发机器有6.8.0所以我添加了这个:

变量.yml :

versions:
  npm: "6.8.0"

剧本.yml :

- name: npm self-update
  command: npm install npm@{{ versions.npm }} -g

这让我一路:

vagrant@vagrant:~$ npm --version
6.8.0

接受的答案很好,但如果您愿意,可以使用已发现的变量作为发行版代号(即ansible_lsb.codename )。 此外,确保主机上安装了gcc g++ make可确保 nodejs 的本机插件正常工作。

只需将节点版本12替换为您想要的即可。

---
- name: Install nodejs
  hosts: all
  become: true
  tasks:
    - name: install nodejs prerequisites
      apt:
        name:
          - apt-transport-https
          - gcc
          - g++
          - make
        state: present
    - name: add nodejs apt key
      apt_key:
        url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
        state: present
    - name: add nodejs repository
      apt_repository:
        repo: deb https://deb.nodesource.com/node_12.x {{ ansible_lsb.codename }} main
        state: present
        update_cache: yes
    - name: install nodejs
      apt:
        name: nodejs
        state: present

您可以使用:

ansible-galaxy install nodesource.node

然后在你的剧本上,添加roles: - nodesource.node

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM