Score:1

Need to know more about the handlers in ansible

sj flag

Below is the tasks I have in my nginx role

# 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

And I have a separate directory called handlers, which has below content.

- 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")

Not sure, if the variable command_output will be read by the handlers.

Score:1
in flag

It won't, because command_output does not exist at the handler. You could modify the error handling of the check task and define "changed" with your condition, but in this case I'd just use the validate attribute in the template task and skip the rest.

- 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

Note: The name of the handler needs to be changed to Reload nginx for this to work.

Vicky Ingle - Mint avatar
sj flag
Will the validation work if the result of nginx -t if it has warnings ?
in flag
Good question. It depends on the return code of nginx, but I would assume yes.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.