Skip to content

Ansible Error Handling

Basic Error Handling

Ignore Errors

# Simple error ignoring
- name: Attempt task with ignored errors
  ansible.builtin.command: /usr/local/bin/may-fail
  ignore_errors: yes

# Conditional error ignoring
- name: Install package
  ansible.builtin.apt:
    name: "{{ package_name }}"
    state: present
  ignore_errors: "{{ ansible_check_mode }}"

Block Error Handling

# Basic block error handling
- name: Handle errors with blocks
  block:
    - name: Execute risky task
      ansible.builtin.command: /usr/local/bin/risky-operation

  rescue:
    - name: Execute on failure
      ansible.builtin.debug:
        msg: "Operation failed, executing backup plan"

  always:
    - name: Always execute
      ansible.builtin.debug:
        msg: "Cleaning up resources"

Advanced Error Handling

Failed Status Checks

# Check and handle task status
- name: Check service status
  ansible.builtin.service:
    name: myapp
    state: started
  register: service_status
  ignore_errors: yes

- name: Handle service failure
  ansible.builtin.debug:
    msg: "Service failed to start: {{ service_status.msg }}"
  when: service_status is failed

# Multiple failure handling
- name: Complex failure handling
  block:
    - name: Primary operation
      ansible.builtin.command: primary-operation
      register: primary

    - name: Fallback operation
      ansible.builtin.command: fallback-operation
      when: primary is failed
      register: fallback

    - name: Emergency operation
      ansible.builtin.command: emergency-operation
      when: fallback is failed

  rescue:
    - name: Notify on complete failure
      ansible.builtin.mail:
        to: [email protected]
        subject: "Operation failed"
        body: "All operations failed"

Custom Error Handling

# Define custom error conditions
- name: Set error threshold
  ansible.builtin.set_fact:
    max_failures: 3
    current_failures: 0

- name: Execute with retry
  block:
    - name: Attempt operation
      ansible.builtin.command: unstable-operation
      register: result
      until: result is success
      retries: "{{ max_failures }}"
      delay: 5
  rescue:
    - name: Handle maximum retries
      ansible.builtin.fail:
        msg: "Operation failed after {{ max_failures }} attempts"
      when: current_failures|int >= max_failures|int

Error Recovery

State Recovery

# Backup and restore
- name: Modify configuration with backup
  block:
    - name: Backup configuration
      ansible.builtin.copy:
        src: /etc/myapp/config.yml
        dest: /etc/myapp/config.yml.bak
        remote_src: yes

    - name: Modify configuration
      ansible.builtin.template:
        src: new_config.yml.j2
        dest: /etc/myapp/config.yml

  rescue:
    - name: Restore configuration
      ansible.builtin.copy:
        src: /etc/myapp/config.yml.bak
        dest: /etc/myapp/config.yml
        remote_src: yes
      when: ansible_failed_task is defined

Best Practices

Error Documentation

# Document error handling strategy
- name: Document error handling
  block:
    - name: Set error documentation
      ansible.builtin.set_fact:
        error_strategy: |
          Error Handling Strategy:
          1. Attempt primary operation
          2. Try fallback if primary fails
          3. Notify admin on complete failure
          4. Always cleanup resources

    - name: Execute with documented strategy
      ansible.builtin.include_tasks: operations.yml

  rescue:
    - name: Log error with context
      ansible.builtin.debug:
        msg: "Failed task: {{ ansible_failed_task.name }}"

Error Logging

# Implement error logging
- name: Log error details
  block:
    - name: Execute operation
      ansible.builtin.command: critical-operation
      register: result

  rescue:
    - name: Log failure details
      ansible.builtin.copy:
        content: |
          Time: {{ ansible_date_time.iso8601 }}
          Task: {{ ansible_failed_task.name }}
          Error: {{ ansible_failed_result }}
        dest: /var/log/ansible/errors.log

Error Monitoring

# Error monitoring integration
- name: Monitor errors
  block:
    - name: Execute monitored task
      ansible.builtin.command: monitored-operation
      register: operation

  rescue:
    - name: Send error metrics
      ansible.builtin.uri:
        url: "http://monitoring.example.com/api/errors"
        method: POST
        body_format: json
        body:
          task: "{{ ansible_failed_task.name }}"
          error: "{{ ansible_failed_result }}"
          timestamp: "{{ ansible_date_time.iso8601 }}"
      delegate_to: localhost