1

Looking for an Ansible task that will fail the playbook if a variable does not match a given regex. Something like:

# 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])?)*
DarVar
  • 16,882
  • 29
  • 97
  • 146

1 Answers1

2

You probably want to use the assert module:

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

See also Playbook Tests and Filters

tinita
  • 3,987
  • 1
  • 21
  • 23