DEV Community

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

Posted on • Edited on

Sync NGINX Configs with Ansible

Hello, I'm Maneshwar. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.


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.


A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: ⭐ Star it on GitHub: Let’s make it even better together.

git-lrc
*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a habit, ship better code. Regular review → fewer bugs → more robust code → better results in your team.
  • 🔗 Why git? Git is universal. Every editor, every IDE, every AI…




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.