--- - name: Install basic development packages ansible.builtin.apt: name: # Development tools - git # Build tools - build-essential - python3 - python3-pip state: present become: true - name: Check if NodeSource Node.js is installed ansible.builtin.command: node --version register: node_version_check failed_when: false changed_when: false - name: Check if NodeSource repository exists and is correct ansible.builtin.shell: | if [ -f /etc/apt/sources.list.d/nodesource.list ]; then if grep -q "deb \[signed-by=/etc/apt/keyrings/nodesource.gpg\] https://deb.nodesource.com/node_22.x nodistro main" /etc/apt/sources.list.d/nodesource.list; then echo "correct_config" else echo "wrong_config" fi else echo "not_exists" fi register: nodesource_repo_check failed_when: false when: node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - name: Check if NodeSource GPG key exists and is correct ansible.builtin.shell: | if [ -f /etc/apt/keyrings/nodesource.gpg ]; then if file /etc/apt/keyrings/nodesource.gpg | grep -q "PGP"; then echo "correct_key" else echo "wrong_key" fi else echo "not_exists" fi register: nodesource_key_check failed_when: false when: node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - name: Remove incorrect NodeSource repository ansible.builtin.file: path: /etc/apt/sources.list.d/nodesource.list state: absent become: true when: - node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - nodesource_repo_check.stdout == "wrong_config" - name: Remove incorrect NodeSource key ansible.builtin.file: path: /etc/apt/keyrings/nodesource.gpg state: absent become: true when: - node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - nodesource_key_check.stdout == "wrong_key" - name: Create keyrings directory ansible.builtin.file: path: /etc/apt/keyrings state: directory mode: '0755' become: true when: - node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - nodesource_key_check.stdout in ["not_exists", "wrong_key"] - name: Add NodeSource GPG key only if needed ansible.builtin.get_url: url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key dest: /etc/apt/keyrings/nodesource.gpg mode: '0644' force: true become: true when: - node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - nodesource_key_check.stdout in ["not_exists", "wrong_key"] - name: Add NodeSource repository only if needed ansible.builtin.apt_repository: repo: "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" state: present update_cache: false become: true when: - node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - nodesource_repo_check.stdout in ["not_exists", "wrong_config"] - name: Install Node.js 22 from NodeSource ansible.builtin.apt: name: nodejs state: present become: true when: node_version_check.rc != 0 or not node_version_check.stdout.startswith('v2') - name: Verify Node.js installation ansible.builtin.command: node --version register: final_node_version changed_when: false - name: Display Node.js version ansible.builtin.debug: msg: "Node.js version installed: {{ final_node_version.stdout }}"