如何忽略ansible中的警告

时间:2018-06-04 09:19:50

标签: ansible ansible-2.x

我一直试图在编写剧本时忽略警告,情节是我执行此virt-host-validate qemu,它会抛出一个警告,如下所示。

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)`

我写的剧本如下。

`- 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}}"`

现在令人担忧的部分是,如果还有任何其他任务因警告而继续,因为我们正在使用ignore_errors: yes 任何人都可以指导我如何解决它? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

我可以解决这个问题。

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

答案 1 :(得分:1)

您还可以在整个角色上将其作为发布任务运行,因此仅当该任务的返回码为非零或STDERR包含“错误”或“失败”时,才会验证该角色中的每个任务-任务将失败。

在所有命令/ shell任务上,注册输出:

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

然后在主要角色上添加一个后任务,该任务将在每个任务之后检查错误:

- 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)