
A continuación se muestran las tareas que tengo en mi rol de nginx.
# tasks file for nginx
- name: Backup and update nginx configuration file
template:
src: templates/proxy.conf.j2
dest: "{{ nginx_conf }}"
backup: true
owner: root
group: root
mode: 0644
- name: Running nginx -t to validate the config
shell: 'nginx -t'
register: command_output
become: true
notify: Reload nginx
- debug:
var: command_output.stderr_lines
Y tengo un directorio separado llamado controladores, que tiene el contenido siguiente.
- name: Reloading nginx service only if the syntax is ok
systemd:
name: nginx.service
state: reloaded'
when: command_output.stderr | regex_search("syntax is ok")
No estoy seguro de si los controladores leerán la variable command_output.
Respuesta1
No lo hará porque command_output
no existe en el controlador. Puede modificar el manejo de errores de la tarea de verificación ydefinir "cambiado"con su condición, pero en este caso solo usaría el validate
atributo en eltemplate
tarea y omitir el resto.
- name: Backup and update nginx configuration file
template:
src: templates/proxy.conf.j2
dest: "{{ nginx_conf }}"
backup: true
owner: root
group: root
mode: 0644
validate: nginx -t
notify: Reload nginx
Nota: Es necesario cambiar el nombre del controlador Reload nginx
para que esto funcione.