Ansible Playbook to Install Node.js on Ubuntu Machine

Introduction

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=192.168.0.118 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
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

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

    - name: Add NodeSource repository
      apt_repository:
        repo: "deb https://deb.nodesource.com/node_14.x {{ ansible_distribution_release }} main"
        state: present
        update_cache: yes
        filename: nodesource

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

    - name: Install build-essential
      apt:
        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

Conclusion

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


Similar Articles