I want to validate vm_name
variable.
This var should have max. 15 characters allowed, no underscore, no special characters like @#$%
, only one (-
) hyphen allowed at position/character number 6. If any of this condition does not pass assertion should fail.
In below code, the only missing part is how to fail if -
hypens are more than one and if not at position/character 6 in above vm_name
example?
Thanks
- name: Validate VM Name
hosts: localhost
vars:
vm_name: abcde-123456789
tasks:
- name: Validate vm_name
ansible.builtin.assert:
that: "(
vm_name | regex_findall('[A-Za-z0-9-]'))[:15] | join | length == vm_name | length"
fail_msg: "incorrect vm_name:{{ vm_name }} ,valid options are: max 15 characters, [0-9], [a-z], [A-Z],-, no special characters like #$%*&"
success_msg: "vm_name: {{ vm_name }} has been successfully validated"
register: validate_vm_name_out