main.yml 8.88 KB
Newer Older
1 2
# requires:
#   - common/tasks/main.yml
John Jarvis committed
3
---
4

5
- name: create nginx app dirs
6 7 8 9 10 11 12 13 14
  file: >
    path="{{ item }}"
    state=directory
    owner="{{ nginx_user }}"
    group="{{ common_web_group }}"
  with_items:
    - "{{ nginx_app_dir }}"
    - "{{ nginx_sites_available_dir }}"
    - "{{ nginx_sites_enabled_dir }}"
John Jarvis committed
15
    - "{{ nginx_conf_dir }}"
John Jarvis committed
16
  notify: restart nginx
17 18 19
  tags:
    - install
    - install:configuration
20

21
- name: create nginx data dirs
22 23 24 25 26 27 28 29
  file: >
    path="{{ item }}"
    state=directory
    owner="{{ common_web_user }}"
    group="{{ nginx_user }}"
  with_items:
    - "{{ nginx_data_dir }}"
    - "{{ nginx_log_dir }}"
John Jarvis committed
30
    - "{{ nginx_server_static_dir }}"
John Jarvis committed
31
  notify: restart nginx
32 33 34
  tags:
    - install
    - install:configuration
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
- name: Install needed packages
  apt: pkg={{ item }} state=present
  notify: restart nginx
  with_items: nginx_debian_pkgs
  tags:
    - install
    - install:system-requirements

- name: Add apt key
  apt_key:
    url: http://nginx.org/keys/nginx_signing.key
    state: present
  notify: restart nginx
  tags:
    - install
    - install:system-requirements

- name: Add nginx repository
  apt_repository:
55
    repo: "{{ NGINX_APT_REPO }}"
56 57 58 59 60 61
    state: present
  notify: restart nginx
  tags:
    - install
    - install:system-requirements

62 63 64 65 66 67 68 69 70 71 72 73 74 75
# REMOVE THIS AFTER LATEST NGINX HAS BEEN DEPLOYED EVERYWHERE
# New package does not identify conflicts properly.
# "nginx-common" only appears as requirement for ubuntu-distributed package, thus
# removing it will remove all nginx packages installed from Ubuntu's repo.
# This is only required if nginx was previously installed from Ubuntu's repo
# and you're switching to Nginx's PPA
- name: Remove old nginx packages
  apt:
    name: nginx-common
    state: absent
  tags:
    - install
    - install:system-requirements

76 77 78 79 80
- name: Install the nginx package
  apt:
    name: nginx
    state: latest
    update_cache: yes
John Jarvis committed
81
  notify: restart nginx
82 83 84
  tags:
    - install
    - install:system-requirements
Joe Blaylock committed
85

86 87 88 89 90
- name: Remove the default site
  file: >
    path=/etc/nginx/sites-enabled/default
    state=absent
  notify: reload nginx
91 92 93
  tags:
    - install
    - install:configuration
94

95
- name: Server configuration file
96
  template: >
97
    src=etc/nginx/nginx.conf.j2 dest=/etc/nginx/nginx.conf
98
    owner=root group={{ common_web_user }} mode=0644
99
  notify: restart nginx
100 101 102
  tags:
    - install
    - install:configuration
103

104
- name: Creating common nginx configuration
105
  template: >
106 107
    src=edx/app/nginx/sites-available/edx-release.j2
    dest={{ nginx_sites_available_dir }}/edx-release
108
    owner=root group=root mode=0600
John Jarvis committed
109
  notify: reload nginx
110 111 112
  tags:
    - install
    - install:configuration
113

114 115
- name: Create robot rules
  template: >
116
    src=edx/app/nginx/robots.txt.j2 dest={{ nginx_app_dir }}/robots.txt
117 118 119
    owner=root group={{ common_web_user }} mode=0644
  notify: reload nginx
  when: NGINX_ROBOT_RULES|length > 0
120 121 122
  tags:
    - install
    - install:configuration
123

124
- name: Creating link for common nginx configuration
125
  file: >
