geerlingguy / ansible-role-nodejs

Ansible Role - Node.js
https://galaxy.ansible.com/geerlingguy/nodejs/
MIT License
410 stars 252 forks source link

Specify output path in /etc/apt/sources.list.d/ #124

Closed dns2utf8 closed 3 years ago

dns2utf8 commented 3 years ago

Hi

Thank you for this role, it helps a lot. I have noticed that I need to manually cleanup the old versions like /etc/apt/sources.list.d/deb_nodesource_com_node_8_x.list when changing to a different version. This is problematic because

Looking forward hearing from you, Stefan

thbar commented 3 years ago

I'm upgrading a setup from 12.x to 14.x and wondered about the same.

My current solution is to add a custom task running after this role, with (adapted from https://github.com/geerlingguy/ansible-role-nodejs/blob/439675c3cee118707bae026e76b7cd94c76d95ce/tasks/setup-Debian.yml):

# remove_old_node.yml
- name: Remove previous NodeSource repositories for Node.js.
  apt_repository:
    repo: "{{ item }}"
    state: absent # the important bit
  with_items:
    - "deb https://deb.nodesource.com/node_{{ delete_nodejs_version }} {{ ansible_distribution_release }} main"
    - "deb-src https://deb.nodesource.com/node_{{ delete_nodejs_version }} {{ ansible_distribution_release }} main"
  when: delete_nodejs_version is defined

I can then optionally set the delete_nodejs_version to e.g. 12.x to remove the old version.

I have coupled this with a ServerSpec to lock the behaviour down:

  describe command('ls /etc/apt/sources.list.d') do
    let(:sources) {
      subject.stdout.split("\n").sort
    }
    it "contains the exact set of specified sources" do
      expect(sources).to eq([
        "apt_postgresql_org_pub_repos_apt.list", 
        "deb_nodesource_com_node_14_x.list", 
        "oss_binaries_phusionpassenger_com_apt_passenger.list", 
        "yarn.list"
      ])
    end
  end
stale[bot] commented 3 years ago

This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution!

Please read this blog post to see the reasons why I mark issues as stale.

stale[bot] commented 3 years ago

This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details.

ericp1337 commented 2 years ago

Another way to remove old versions of the NodeJS repositories is to use a task like this where you use the lineinfile module to look for all lines which do not match the currently selected NodeJS version and remove those lines.

- name: software | nodejs | remove old repositories
  lineinfile:
    path: /etc/apt/sources.list.d/nodesource.list
    regexp: 'https:\/\/deb\.nodesource\.com\/node_(?!{{ nodejs_version|regex_replace("x", "") }})'
    state: absent
  when: ansible_os_family == "Debian"

This is what I've come up with which doesn't require maintaining a separate "old node versions" list. Maybe someone knows of a cleaner way to do it?