Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

640
Visualizações
Allow null or empty values in a variable using Ansible

I have this piece of code that checks if the variable "owner" matches with the following regex and accepts that it is undefined, that is, the playbook can work without that variable being passed.

varexample is undefined or varexample is match('^[a-zA-Z]+$')

What I would like to do is that this variable accepts empty or null values. What I tried is something like this

varexample is null or varexample is match('^[a-zA-Z]+$')

But I got this error:

The conditional check 'varexample is null or varexample is match('[a-zA-Z]+')' failed. The error was: template error while templating string: no test named 'null'. String: {% if varexample is null or varexample is match('[a-zA-Z]+') %} True {% else %} False {% endif %}

Can someone give me a hint or some help?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Q: "variable accepts null values"

A: It's a bug. Ansible shouldn't match null to '^[a-zA-Z]+$'

    - set_fact:
        varexample:
    - debug:
        var: varexample
    - debug:
        msg: "undefined: {{ varexample is undefined }}"
    - debug:
        msg: "match: {{ varexample is match('^[a-zA-Z]+$') }}"

gives

  varexample: null
  msg: 'undefined: False'
  msg: 'match: True'

As a result of this bug, your condition should be working as expected

  varexample is undefined or varexample is match('^[a-zA-Z]+$')

To be on the save side, for the case the bug will be fixed, you can additionally test None, e.g.

    - debug:
        msg: "Test passed. varexample: {{ item.varexample }}"
      when: item.varexample is undefined or
            item.varexample == None or
            item.varexample is match('^[a-zA-Z]+$')
      loop:
        - varexample: ABC
        - varexample: 123
        - varexample:

gives

ok: [localhost] => (item={'varexample': 'ABC'}) => 
  msg: 'Test passed. varexample: ABC'
skipping: [localhost] => (item={'varexample': 123}) 
ok: [localhost] => (item={'varexample': None}) => 
  msg: 'Test passed. varexample: '

Details

    - debug:
        msg: |
          Undefined: {{ item.varexample is undefined }}
          Is None: {{ item.varexample == None }}
          Match a-zA-Z: {{ item.varexample is match('^[a-zA-Z]+$') }}
      loop:
        - varexample: ABC
        - varexample: 123
        - varexample:
ok: [localhost] => (item={'varexample': 'ABC'}) => 
  msg: |-
    Undefined: False
    Is None: False
    Match a-zA-Z: True
ok: [localhost] => (item={'varexample': 123}) => 
  msg: |-
    Undefined: False
    Is None: False
    Match a-zA-Z: False
ok: [localhost] => (item={'varexample': None}) => 
  msg: |-
    Undefined: False
    Is None: True
    Match a-zA-Z: True
over 4 years ago · Santiago Trujillo Relatório

0

Simply use a default that your match would consider truthy.

For example; with a varexample | default('a') is match('^[a-zA-Z]+$'), you should be able to achieve what you are looking for.

Given the tasks:

- debug:
    msg: "for {{ item }} the result is {{ item | default('a') is match('[a-zA-Z]+') }}"
  loop: "{{ cases }}"
  vars:
    cases:
      - ~
      - 123
      - abc
      - 

- debug:
    msg: "for an undefined variable the result is {{ item | default('a') is match('[a-zA-Z]+') }}"

This yields:

TASK [debug] ****************************************************************************
ok: [localhost] => (item=None) => 
  msg: for  the result is True
ok: [localhost] => (item=123) => 
  msg: for 123 the result is False
ok: [localhost] => (item=abc) => 
  msg: for abc the result is True
ok: [localhost] => (item=None) => 
  msg: for  the result is True

TASK [debug] ****************************************************************************
ok: [localhost] => 
  msg: for an undefined variable the result is True
over 4 years ago · Santiago Trujillo Relatório

0

I think the closest answer to exactly what you asked for is something like:

varexample is defined and varexample is match('^[a-zA-Z]+$')

You can also use "is not defined", or just providing a default like mentioned in the other answer is another good way to go.

varexample|default('') is match('^[a-zA-Z]+$')
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda