user3398900
user3398900

Reputation: 845

How to Ignore warnings in ansible

I have been trying to ignore a warning while writing playbooks the scenario is i execute this virt-host-validate qemu and it throws up one single warning like as below.

root@n0:~/playbook_promenade# virt-host-validate 
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists :PASS
QEMU: Checking if device /dev/kvm is accessible : PASS
QEMU: Checking if device /dev/vhost-net exists : PASS
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup 'memory' controller support : PASS
QEMU: Checking for cgroup 'memory' controller mount-point : PASS
QEMU: Checking for cgroup 'cpu' controller support : PASS
QEMU: Checking for cgroup 'cpu' controller mount-point: PASS
QEMU: Checking for cgroup 'cpuacct' controller support : PASS
QEMU: Checking for cgroup 'cpuacct' controller mount-point : PASS
QEMU: Checking for cgroup 'devices' controller support : PASS
QEMU: Checking for cgroup 'devices' controller mount-point : PASS
QEMU: Checking for cgroup 'net_cls' controller support : PASS
QEMU: Checking for cgroup 'net_cls' controller mount-point : PASS
QEMU: Checking for cgroup 'blkio' controller support : PASS
QEMU: Checking for cgroup 'blkio' controller mount-point : PASS
QEMU: Checking for device assignment IOMMU support  : WARN (No ACPI DMAR table found, IOMMU either disabled in BIOS or not supported by this hardware platform)`

The playbook i have written is as follows.

`- hosts: localhost
   sudo: yes
   tasks:
    - name: Check if Host supports Virtualization.
      command: virt-host-validate qemu 
      ignore_errors: yes
      failed_when: false
      changed_when: false
      register: host_status
    - debug:
       msg: "status: {{host_status.stdout}}"`

now the worrying part is if there is any other task failing apart from the warn this playbook continues as we are using ignore_errors: yes can anyone please guide me how to fix it ?? thanks in advance.

Upvotes: 4

Views: 20272

Answers (2)

Noam Manos
Noam Manos

Reputation: 16971

You can also run it as a post task on the whole role, so each task in that role will be verified, and only if it has a non-zero return code or an STDERR that contains "error" or "fail" - the task will fail.

On all of the command/shell tasks, register the output:

- name: A shell task
  shell: |
    # your commands ...
  register: out

Then on the main role, add a post-task that will check for errors after each task:

- name: A role of shell/command tasks
  roles:
  - role: ...
  ...
  post_tasks:
  - name: Checking if command failed, or if stderr contains errors/failures
    fail: msg={{ out.stderr.split('\n') }}
    when: out is defined and out.stderr is defined and (out.stderr | lower | regex_search('error|fail') or out.rc != 0)

Upvotes: 0

user3398900
user3398900

Reputation: 845

I could fix it with this.

  tasks:
   - name: Check if Host supports Virtualization.
     command: virt-host-validate qemu
     register: command_result
     failed_when:
       - "'FAIL' in command_result.stderr"

Upvotes: 2

Related Questions