Skip to content

Instantly share code, notes, and snippets.

@oaleynik
Last active August 19, 2019 21:48
Show Gist options
  • Save oaleynik/c35ff3f498eb16232a42 to your computer and use it in GitHub Desktop.
Save oaleynik/c35ff3f498eb16232a42 to your computer and use it in GitHub Desktop.
Run N instances on Amazon EC2, install nvm, node, npm, PM2 and make sure PM2 will be automatically started in the case of the instance reboot
- name: Provision infrastructure
hosts: localhost
gather_facts: False
vars:
keypair: mykeypair
instance_type: m4.large
region: us-west-2
image: ami-f0091d91
tasks:
- name: Launch instances
ec2:
key_name: "{{ keypair }}"
groups: ['SSH']
instance_type: "{{ instance_type }}"
region: "{{ region }}"
image: "{{ image }}"
wait: yes
exact_count: 3
count_tag:
Name: MY-CLUSTER
instance_tags:
Name: MY-CLUSTER
register: ec2
- name: Add all tagged instances to host group
add_host: name={{ item.public_ip }} groups=tagged
with_items: ec2.tagged_instances
- name: Wait for SSH to come up
wait_for: host={{ item.public_dns_name }} port=22 delay=30 timeout=320 state=started
with_items: ec2.instances
- name: Provision instances
hosts: tagged
gather_facts: True
user: ec2-user
vars:
nvm_user: ec2-user
nvm_user_home: /home/ec2-user
nvm_user_profile: /home/ec2-user/.bash_profile
nvm_dir: /home/ec2-user/.nvm
nvm_version: v0.30.2
nvm_node_version: 5.4.1
tasks:
- name: Update yum packages
yum: name=* state=latest
become: true
become_method: sudo
- name: Install development tools and libraries
yum: name="@{{ item }}" state=present
become: true
become_method: sudo
with_items:
- Development Tools
- Development Libraries
- name: Check if nvm installed
stat: path="{{ nvm_dir }}"
register: nvm_path
changed_when: False
- name: Create nvm folder
when: not nvm_path.stat.exists
become: yes
become_method: sudo
file: path="{{ nvm_dir }}" owner="{{ nvm_user }}" group="{{ nvm_user }}" mode=755 state=directory
- name: Install nvm
when: not nvm_path.stat.exists
git: repo=https://github.com/creationix/nvm.git dest="{{ nvm_dir }}" version="{{ nvm_version }}"
- name: Source nvm in {{ nvm_user_profile }}
lineinfile: dest="{{ nvm_user_profile }}" line='[ -s "{{ nvm_dir }}/nvm.sh" ] && . "{{ nvm_dir }}/nvm.sh"'
- name: Check installed node version
command: bash -lc 'nvm ls'
register: nvm_install_result
changed_when: False
ignore_errors: True
- name: Install nodejs using nvm
when: nvm_install_result|failed or nvm_install_result.stdout.find('{{ nvm_node_version }}') == -1
command: bash -lc 'nvm install "{{ nvm_node_version }}"'
- name: Check the default nodejs version
command: bash -lc 'nvm ls | grep -e "default -> {{ nvm_node_version }}"'
register: nvm_check_default
changed_when: False
ignore_errors: True
- name: Set default node version
when: nvm_check_default|failed
command: bash -lc 'nvm alias default "{{ nvm_node_version }}"'
tags: nvm
- name: Install PM2
npm: name=pm2 global=yes state=present executable="{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/npm"
tags: npm
- name: Ensure PM2 started
command: bash -lc 'pm2 ping &>/dev/null'
- name: Check PM2 startup script
stat: path=/var/lock/subsys/pm2-init.sh
register: pm2_startup_lock
changed_when: False
- name: Get startup initialization script for PM2
when: not pm2_startup_lock.stat.exists
command: bash -lc 'pm2 startup "{{ platform }}" | grep -e "sudo su -c" | sed -e "s/^ *//" | sed -E "s/sudo su -c \"(.*)\"/\1/"'
register: pm2_startup
- name: Ensure PM2 will start on reboot
when: not pm2_startup_lock.stat.exists
become: yes
become_method: sudo
command: "{{pm2_startup.stdout}}"
register: pm2_ensure_startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment