如果变量与正则表达式不匹配,则失败

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

寻找一个Ansible任务,如果变量与给定的正则表达式不匹配,将导致该剧本失败。就像是:

# fail when hostname doesn't match a regex
- fail:
    msg: "The inventory hostname must match regex"
  when: {{ inventory_hostname }} not matches [a-z](([-0-9a-z]+)?[0-9a-z])?(\.[a-z0-9](([-0-9a-z]+)?[0-9a-z])?)*
ansible
1个回答
2
投票

你可能想使用assert module

- assert:
    that:
    - inventory_hostname | match('your regex')

另见Playbook TestsFilters

© www.soinside.com 2019 - 2024. All rights reserved.