AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Output to File: Save Playbook Results and Logs (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: web-servers

How to save Ansible output to a file. Log playbook results, register output, use callback plugins, and redirect stdout. Complete guide with examples.

Ansible Output to File: Save Playbook Results and Logs (Complete Guide)

There are several ways to capture Ansible output — from simple shell redirection to callback plugins and the register directive. Choose the right method based on whether you need task-level data or full playbook logs.

Method 1: Shell Redirection

Method 2: Ansible Log File (ansible.cfg)

This logs all Ansible output automatically without changing playbooks.

Method 3: Register and Save with copy

Save Structured Data

Method 4: Callback Plugins

JSON Log Callback

Custom Log Callback

Per-Run JSON Output

Method 5: Write to File During Playbook

Aggregate Results

Method 6: Fetch Remote Files

Combine Multiple Methods

FAQ

How do I save Ansible playbook output to a file?

The simplest way is shell redirection: ansible-playbook site.yml > output.log 2>&1. For automatic logging, set log_path in ansible.cfg. For task-specific output, use register and ansible.builtin.copy.

How do I log all Ansible runs automatically?

Set log_path = /var/log/ansible/ansible.log in your ansible.cfg [defaults] section. All playbook runs will be appended to this file automatically.

How do I save command output from a remote host?

Use register to capture the output, then ansible.builtin.copy with content: "{{ result.stdout }}" to write it to a file. Use delegate_to: localhost to save on the control node.

How do I get JSON output from Ansible?

Set ANSIBLE_STDOUT_CALLBACK=json as an environment variable or stdout_callback = json in ansible.cfg. Redirect to a file: ANSIBLE_STDOUT_CALLBACK=json ansible-playbook site.yml > results.json.

Can I save output from multiple hosts to one file?

Yes. Use delegate_to: localhost with ansible.builtin.lineinfile or a template to aggregate output from all hosts into a single report file on the control node.

Conclusion

Choose the right output method for your needs: shell redirection for quick logging, log_path for automatic logging, register + copy for task-specific data, and callback plugins for structured output formats.

Related ArticlesAnsible debug Module: Print Variables and MessagesAnsible Callback Plugins: Customize OutputAnsible fetch Module: Download Files

Category: web-servers

Browse all Ansible tutorials · AnsiblePilot Home