126 127
    src={{ nginx_sites_available_dir }}/edx-release
    dest={{ nginx_sites_enabled_dir }}/edx-release
128
    state=link owner=root group=root
John Jarvis committed
129
  notify: reload nginx
130 131 132
  tags:
    - install
    - install:configuration
133

134
- name: Copying nginx configs for {{ nginx_sites }}
135
  template: >
136
    src={{ nginx_template_dir }}/{{ item }}.j2
137
    dest={{ nginx_sites_available_dir }}/{{ item }}
138
    owner=root group={{ common_web_user }} mode=0640
John Jarvis committed
139
  notify: reload nginx
140
  with_items: nginx_sites
141 142 143
  tags:
    - install
    - install:configuration
144

145
- name: Creating nginx config links for {{ nginx_sites }}
146
  file: >
147
    src={{ nginx_sites_available_dir }}/{{ item  }}
148 149
    dest={{ nginx_sites_enabled_dir }}/{{ item }}
    state=link owner=root group=root
John Jarvis committed
150
  notify: reload nginx
151
  with_items: nginx_sites
152 153 154
  tags:
    - install
    - install:configuration
Joe Blaylock committed
155

John Jarvis committed
156 157 158 159 160 161 162
- name: Copying nginx extra configs
  template: >
    src={{ item }}
    dest={{ nginx_sites_available_dir }}/{{ item|basename|replace(".j2", "") }}
    owner=root group={{ common_web_user }} mode=0640
  notify: reload nginx
  with_items: nginx_extra_sites
163 164 165
  tags:
    - install
    - install:configuration
John Jarvis committed
166 167 168 169 170 171 172 173

- name: Creating links for nginx extra configs
  file: >
    src={{ nginx_sites_available_dir }}/{{ item|basename|replace(".j2", "")  }}
    dest={{ nginx_sites_enabled_dir }}/{{ item|basename|replace(".j2", "") }}
    state=link owner=root group=root
  notify: reload nginx
  with_items: nginx_extra_sites
174 175 176
  tags:
    - install
    - install:configuration
John Jarvis committed
177 178 179 180 181 182 183 184

- name: Copying custom nginx config
  template: >
    src={{ item }}
    dest={{ nginx_conf_dir }}/{{ item|basename|replace(".j2", "") }}
    owner=root group={{ common_web_user }} mode=0640
  notify: reload nginx
  with_items: nginx_extra_configs
185 186 187
  tags:
    - install
    - install:configuration
John Jarvis committed
188

189 190 191 192 193 194 195
- name: Copying nginx redirect configs for {{ nginx_redirects }}
  template: >
    src={{ nginx_template_dir }}/nginx_redirect.j2
    dest={{ nginx_sites_available_dir }}/{{ item.key }}
    owner=root group={{ common_web_user }} mode=0640
  notify: reload nginx
  with_dict: nginx_redirects
196 197 198
  tags:
    - install
    - install:configuration
199 200 201 202 203 204 205 206

- name: Creating nginx redirect links for {{ nginx_redirects }}
  file: >
    src={{ nginx_sites_available_dir }}/{{ item.key  }}
    dest={{ nginx_sites_enabled_dir }}/{{ item.key }}
    state=link owner=root group=root
  notify: reload nginx
  with_dict: nginx_redirects
207 208 209
  tags:
    - install
    - install:configuration
210

John Jarvis committed
211 212 213 214 215 216 217 218 219
  # These are static pages that can be used
  # for nginx rate limiting, 500 errors, etc.

- name: Create NGINX server templates
  template: >
    src=edx/var/nginx/server-static/server-template.j2
    dest={{ nginx_server_static_dir }}/{{ item.file }}
    owner=root group={{ common_web_user }} mode=0640
  with_items: NGINX_SERVER_HTML_FILES
220 221 222
  tags:
    - install
    - install:configuration
John Jarvis committed
223

224
- name: Write out htpasswd file
225
  htpasswd: >
226 227 228
    name={{ item.name }}
    password={{ item.password }}
    state={{ item.state  }}
