Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
How to include special characters in the 'when' module?

The variable error_code below contains this string:

"failed": true

How can I use this string as the trigger for the 'when module? I am not sure how to escape these special characters so the playbook interprets them correctly. Here's what I have tried but it is not working:

  - name: copying index
    copy: 
      src: /tmp/index.html
      dest: /var/www/html/
    notify: reloadone
    register: error_code
  - name: verify content
    fail:
      msg: There has been an error with the index file
    when: " \"failed\"\: true in error_code"
  handlers:
  - name: reloadone
    systemd:
      state: restarted
      name: httpd
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Put the string into the single-guotes, e.g.

- hosts: localhost
  gather_facts: false
  vars:
    error_code: '"failed": true'
  tasks:
    - debug:
        var: error_code
    - name: verify content
      fail:
        msg: There has been an error with the index file
      when: error_code == result
      vars:
        result: '"failed": true'

gives

TASK [debug] ******************************************************
ok: [localhost] => 
  error_code: '"failed": true'

TASK [verify content] *********************************************
fatal: [localhost]: FAILED! => changed=false 
  msg: There has been an error with the index file

The next option is to convert the string to a dictionary and test the Boolean value of the attribute failed, e.g.

- hosts: localhost
  gather_facts: false
  vars:
    error_code: '"failed": true'
  tasks:
    - debug:
        var: error_code|from_yaml
    - name: verify content
      fail:
        msg: There has been an error with the index file
      when: result.failed
      vars:
        result: "{{ error_code|from_yaml }}"

gives

TASK [debug] ****************************************************
ok: [localhost] => 
  error_code|from_yaml:
    failed: true

TASK [verify content] *******************************************
fatal: [localhost]: FAILED! => changed=false 
  msg: There has been an error with the index file

If the code doesn't fail

    error_code: '"failed": false' 

the condition will be skipped

TASK [debug] *****************************************************
ok: [localhost] => 
  error_code|from_yaml:
    failed: false

TASK [verify content] ********************************************
skipping: [localhost]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda