-
Notifications
You must be signed in to change notification settings - Fork 3
/
.playbook.yml
161 lines (142 loc) · 4.84 KB
/
.playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Create a compute instance on OpenStack using Ansible
---
- name: Launch an OpenStack compute instance
hosts: localhost
vars_files:
- vars/main.yml
tasks:
# Create key file with 0600 permissions
# http://docs.ansible.com/ansible/copy_module.html
- name: Add private key
copy:
content: "{{ id_rsa_base64| b64decode }}"
dest: "{{ user_home }}/{{ rsa_filename }}"
mode: 0600
# Generate a public key from the private key
# http://docs.ansible.com/ansible/shell_module.html
- name: Generate public key
shell: "ssh-keygen -P '' -y -f {{ user_home }}/{{ rsa_filename }} > {{ user_home }}/{{ rsa_filename }}.pub"
# Add the public key to OpenStack
# http://docs.ansible.com/ansible/os_keypair_module.html
- name: Add keypair
os_keypair:
state: present
name: "{{ os_key_name }}"
public_key_file: "{{ user_home }}/{{ rsa_filename }}.pub"
auth:
auth_url: "{{ os_auth_url }}"
username: "{{ os_username }}"
password: "{{ os_password }}"
project_name: "{{ os_proj_name }}"
register: "create_key"
# Launch an OpenStack compute instance
# http://docs.ansible.com/ansible/os_server_module.html
- name: Launch the compute instance
os_server:
state: present
auth:
auth_url: "{{ os_auth_url }}"
username: "{{ os_username }}"
password: "{{ os_password }}"
project_name: "{{ os_proj_name }}"
name: "{{ os_vm_name }}"
flavor_ram: "{{ os_vm_ram }}"
image: "{{ os_vm_image }}"
key_name: "{{ os_key_name }}"
register: "create_server"
# Add system to host_group
# http://docs.ansible.com/ansible/add_host_module.html
- name: Add system as host
add_host:
hostname: "OpenStack Instance ({{ os_vm_image }})"
ansible_host: "{{ create_server.openstack.public_v4 }}"
ansible_port: "{{ ssh_port }}"
groups: "{{ host_group }}"
ansible_ssh_private_key_file: "{{ user_home }}/{{ rsa_filename }}"
# Wait for port to be opened
# http://docs.ansible.com/ansible/wait_for_module.html
- name: Wait for SSH to be active
local_action: wait_for port="{{ ssh_port }}" host="{{ create_server.openstack.public_v4 }}" search_regex=OpenSSH
# --------------------------------------------------------------------------------
# Install Node.JS on the created instance
- name: Configure instance to run a Node.JS app
hosts: new_hosts #must match groupname in "add_host" above
user: ubuntu
vars:
app_folder: "bluemix-node-sample"
tasks:
# Update apt list
# http://docs.ansible.com/ansible/apt_module.html
- name: Update apt list
apt:
update_cache: yes
become: true
# Install APT packages
- name: Install packages
apt:
name: "{{ item }}"
become: true
with_items:
- git
- build-essential
- python-dev
- python-pip
# Install PIP packages
# http://docs.ansible.com/ansible/pip_module.html
- name: pip install
pip:
name: "{{ item }}"
become: true
with_items:
- urllib3
- pyopenssl
- ndg-httpsclient
- pyasn1
# Add key for Node.JS
# http://docs.ansible.com/ansible/apt_key_module.html
- name: Add apt key for nodesource
apt_key: url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key
become: true
# Add the Node.JS repo
# http://docs.ansible.com/ansible/apt_repository_module.html
- name: add repo for nodesource
apt_repository:
repo: 'deb https://deb.nodesource.com/node_6.x {{ ansible_distribution_release }} main'
update_cache: true
become: true
# Instal Node.JS
- name: Install packages
apt:
name: nodejs
become: true
register: output
# Clone the Node.JS project
# http://docs.ansible.com/ansible/git_module.html
- name: clone repo
git:
repo: https://github.com/IBMCloudDevOps/bluemix-node-sample
accept_hostkey: yes
force: yes
dest: "{{ ansible_env.HOME }}/{{ app_folder }}"
# Install npm packages
# http://docs.ansible.com/ansible/command_module.html
- name: npm install
command: "npm install"
args:
chdir: "{{ ansible_env.HOME }}/{{ app_folder }}"
# Run node server as cronjob
# http://docs.ansible.com/ansible/cron_module.html
- name: Start server on boot
cron:
name: "Run node app"
special_time: reboot
job: "/usr/bin/nodejs {{ ansible_env.HOME }}/{{ app_folder }}/app.js"
# Reboot the server
- name: Restart server
command: /sbin/shutdown -r now
async: 0
poll: 0
ignore_errors: true
become: true
- name: Print app URL
debug: msg="Node app available at http://{{ ansible_host }}:3000"