229
    path={{ nginx_htpasswd_file }}
230
  when: NGINX_CREATE_HTPASSWD_FILE
231 232 233
  tags:
    - install
    - install:configuration
Bilal committed
234
  with_items: NGINX_USERS
Joe Blaylock committed
235

236
- name: Create nginx log file location (just in case)
237
  file: >
238 239
    path={{ nginx_log_dir}} state=directory
    owner={{ common_web_user }} group={{ common_web_user }}
240 241 242
  tags:
    - install
    - install:configuration
Joe Blaylock committed
243

244 245 246
# Check to see if the ssl cert/key exists before copying.
# This extra check is done to prevent failures when
# ansible-playbook is run locally
247 248 249
- local_action:
    module: stat
    path: "{{ NGINX_SSL_CERTIFICATE }}"
John Jarvis committed
250
  sudo: False
251
  register: ssl_cert
252 253 254
  tags:
    - install
    - install:configuration
255

256 257 258
- local_action:
    module: stat
    path: "{{ NGINX_SSL_KEY }}"
John Jarvis committed
259
  sudo: False
260
  register: ssl_key
261 262 263
  tags:
    - install
    - install:configuration
264

265
- name: copy ssl cert
John Jarvis committed
266 267
  copy: >
    src={{ NGINX_SSL_CERTIFICATE }}
268
    dest=/etc/ssl/certs/
John Jarvis committed
269
    owner=root group=root mode=0644
270
  when: ssl_cert.stat.exists and NGINX_ENABLE_SSL and NGINX_SSL_CERTIFICATE != 'ssl-cert-snakeoil.pem'
271 272 273
  tags:
    - install
    - install:configuration
John Jarvis committed
274

275
- name: copy ssl key
John Jarvis committed
276 277
  copy: >
    src={{ NGINX_SSL_KEY }}
278
    dest=/etc/ssl/private/
279
    owner=root group=root mode=0640
280
  when: ssl_key.stat.exists and NGINX_ENABLE_SSL and NGINX_SSL_KEY != 'ssl-cert-snakeoil.key'
281 282 283
  tags:
    - install
    - install:configuration
284 285 286 287 288

# removing default link
- name: Removing default nginx config and restart (enabled)
  file: path={{ nginx_sites_enabled_dir }}/default state=absent
  notify: reload nginx
289 290 291
  tags:
    - install
    - install:configuration
John Jarvis committed
292

293 294 295 296
# Note that nginx logs to /var/log until it reads its configuration, so /etc/logrotate.d/nginx is still good

- name: Set up nginx access log rotation
  template: >
297 298
    src=etc/logrotate.d/edx_logrotate_nginx_access.j2
    dest=/etc/logrotate.d/nginx-access
299
    owner=root group=root mode=644
300 301 302
  tags:
    - install
    - install:configuration
303

304
- name: Set up nginx access log rotation
305
  template: >
306 307
    src=etc/logrotate.d/edx_logrotate_nginx_error.j2
    dest=/etc/logrotate.d/nginx-error
308
    owner=root group=root mode=644
309 310 311
  tags:
    - install
    - install:configuration
Joe Blaylock committed
312

313 314 315 316 317 318 319 320 321 322 323
# nginx is started during package installation, before any of the configuration files are in place.
# The handler that reloads the configuration would be run only at the very end of the playbook, so
# none of the local services would be available in the meantime, e.g. causing certs to error out
# since it can't reach xqueue on the its nginx port.  For this reason, we flush the handlers here
# to ensure the nginx configuration is reloaded when necessary.
- name: Restart or reload nginx if necessary
  meta: flush_handlers
  tags:
    - install
    - install:configuration

324 325 326
# If tasks that notify restart nginx don't change the state of the remote system
# their corresponding notifications don't get run.  If nginx has been stopped for
# any reason, this will ensure that it is started up again.
327
- name: make sure nginx has started
328
  service: name=nginx state=started
329 330 331
  tags:
    - manage
    - manage:start