Ansible-playbook to install and configure Nginx Nodejs Npm PM2

+1 vote
Hi Guys, I want to create a Ansible-playbook to install and configure Nginx which should mapped with port 80, nodejs version 8, npm version 6, pm2 on aws ubuntu 16. I have one project as zip file in my local system. So once installed all then i need to move this .zip file to remote system with file path /home/ubuntu. How to achieve this? If you people have any play book please post here. It will help me. Thanks
Sep 3, 2019 in Ansible by Latchu

edited Sep 3, 2019 10,066 views

1 answer to this question.

+1 vote
Best answer

Try this out:

---
- hosts: all
become: yes
tasks:
- name: Install nginx web server
apt:
name: nginx
state: present
update_cache: yes

- name: Add Nodesource GPG key
ansible.builtin.apt_key:
url: "https://example.com/nodesource.gpg.key"
state: present

- name: Add Node.js repository
ansible.builtin.apt_repository:
repo: "deb https://example.com/node_repo {{ ansible_distribution_release }} main"
state: present
update_cache: yes

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

- name: Run npm install
ansible.builtin.command: npm install
args:
chdir: /home/ubuntu

- name: Install PM2 globally
community.general.npm:
name: pm2
global: yes

Hope it helps!!

Get your Node.js Certification today to become a certified expert.

Thank you!!

answered Sep 3, 2019 by Kalgi
• 52,340 points
Many Thanks Kalgi. I have completed that nginx, nodejs, npm, pm2. Now i would like to implement my project using ansible. I have my project as zip in my local directory. i have copied and stored then unzip in remote system using copy and unarchive module. My issue that i couldn't copy .(dot) file like hidden file. How to copy a hidden file from local system to remote system. Please let me know pa. Many Thanks for your support.
My project in remote system following directory /home/ubuntu/proj/ . When i try to start my project using pm2 start through ansible then it thrown error like below.

FAILED! => {"changed": true, "cmd": ["pm2", "start", "index.js", "--name", "Proj-US", "/home/ubuntu/proj-us/"], "delta": "0:00:00.716599", "end": "2019-09-04 05:24:18.904163", "msg": "non-zero return code", "rc": 1, "start": "2019-09-04 05:24:18.187564", "stderr": "[PM2][ERROR] script not found : /home/ubuntu/index.js\nscript not found : /home/ubuntu/index.js", "stderr_lines": ["[PM2][ERROR] script not found : /home/ubuntu/index.js", "script not found : /home/ubuntu/index.js"],

But index.file is available in /home/ubuntu/proj-us. below one is my playbook portion for pm2 start projec

- name: To Start a App
    command: pm2 start index.js --name Proj-US chdir= /home/ubuntu/proj-us
    ignore_errors: yes
    when: npm_finished.changed

Related Questions In Ansible

0 votes
1 answer

Playbook to install and start nginx on ubuntu

Write your playbook like this: --- - hosts: droplets ...READ MORE

answered Mar 15, 2019 in Ansible by Deep
12,180 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,513 views
+2 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP