0

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
U880D
  • 8,601
  • 6
  • 24
  • 40
southwind
  • 29
  • 7
  • @U880D I want to split vm_name. – southwind Aug 22 '23 at 10:24
  • @U880D - name: Convert Variable to list ansible.builtin.set_fact: new_vm_name: "{{ vm_name | split ('-') }}" Output Below:{ "msg": "Unexpected templating type error occurred on ({{ vm_name | split ('-') }}): descriptor 'split' for 'str' objects doesn't apply to a 'NoneType' object. descriptor 'split' for 'str' objects doesn't apply to a 'NoneType' object – southwind Aug 22 '23 at 10:32
  • @U880D My approach could be wrong. Main aim is vm_name should contain max 15 characters long, only allowed alpha-numeric and one - (hyphen) allowed that must be 6th character if exists in it, no uderscore or special characters allowed. – southwind Aug 22 '23 at 10:39
  • 1
    @U880D apologies if my question was not making sense before. I have edited my question as well. – southwind Aug 22 '23 at 10:56
  • Does [Check if variable/string matches condition](https://stackoverflow.com/a/72938067/6771046) or [Check variable/string matches the condition in Ansible](https://stackoverflow.com/a/72049548/6771046) answer your question? – U880D Aug 22 '23 at 10:58
  • `assert: that: vm_name is match('^[a-zA-Z0-9]{5}-[[a-zA-Z0-9]{9}$')` – U880D Aug 22 '23 at 11:33
  • Thanks @U880D. I have used same vm_name var as in my question which should pass assertion using below but for some reasons it fails. My understanding is in below assert we are using first 5 alpha/numeric characters then - then 9 alpha/numeric characters and based on that example I used should pass assertion? Please correct me if I am wrong. assert: that: vm_name is match('^[a-zA-Z0-9]{5}-[[a-zA-Z0-9]{9}$') – southwind Aug 22 '23 at 23:09
  • "_My understanding is in below assert we are using ..._", that's correct. In a minimal example test on my system it pass the test if the pattern fit, it fails if the pattern doesn't fit. – U880D Aug 23 '23 at 06:11

1 Answers1

0

Based on the discussion within the comments, a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    vm_name: abcde-123456789

  tasks:

  - assert:
      that: vm_name is match('^[a-zA-Z0-9]{5}-[a-zA-Z0-9]{9}$')
      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."

will result into an output of

TASK [assert] ****************************************************
ok: [localhost] => changed=false
  msg: 'vm_name: abcde-123456789 has been successfully validated.'

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40
  • Maybe you can have also a look into [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – U880D Aug 25 '23 at 06:54