DEV Community

Cover image for Sync NGINX Configs with Ansible
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Sync NGINX Configs with Ansible

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool for helping you automatically index API endpoints across all your repositories. LiveAPI helps you discover, understand, and use APIs in large tech infrastructures with ease.


You know the pain: updating NGINX config files on a remote server, reloading NGINX, making sure you didn't fat-finger anything... and repeating it across environments. Let’s automate that away with Ansible.

Project Structure

Start by initializing a role:

ansible-galaxy init roles/nginx-conf-sync
Enter fullscreen mode Exit fullscreen mode

Here’s the resulting layout:

hex-ansible
├─ README.md
├─ hosts.ini 
├─ nginx-conf-sync-playbook.yml
├─ nginx-purge-playbook.yml
└─ roles
   └─ nginx-conf-sync
      ├─ defaults/main.yml
      ├─ files/
      │  ├─ blog.dev.to
      │  ├─ dev.to
      │  └─ api.dev.to
      ├─ handlers/main.yml
      ├─ meta/main.yml
      ├─ tasks/main.yml
      ├─ tasks/create-nginx-confs.yml
      ├─ templates/
      ├─ tests/
      │  ├─ inventory
      │  └─ test.yml
      └─ vars/main.yml
Enter fullscreen mode Exit fullscreen mode

Put your actual NGINX site config files inside the files/ folder. Example file: files/blog.dev.to:

server {
    listen 80;
    server_name blog.dev.to;

    access_log  /var/log/nginx/nginx_access.log;
    error_log   /var/log/nginx/nginx_error.log  debug;

    location / {
        include proxy_params;
        proxy_pass http://127.1.22.71:9080;
    }
}
Enter fullscreen mode Exit fullscreen mode

The Inventory File

This is your hosts.ini:

[master]
master ansible_host=128.122.213.172 ansible_user=root ansible_ssh_private_key_file=~/.ssh/secret.txt
Enter fullscreen mode Exit fullscreen mode

Make sure you can SSH into the machine using that key.

defaults/main.yml

This lets you declare which config files to sync:

---
nginx_conf_domains:
  - blog.dev.to
  - dev.to
  - api.dev.to
Enter fullscreen mode Exit fullscreen mode

The Playbook

Create nginx-conf-sync-playbook.yml at the root:

- name: Sync NGINX Configs
  hosts: master
  become: yes
  roles:
    - nginx-conf-sync
Enter fullscreen mode Exit fullscreen mode

The Logic

In roles/nginx-conf-sync/tasks/main.yml:

---
- import_tasks: create-nginx-confs.yml
Enter fullscreen mode Exit fullscreen mode

And the real deal in create-nginx-confs.yml:

---
- name: Ensure NGINX config directory exists
  file:
    path: "/etc/nginx/sites-available"
    state: directory

- name: Ensure NGINX enabled directory exists
  file:
    path: "/etc/nginx/sites-enabled"
    state: directory

- name: Create .htpasswd file for basic auth
  copy:
    dest: /etc/nginx/.htpasswd
    content: |
      hexmos:$apr1$v5j1xXVg$i6XGv4RoQGT2FwqpKsJcs0
    owner: root
    group: root
    mode: "0640"

- name: Sync nginx conf files
  copy:
    src: "{{ item }}"
    dest: "/etc/nginx/sites-available/{{ item }}"
  with_items: "{{ nginx_conf_domains }}"

- name: Create symlinks in sites-enabled
  file:
    src: "/etc/nginx/sites-available/{{ item }}"
    dest: "/etc/nginx/sites-enabled/{{ item }}"
    state: link
    force: yes
  with_items: "{{ nginx_conf_domains }}"

- name: Reload nginx
  service:
    name: nginx
    state: reloaded
Enter fullscreen mode Exit fullscreen mode

Run It

ansible-playbook -i hosts.ini nginx-conf-sync-playbook.yml
Enter fullscreen mode Exit fullscreen mode

Boom. Your NGINX configs are in place, symlinked, and NGINX is reloaded.

TL;DR

Ansible makes syncing NGINX config files effortless and repeatable. Dump your config files in the role, declare them in defaults, run the playbook, and you’re good to go.


LiveAPI helps you get all your backend APIs documented in a few minutes

With LiveAPI, you can quickly generate interactive API documentation that allows users to search and execute APIs directly from the browser.

Image description

If you’re tired of manually creating docs for your APIs, this tool might just make your life easier.

Top comments (2)

Collapse
 
dotallio profile image
Dotallio

This is super clear, I wish I had this when I started automating NGINX updates! Do you think you could hook in LiveAPI to auto-test endpoints right after Ansible runs?

Collapse
 
lovestaco profile image
Athreya aka Maneshwar

Hey bot!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.