main.yml 7.26 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
- name: Install nginx packages
37
  apt: pkg={{','.join(nginx_debian_pkgs)}} state=present
John Jarvis committed
38
  notify: restart nginx
39 40 41
  tags:
    - install
    - install:system-requirements
Joe Blaylock committed
42

43 44 45 46 47
- name: Remove the default site
  file: >
    path=/etc/nginx/sites-enabled/default
    state=absent
  notify: reload nginx
48 49 50
  tags:
    - install
    - install:configuration
51

52
- name: Server configuration file
53
  template: >
54
    src=etc/nginx/nginx.conf.j2 dest=/etc/nginx/nginx.conf
55
    owner=root group={{ common_web_user }} mode=0644
56
  notify: restart nginx
57 58 59
  tags:
    - install
    - install:configuration
60

61
- name: Creating common nginx configuration
62
  template: >
63 64
    src=edx/app/nginx/sites-available/edx-release.j2
    dest={{ nginx_sites_available_dir }}/edx-release
65
    owner=root group=root mode=0600
John Jarvis committed
66
  notify: reload nginx
67 68 69
  tags:
    - install
    - install:configuration
70

71 72
- name: Create robot rules
  template: >
73
    src=edx/app/nginx/robots.txt.j2 dest={{ nginx_app_dir }}/robots.txt
74 75 76
    owner=root group={{ common_web_user }} mode=0644
  notify: reload nginx
  when: NGINX_ROBOT_RULES|length > 0
77 78 79
  tags:
    - install
    - install:configuration
80

81
- name: Creating link for common nginx configuration
82
  file: >
83 84
    src={{ nginx_sites_available_dir }}/edx-release
    dest={{ nginx_sites_enabled_dir }}/edx-release
85
    state=link owner=root group=root
John Jarvis committed
86
  notify: reload nginx
87 88 89
  tags:
    - install
    - install:configuration
90

91
- name: Copying nginx configs for {{ nginx_sites }}
92
  template: >
93
    src={{ nginx_template_dir }}/{{ item }}.j2
94
    dest={{ nginx_sites_available_dir }}/{{ item }}
95
    owner=root group={{ common_web_user }} mode=0640
John Jarvis committed
96
  notify: reload nginx
97
  with_items: nginx_sites
98 99 100
  tags:
    - install
    - install:configuration
101

102
- name: Creating nginx config links for {{ nginx_sites }}
103
  file: >
104
    src={{ nginx_sites_available_dir }}/{{ item  }}
105 106
    dest={{ nginx_sites_enabled_dir }}/{{ item }}
    state=link owner=root group=root
John Jarvis committed
107
  notify: reload nginx
108
  with_items: nginx_sites
109 110 111
  tags:
    - install
    - install:configuration
Joe Blaylock committed
112

John Jarvis committed
113 114 115 116 117 118 119
- 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
120 121 122
  tags:
    - install
    - install:configuration
John Jarvis committed
123 124 125 126 127 128 129 130

- 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
131 132 133
  tags:
    - install
    - install:configuration
John Jarvis committed
134 135 136 137 138 139 140 141

- 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
142 143 144
  tags:
    - install
    - install:configuration
John Jarvis committed
145

146 147 148 149 150 151 152
- 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
153 154 155
  tags:
    - install
    - install:configuration
156 157 158 159 160 161 162 163

- 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
164 165 166
  tags:
    - install
    - install:configuration
167

John Jarvis committed
168 169 170 171 172 173 174 175 176
  # 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
177 178 179
  tags:
    - install
    - install:configuration
John Jarvis committed
180

181
- name: Write out htpasswd file
182
  htpasswd: >
183 184
    name={{ COMMON_HTPASSWD_USER }}
    password={{ COMMON_HTPASSWD_PASS }}
185
    path={{ nginx_htpasswd_file }}
Fred Smith committed
186
  when: COMMON_ENABLE_BASIC_AUTH
187 188 189
  tags:
    - install
    - install:configuration
Joe Blaylock committed
190

191
- name: Create nginx log file location (just in case)
192
  file: >
193 194
    path={{ nginx_log_dir}} state=directory
    owner={{ common_web_user }} group={{ common_web_user }}
195 196 197
  tags:
    - install
    - install:configuration
Joe Blaylock committed
198

199 200 201
# 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
202 203 204
- local_action:
    module: stat
    path: "{{ NGINX_SSL_CERTIFICATE }}"
John Jarvis committed
205
  sudo: False
206
  register: ssl_cert
207 208 209
  tags:
    - install
    - install:configuration
210

211 212 213
- local_action:
    module: stat
    path: "{{ NGINX_SSL_KEY }}"
John Jarvis committed
214
  sudo: False
215
  register: ssl_key
216 217 218
  tags:
    - install
    - install:configuration
219

220
- name: copy ssl cert
John Jarvis committed
221 222
  copy: >
    src={{ NGINX_SSL_CERTIFICATE }}
223
    dest=/etc/ssl/certs/
John Jarvis committed
224
    owner=root group=root mode=0644
225
  when: ssl_cert.stat.exists and NGINX_ENABLE_SSL and NGINX_SSL_CERTIFICATE != 'ssl-cert-snakeoil.pem'
226 227 228
  tags:
    - install
    - install:configuration
John Jarvis committed
229

230
- name: copy ssl key
John Jarvis committed
231 232
  copy: >
    src={{ NGINX_SSL_KEY }}
233
    dest=/etc/ssl/private/
234
    owner=root group=root mode=0640
235
  when: ssl_key.stat.exists and NGINX_ENABLE_SSL and NGINX_SSL_KEY != 'ssl-cert-snakeoil.key'
236 237 238
  tags:
    - install
    - install:configuration
239 240 241 242 243

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

248 249 250 251
# 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: >
252 253
    src=etc/logrotate.d/edx_logrotate_nginx_access.j2
    dest=/etc/logrotate.d/nginx-access
254
    owner=root group=root mode=644
255 256 257
  tags:
    - install
    - install:configuration
258

259
- name: Set up nginx access log rotation
260
  template: >
261 262
    src=etc/logrotate.d/edx_logrotate_nginx_error.j2
    dest=/etc/logrotate.d/nginx-error
263
    owner=root group=root mode=644
264 265 266
  tags:
    - install
    - install:configuration
Joe Blaylock committed
267

268 269 270
# 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.
271
- name: make sure nginx has started
272
  service: name=nginx state=started
273 274 275
  tags:
    - manage
    - manage:start
276 277
    - devstack
    - devstack:manage