Ansible Playbook to Install Node.js on Ubuntu Machine


In this article, I will walk you through the process of installing Node.js on Ubuntu using the Ansible Playbook.

Step 1

On your Ansible control node, create a nodejs-install-inventory.txt file and add configurations related to your host server where you want to install Node.js, as shown below.

Replace ansible_host,  ansible_user & ansible_sudo_pass with your corresponding host server values.

ansible-target1 ansible_host= ansible_user=xxxx ansible_sudo_pass=xxxx

install Node.js on Ubuntu machine

Step 2

Next, create another file called ansible-install-nodejs-playbook.yml in the same directory where you have created the nodejs-install-inventory.txt file in Step 1. Add the below YAML configuration to it.

- hosts: all
  gather_facts: yes
  become: yes
    - name: Update apt cache
        update_cache: yes

    - name: Install Node.js dependencies
        name: "{{ item }}"
        state: present
        - curl
        - software-properties-common

    - name: Add NodeSource repository
        repo: "deb {{ ansible_distribution_release }} main"
        state: present
        update_cache: yes
        filename: nodesource

    - name: Install Node.js
        name: nodejs
        state: present

    - name: Install build-essential
        name: build-essential
        state: present

Step 3

From your Ansible control machine, navigate to your Ansible playbook directory using the command prompt or VS Code terminal and run the below Ansible playbook command.

ansible-playbook -i nodejs-install-inventory.txt ansible-install-nodejs-playbook.yml

install Node.js on Ubuntu machine

Step 4

If everything goes well, you will see the output similar to the screenshot below.

install Node.js on Ubuntu machine

Step 5

Open your host server and verify if Node.js was installed or not by running the below commands.

node -v
npm -v

install Node.js on Ubuntu machine


So in this article, we learned on how to install Node.js using Ansible Playbook on an Ubuntu machine.

Similar Articles