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
  • 15,372
  • 25
  • 90
  • 130

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,621
  • 1
  • 19
  • 20