ansible_playbooks/nginx_pg_subdomain.yml

65 lines
1.7 KiB
YAML
Raw Normal View History

2023-08-03 15:57:00 -04:00
---
- name: Configure Nginx for Subdomain
hosts: servers
become: true
tasks:
- name: Create Nginx sites-available directory if it doesn't exist
file:
path: /etc/nginx/sites-available
state: directory
mode: '0755'
become: yes
- name: Create Nginx sites-enabled directory if it doesn't exist
file:
path: /etc/nginx/sites-enabled
state: directory
mode: '0755'
become: yes
- name: Create empty pg.usmidsap02 file if it doesn't exist
file:
path: /etc/nginx/sites-available/pg.usmidsap02
state: touch
mode: '0644'
become: yes
- name: Create Nginx configuration for the subdomain
become: yes
blockinfile:
path: /etc/nginx/sites-available/pg.usmidsap02
block: |
server {
listen 5432;
server_name pg.usmidsap02;
location / {
proxy_pass http://127.0.0.1:5432;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
marker: "# {mark} ANSIBLE MANAGED BLOCK - pg.usmidsap02"
- name: Create a symbolic link to enable the site
become: yes
file:
src: /etc/nginx/sites-available/pg.usmidsap02
dest: /etc/nginx/sites-enabled/pg.usmidsap02
state: link
- name: Check Nginx configuration
become: yes
command: nginx -t
register: nginx_test
ignore_errors: true
- name: Reload Nginx if configuration is valid
become: yes
systemd:
name: nginx
state: reloaded
when: nginx_test.rc == 0