3

how to validate the variable value using regex in ansible

var:
  DeviceName: "xxxxx-ax34tas1.xxxint.pngxx.abc.com"

- name: valdiate switchname
      set_fact:
        switchname: "{{'false' if DeviceName | regex_match('\\S+(ias|pas|tas|sps)\\S+(mfg|kssm|png|cd|ss|ch|ra|cr)\\S+') else 'true'}}"
  

it should come as false but i dont know why it is coming true

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47
divay singhal
  • 31
  • 1
  • 2

1 Answers1

2

As far as I know the documentation there is no filter regex_match, at least as of v2.9 and later.

it should come as false but I dont know why it is coming true

Since a simple test construct

var:
  DeviceName: "switch.example.com"

- name: Valdiate device name
  set_fact:
    switchname: "{{'false' if DeviceName | regex_search('switch') else 'true'}}"

delivers true for a switch and false for a server it seems to be your regular expression.

After I've found Use Jinja match filter instead of regex_match

[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using result|match use result is match. This feature will be removed in version 2.9.

I've tested with

var:
  DeviceName: "switch.example.com"

- name: Valdiate device name
  set_fact:
    switchname: "{{ DeviceName is match ('server*') }}"

and found it also working properly. As of Ansible 2.9 this should be the preferred solution.

U880D
  • 1,017
  • 2
  • 12
  • 18