DEV Community

Cover image for Making Ansible Snappy, Quiet, and Friendly: A Dev’s Guide to `ansible.cfg`
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Making Ansible Snappy, Quiet, and Friendly: A Dev’s Guide to `ansible.cfg`

Making Ansible Snappy, Quiet, and Friendly: A Dev’s Guide to ansible.cfg

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.


Ansible is great—until it floods your terminal with unreadable JSON, times out on half the hosts, and throws a hissy fit about SSH keys.

If you’re tired of that, this guide is for you.

Let’s walk through a practical ansible.cfg setup, explain what each line does, and sprinkle in some useful CLI tips.

Why ansible.cfg Even Matters

Before you start writing playbooks, tweak this config. It sets global behaviors like:

  • Inventory location
  • Logging style
  • How many hosts you run in parallel
  • Whether you want to deal with SSH key prompts ever again (spoiler: you don’t)

The Config File (ansible.cfg)

Drop this in your project root or ~/.ansible.cfg.

[defaults]
# Clean, YAML-formatted output in your terminal (no JSON dump hell)
stdout_callback = yaml
stderr_callback = yaml

# Path to your inventory file
inventory = hosts.ini

# Skip SSH key confirmation prompts
host_key_checking = False

# SSH timeout for unresponsive hosts
timeout = 30

# How many hosts to hit in parallel (default is 5)
forks = 10

# Speed up execution by skipping sudo temp file creation
pipelining = True

[ssh_connection]
# Reuse SSH sessions across tasks for faster playbook runs
pipelining = True
Enter fullscreen mode Exit fullscreen mode

Notes:

  • stdout_callback = yaml: Makes ansible-playbook output readable. No more chasing curly braces and commas.
  • pipelining = True: Reduces overhead by skipping sudo temp files and reusing SSH connections.
  • host_key_checking = False: Don’t worry, it’s fine if you’re on a controlled private infra. But maybe don’t do this in prod unless you're sure.
  • forks = 10: Increase parallelism if you're managing lots of hosts. Don’t go crazy unless your target machines and local machine can handle it.

Inventory Example (hosts.ini)

Here’s a simple static inventory format:

[web]
192.168.1.10

[db]
192.168.1.20

[master-new]
34.71.194.14 ansible_user=root
Enter fullscreen mode Exit fullscreen mode

Useful Ansible Commands

Check Host Info (with Full Debug Output)

ansible -i hosts.ini master-new -m setup -u root -vvvv
Enter fullscreen mode Exit fullscreen mode

Use this to fetch system info like IP, memory, OS, etc. -vvvv gives you full debugging output. Very handy when debugging SSH issues.

Using Roles and Collections

Generate a Role Locally

ansible-galaxy init roles/ghost --offline
Enter fullscreen mode Exit fullscreen mode

This scaffolds a new Ansible role inside roles/ghost.

Install Collections and Roles via requirements.yml

ansible-galaxy install -r requirements.yml
Enter fullscreen mode Exit fullscreen mode

Example requirements.yml:

roles:
  - src: geerlingguy.nginx
    version: 3.1.0

collections:
  - name: community.general
    version: ">=4.0.0"
Enter fullscreen mode Exit fullscreen mode

Pro Tips

  • Use --check to dry-run playbooks:
  ansible-playbook playbook.yml --check
Enter fullscreen mode Exit fullscreen mode
  • Want faster execution? Add this to your playbook:
  gather_facts: false
Enter fullscreen mode Exit fullscreen mode

Unless you need host facts, skip them to save time.

  • Combine your inventory and playbook runs:
  ansible-playbook -i hosts.ini site.yml
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

With a tuned ansible.cfg, static inventory, and solid role structure, Ansible becomes way more bearable. You’ll avoid half the pain points most devs hit when trying to automate infra.

Got a cluster to set up? Some packages to bootstrap? Use this as your launchpad and keep your ansible.cfg opinionated and tight.


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

With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

LiveAPI Demo

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.

Top comments (0)