2

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])?)*
asked Feb 12, 2018 at 18:52

1 Answer 1

3

You probably want to use the assert module:

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

See also Playbook Tests and Filters

Dale C. Anderson
2,4801 gold badge28 silver badges26 bronze badges
answered Feb 12, 2018 at 19:01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.