DEV Community

Cover image for Docker + Ansible: The Clean Way to Automate Infrastructure
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on • Edited on

Docker + Ansible: The Clean Way to Automate Infrastructure

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.

_Hi there! I’m Maneshwar. Right now, I’m building

If you've ever felt overwhelmed by managing multiple servers, configurations, or deployment steps, Ansible is your friend.

This blog post walks through a real, modular Ansible setup, ideal for developers and DevOps engineers looking to scale smart automation without hacking things together.

We'll also dive into using external roles, tags, loops, handlers, and testing, all tied together in a clean folder structure.

The Folder Structure (Project Baseline)

ansible/
├─ README.md
├─ ansible.cfg
├─ go-install-playbook.yml
├─ hosts.ini
├─ install_ansible.sh
├─ install-server-tools.yml
├─ requirements.yml
└─ roles/
   └─ server-tools/
      ├─ README.md
      ├─ defaults/main.yml
      ├─ files/
      ├─ handlers/main.yml
      ├─ meta/main.yml
      ├─ tasks/
      │  ├─ install_docker.yml
      │  ├─ main.yml
      │  └─ wander.yml
      ├─ templates/
      ├─ tests/
      │  ├─ inventory
      │  └─ test.yml
      └─ vars/main.yml
Enter fullscreen mode Exit fullscreen mode

This folder setup follows Ansible best practices. Each role is modular, reusable, and testable.

Conditional Installations (Cross-Distro Support)

Use ansible_facts to install OS-specific packages:

- name: Install web server based on OS family
  package:
    name: "{{ 'httpd' if ansible_facts['os_family'] == 'RedHat' else 'apache2' }}"
    state: present
Enter fullscreen mode Exit fullscreen mode

This ensures your playbook works across different Linux distributions.

Looping Through Tasks

Instead of repeating tasks for each user:

- name: Create users
  user:
    name: "{{ item.name }}"
    groups: "{{ item.groups }}"
  loop: "{{ user_list }}"
Enter fullscreen mode Exit fullscreen mode

Where user_list could be:

user_list:
  - { name: "alice", groups: "docker" }
  - { name: "bob", groups: "sudo" }
Enter fullscreen mode Exit fullscreen mode

Handlers in Action

Handlers are only triggered when notified, perfect for service restarts:

- name: Update NGINX config
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
  notify: Restart NGINX
Enter fullscreen mode Exit fullscreen mode

In handlers/main.yml:

- name: Restart NGINX
  service:
    name: nginx
    state: restarted
Enter fullscreen mode Exit fullscreen mode

Tagging for Targeted Execution

Apply tags for surgical execution:

- name: Sync NGINX config
  copy:
    src: nginx.conf
    dest: /etc/nginx/nginx.conf
  tags:
    - nginx
    - config
Enter fullscreen mode Exit fullscreen mode

Run only config-related tasks:

ansible-playbook install-server-tools.yml --tags config
Enter fullscreen mode Exit fullscreen mode

Role: server-tools Example

A role that installs CLI tools like Docker and Wander:

tasks/main.yml

---
- import_tasks: install_docker.yml
- import_tasks: wander.yml
Enter fullscreen mode Exit fullscreen mode

tasks/wander.yml

- name: Install Wander binary
  get_url:
    url: https://github.com/robinovitch61/wander/releases/latest/download/wander_Linux_x86_64
    dest: /usr/local/bin/wander
    mode: '0755'
Enter fullscreen mode Exit fullscreen mode

Wrapping It Up

Once everything is wired together:

ansible-playbook -i hosts.ini install-server-tools.yml -v
Enter fullscreen mode Exit fullscreen mode

This sets up Docker, your dev tools, certbot for HTTPS, cron, and a robust, reload-safe nginx config.

Stay clean, stay modular. Happy automation!


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

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

Stay clean, stay modular. Happy automation!

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 (1)

Collapse
 
blenderman profile image
BBM

Super helpful breakdown—love the modular approach!