AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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.

See also: Ansible Callback Plugins: Customize Output, Logging, and Notifications Complete Guide

Method 1: Shell Redirection

# Redirect all output to a file
ansible-playbook site.yml > output.log 2>&1

# Tee: display AND save ansible-playbook site.yml 2>&1 | tee output.log

# With timestamp in filename ansible-playbook site.yml 2>&1 | tee "deploy-$(date +%Y%m%d-%H%M%S).log"

Method 2: Ansible Log File (ansible.cfg)

# ansible.cfg
[defaults]
log_path = /var/log/ansible/ansible.log
# Or via environment variable
export ANSIBLE_LOG_PATH=/var/log/ansible/ansible.log
ansible-playbook site.yml

This logs all Ansible output automatically without changing playbooks.

See also: AAP 2.6 Monitoring and Logging: Prometheus, Grafana, and Log Aggregation

Method 3: Register and Save with copy

- name: Run command and capture output
  ansible.builtin.command: df -h
  register: disk_output
  changed_when: false

- name: Save output to file on remote host ansible.builtin.copy: content: "{{ disk_output.stdout }}" dest: /tmp/disk-report.txt

- name: Save output to file on control node ansible.builtin.copy: content: "{{ disk_output.stdout }}" dest: /tmp/disk-report-{{ inventory_hostname }}.txt delegate_to: localhost

Save Structured Data

- name: Gather system info
  ansible.builtin.setup:
    filter: ansible_distribution*
  register: sys_info

- name: Save as JSON ansible.builtin.copy: content: "{{ sys_info | to_nice_json }}" dest: /tmp/system-info.json delegate_to: localhost

- name: Save as YAML ansible.builtin.copy: content: "{{ sys_info | to_nice_yaml }}" dest: /tmp/system-info.yml delegate_to: localhost

Method 4: Callback Plugins

JSON Log Callback

# ansible.cfg
[defaults]
callbacks_enabled = ansible.builtin.log_plays
stdout_callback = ansible.builtin.json

# Or for YAML-formatted output stdout_callback = ansible.builtin.yaml

Custom Log Callback

# ansible.cfg
[defaults]
callbacks_enabled = community.general.log_plays

[callback_log_plays] log_folder = /var/log/ansible/plays

Per-Run JSON Output

# Output as JSON
ANSIBLE_STDOUT_CALLBACK=json ansible-playbook site.yml > results.json

# Parse results cat results.json | jq '.plays[].tasks[].hosts'

See also: Ansible Monitoring and Observability: Prometheus, Grafana, and ELK Stack Integration

Method 5: Write to File During Playbook

- name: Collect info from all hosts
  ansible.builtin.command: hostname -f
  register: hostname_result
  changed_when: false

- name: Build report file ansible.builtin.lineinfile: path: /tmp/inventory-report.txt line: "{{ inventory_hostname }}: {{ hostname_result.stdout }}" create: true delegate_to: localhost

Aggregate Results

- name: Collect metrics from all hosts
  ansible.builtin.command: uptime
  register: uptime_result
  changed_when: false

- name: Write consolidated report ansible.builtin.template: src: report.j2 dest: /tmp/fleet-report.txt run_once: true delegate_to: localhost vars: all_results: "{{ ansible_play_hosts | map('extract', hostvars, 'uptime_result') | list }}"

Method 6: Fetch Remote Files

- name: Generate report on remote host
  ansible.builtin.command: /opt/scripts/generate-report.sh
  register: report

- name: Fetch report to control node ansible.builtin.fetch: src: /tmp/report.txt dest: ./reports/{{ inventory_hostname }}/ flat: true

Combine Multiple Methods

---
- name: Deployment with full logging
  hosts: webservers
  vars:
    log_dir: /tmp/deploy-logs
    timestamp: "{{ ansible_date_time.iso8601_basic_short }}"

pre_tasks: - name: Create log directory ansible.builtin.file: path: "{{ log_dir }}" state: directory delegate_to: localhost run_once: true

tasks: - name: Deploy application ansible.builtin.copy: src: myapp.tar.gz dest: /opt/myapp/ register: deploy_result

- name: Log deployment result ansible.builtin.copy: content: | Host: {{ inventory_hostname }} Time: {{ ansible_date_time.iso8601 }} Changed: {{ deploy_result.changed }} Checksum: {{ deploy_result.checksum | default('N/A') }} dest: "{{ log_dir }}/{{ inventory_hostname }}-{{ timestamp }}.log" delegate_to: localhost

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 Articles

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

Category: web-servers

Browse all Ansible tutorials · AnsiblePilot Home