如何在Ansible中使用assert模块?

问题描述 投票:0回答:1

我想要一本用

assert
编写的 Ansible 剧本,并检查这两个接口是否不相等:
bond_interfaces: [eno5, eno6]

node0125.compute.neda: 
  management_ip: 172.21.48.35
  management_interface: ens3f0
  bond_interfaces: [eno5, eno6]
  private_ip: 172.21.32.35
  # ilo_ip: 172.21.56.5
  ansible_host: '{{ management_ip }}'
  tier: balanced
  os_version: 20.04

我不知道怎么写。

ansible assert
1个回答
0
投票

根据

assert
模块的文档 – 断言给定的表达式为真一个最小的示例剧本可能看起来像

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    node0125_compute_neda: 
      bond_interfaces: [eno5, eno6]

  tasks:

  - assert:
      that:
        - node0125_compute_neda.bond_interfaces[0] != node0125_compute_neda.bond_interfaces[1]
      fail_msg: "Network bond is not over two different interfaces"

产生

的输出
TASK [assert] ******************
ok: [localhost] => changed=false
  msg: All assertions passed

或者如果在同一个界面上

TASK [assert] ******************************************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false
  assertion: node0125_compute_neda.bond_interfaces[0] != node0125_compute_neda.bond_interfaces[1]
  evaluated_to: false
  msg: Network bond is not over two different interfaces
© www.soinside.com 2019 - 2024. All rights reserved.