Ansible:强制安装最新的 nodejs 包

Ansible: force installing latest nodejs package

我不明白为什么 Ansible 会安装旧版本的 nodejs,即使我将它设置为安装最新版本:

- name: NodeJS => Install NodeJS
  apt:
      pkg: "{{ item }}"
      state: latest
      force: yes
      update_cache: yes
  with_items:
    - nodejs
    - npm
  become: yes

- name: NodeJS => Create link symlink for node
  become: yes
  file:
    src: /usr/bin/nodejs
    dest: /usr/bin/node
    state: link

对于我现在拥有的当前版本:

$ node -v
v0.10.25

$ npm -v
1.3.10

更新解决方案

我终于在 task/main.yml

中做到了
---
- name: Ensure apt-transport-https is installed.
  apt: name=apt-transport-https state=present

- name: Add Nodesource apt key.
  become: yes
  apt_key:
    url: https://keyserver.ubuntu.com/pks/lookup?op=get&fingerprint=on&search=0x1655A0AB68576280
    id: "68576280"
    state: present

- name: Add NodeSource repositories for Node.js.
  become: yes
  apt_repository:
    repo: "{{ item }}"
    state: present
  with_items:
    - "deb https://deb.nodesource.com/node_{{ params['nodejs'].version }} {{ ansible_distribution_release }} main"
#    - "deb-src https://deb.nodesource.com/node_{{ params['nodejs'].version }} {{ ansible_distribution_release }} main"
  register: node_repo

- name: Update apt cache if repo was added.
  become: yes
  apt: update_cache=yes
  when: node_repo.changed

- name: Ensure Node.js and npm are installed.
  become: yes
  apt: "name=nodejs={{ params['nodejs'].version|regex_replace('x', '') }}* state=present"

您需要先为要安装的 nodejs 分支设置 ppa 存储库,例如 6.x 分支(如果需要 7.x 分支,请更改)

- apt_repository:
    repo: deb https://deb.nodesource.com/setup_6.x nodejs
    state: present

然后就可以安装node和npm了。