Simplify error and rescue output

Hi,

I have a task for configuring, testing and restarting apache. It works as expected, but the output is rather verbose. I’ve included sample output for a malformed configuration.

For the error I would like to do something like outputting only the stderr field.

The restore and delete backup tasks seem to output the whole task result. I would like to either output nothing at all, or perhaps only the file name or something, though I’m not sure exactly what the output is.

Using ansible core 2.19.4.

- name: Configure apache
  block:
    - name: Copy files
      ansible.builtin.copy:
        src: apache/{{ item }}
        dest: "/usr/local/etc/apache24/Includes/{{ item }}"
        backup: true
      with_items:
        - 'macro-redirect-https.conf'
      register: updated

    - name: "Test config"
      ansible.builtin.shell: 'apachectl -t'
      when: updated is changed
      notify:
        - Restart apache
  rescue:
    - name: Restore backup
      ansible.builtin.copy:
        remote_src: true
        src: "{{ item['backup_file'] }}"
        dest: "{{ item['dest'] }}"
      with_items: "{{ updated.results }}"
      when: updated is changed
  always:
    - name: Delete backup
      ansible.builtin.file:
        path: "{{ item['backup_file'] }}"
        state: absent
      with_items: "{{ updated.results }}"
      when: updated is changed

Problematic output:

TASK [freebsd : Test config] *************************************************************************************************************
[ERROR]: Task failed: Module failed: non-zero return code
Origin: /home/e/ansible_quickstart/roles/freebsd/tasks/apache.yaml:27:7

25       register: updated
26
27     - name: "Test config"
         ^ column 7

fatal: [web01]: FAILED! => {
    "changed": true,
    "cmd": "apachectl -t",
    "delta": "0:00:00.044928",
    "end": "2026-03-23 13:03:58.560124",
    "msg": "non-zero return code",
    "rc": 1,
    "start": "2026-03-23 13:03:58.515196",
    "stderr": "AH00526: Syntax error on line 2 of macro 'redirecthttps' (defined on line 1 of \"/usr/local/etc/apache24/Includes/macro-redirect-https.conf\") used on line 4 of \"/usr/local/etc/apache24/vhost.conf\":\nInvalid command 'RedirectMatch2', perhaps misspelled or defined by a module not included in the server configuration",
    "stderr_lines": [
        "AH00526: Syntax error on line 2 of macro 'redirecthttps' (defined on line 1 of \"/usr/local/etc/apache24/Includes/macro-redirect-https.conf\") used on line 4 of \"/usr/local/etc/apache24/vhost.conf\":",
        "Invalid command 'RedirectMatch2', perhaps misspelled or defined by a module not included in the server configuration"
    ],
    "stdout": "",
    "stdout_lines": []
}

TASK [freebsd : Restore backup] **********************************************************************************************************
changed: [web01] => (item={'diff': [{'before_header': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf', 'before': '<Macro RedirectHTTPS $hostname>\n    RedirectMatch permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n</Macro>\n<Macro RedirectHTTPS2 $hostname>\n    RedirectMatch2 permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n</Macro>\n', 'after_header': '/home/e/ansible_quickstart/roles/freebsd/files/apache/macro-redirect-https.conf', 'after': '<Macro RedirectHTTPS $hostname>\n    RedirectMatch permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n    RedirectMatch2 permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n</Macro>\n'}], 'dest': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf', 'src': '/home/e/.ansible/tmp/ansible-tmp-1774267431.6175117-1991-16980776241965/.source.conf', 'md5sum': '1e390764aa09d46e448c23ef01634fed', 'checksum': 'ea2418ad9d069e6fccea1e6e67e12ded165b94eb', 'changed': True, 'backup_file': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf.81850.2026-03-23@13:03:58~', 'uid': 0, 'gid': 0, 'owner': 'root', 'group': 'wheel', 'mode': '0644', 'state': 'file', 'size': 216, 'invocation': {'module_args': {'backup': True, 'dest': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf', 'src': '/home/e/.ansible/tmp/ansible-tmp-1774267431.6175117-1991-16980776241965/.source.conf', '_original_basename': 'macro-redirect-https.conf', 'follow': False, 'checksum': 'ea2418ad9d069e6fccea1e6e67e12ded165b94eb', 'force': True, 'remote_src': False, 'unsafe_writes': False, 'content': None, 'validate': None, 'directory_mode': None, 'local_follow': None, 'mode': None, 'owner': None, 'group': None, 'seuser': None, 'serole': None, 'selevel': None, 'setype': None, 'attributes': None}}, 'failed': False, 'item': 'macro-redirect-https.conf', 'ansible_loop_var': 'item'})

TASK [freebsd : Delete backup] ***********************************************************************************************************
--- before
+++ after
@@ -1,4 +1,4 @@
 {
     "path": "/usr/local/etc/apache24/Includes/macro-redirect-https.conf.81850.2026-03-23@13:03:58~",
-    "state": "file"
+    "state": "absent"
 }

changed: [web01] => (item={'diff': [{'before_header': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf', 'before': '<Macro RedirectHTTPS $hostname>\n    RedirectMatch permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n</Macro>\n<Macro RedirectHTTPS2 $hostname>\n    RedirectMatch2 permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n</Macro>\n', 'after_header': '/home/e/ansible_quickstart/roles/freebsd/files/apache/macro-redirect-https.conf', 'after': '<Macro RedirectHTTPS $hostname>\n    RedirectMatch permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n    RedirectMatch2 permanent ^/((?!\\.well-known/acme-challenge/).*)$ https://$hostname/\n</Macro>\n'}], 'dest': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf', 'src': '/home/e/.ansible/tmp/ansible-tmp-1774267431.6175117-1991-16980776241965/.source.conf', 'md5sum': '1e390764aa09d46e448c23ef01634fed', 'checksum': 'ea2418ad9d069e6fccea1e6e67e12ded165b94eb', 'changed': True, 'backup_file': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf.81850.2026-03-23@13:03:58~', 'uid': 0, 'gid': 0, 'owner': 'root', 'group': 'wheel', 'mode': '0644', 'state': 'file', 'size': 216, 'invocation': {'module_args': {'backup': True, 'dest': '/usr/local/etc/apache24/Includes/macro-redirect-https.conf', 'src': '/home/e/.ansible/tmp/ansible-tmp-1774267431.6175117-1991-16980776241965/.source.conf', '_original_basename': 'macro-redirect-https.conf', 'follow': False, 'checksum': 'ea2418ad9d069e6fccea1e6e67e12ded165b94eb', 'force': True, 'remote_src': False, 'unsafe_writes': False, 'content': None, 'validate': None, 'directory_mode': None, 'local_follow': None, 'mode': None, 'owner': None, 'group': None, 'seuser': None, 'serole': None, 'selevel': None, 'setype': None, 'attributes': None}}, 'failed': False, 'item': 'macro-redirect-https.conf', 'ansible_loop_var': 'item'})

You can reduce the block to a single copy task by using the validate option.

Thanks, but that is not sufficient in this instance, as I’m only providing partial configuration, which won’t validate without first having loaded the rest of Apache’s configuration.