Имеется роль nginx, в которой выполняется его настройка и копирование файлов.
Последней задачей роли выполняется nginx restart.
Проблема заключается в том, что попытка перезапуска NGINX выполняется в любом случае, и если в файле шаблона виртуалхоста есть ошибка — то NGINX не запустится.
Что бы выполнить проверку — nginx -t — добавляем задачу с вызовом модуля shell:
... - name: Check NGINX configs shell: "/usr/sbin/nginx -t" register: nginx_config_status ...
Далее добавляем вывод кода в аутпут Ansible — просто для себя:
...
- name: NGINX test status
debug:
msg: "{{ nginx_config_status.rc }}"
...
Или всё содержимое, а не только rc (return code):
...
- name: NGINX test status
debug:
msg: "{{ nginx_config_status }}"
...
И в последней задаче — добавляем условие when:
...
- name: Service NGINX restart and enable on boot
systemd:
name: nginx
state: restarted
enabled: yes
daemon_reload: yes
when: nginx_config_status.rc == 0
...
Хотя условие тут «условное», т.к. Ansible остановится в любом случае на выполеннии задачи «Check NGINX configs«, если она выполнится с ошибкой.
Теперь полностью всё выглядит так:
...
- name: Check NGINX configs
shell: "/usr/sbin/nginx -t"
register: nginx_config_status
- name: NGINX test status
debug:
msg: "{{ nginx_config_status }}"
- name: NGINX test status
debug:
msg: "{{ nginx_config_status.rc }}"
- name: Service NGINX restart and enable on boot
systemd:
name: nginx
state: restarted
enabled: yes
daemon_reload: yes
when: nginx_config_status.rc == 0
Проверяем.
Добавляем лишнюю фигурную скобку в один из конфигов, вызываем роль nginx:
[simterm]
...
TASK [nginx : Add NGINX RabbitMQ WebUI config] ****
changed: [production.mobilebackend.domain.world]
TASK [nginx : Check NGINX configs] ****
fatal: [production.mobilebackend.domain.world]: FAILED! => {"changed": true, "cmd": "/usr/sbin/nginx -t", "delta": "0:00:00.006300", "end": "2018-07-11 14:48:31.615100", "msg": "non-zero return code", "rc": 1, "start": "2018-07-11 14:48:31.608800", "stderr": "nginx: [emerg] unexpected \"}\" in /etc/nginx/conf.d/rabbitadmin-production.domain.world.conf:4\nnginx: configuration file /etc/nginx/nginx.conf test failed", "stderr_lines": ["nginx: [emerg] unexpected \"}\" in /etc/nginx/conf.d/rabbitadmin-production.domain.world.conf:4", "nginx: configuration file /etc/nginx/nginx.conf test failed"], "stdout": "", "stdout_lines": []}
PLAY RECAP ****
production.mobilebackend.domain.world : ok=4 changed=1 unreachable=0 failed=1
Something went wrong. Exit.
[/simterm]
ОК, исправляем ошибку, вызываем Ansible ещё раз:
[simterm]
...
TASK [nginx : Add NGINX RabbitMQ WebUI config] ****
changed: [production.mobilebackend.domain.world]
TASK [nginx : Check NGINX configs] ****
changed: [production.mobilebackend.domain.world]
TASK [nginx : NGINX test status] ****
ok: [production.mobilebackend.domain.world] => {
"msg": {
"changed": true,
"cmd": "/usr/sbin/nginx -t",
"delta": "0:00:00.007164",
"end": "2018-07-11 14:53:04.623855",
"failed": false,
"rc": 0,
"start": "2018-07-11 14:53:04.616691",
"stderr": "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok\nnginx: configuration file /etc/nginx/nginx.conf test is successful",
"stderr_lines": [
"nginx: the configuration file /etc/nginx/nginx.conf syntax is ok",
"nginx: configuration file /etc/nginx/nginx.conf test is successful"
],
"stdout": "",
"stdout_lines": []
}
}
TASK [nginx : NGINX test status] ****
ok: [production.mobilebackend.domain.world] => {
"msg": "0"
}
TASK [nginx : Service NGINX restart and enable on boot] ****
changed: [production.mobilebackend.domain.world]
PLAY RECAP ****
production.mobilebackend.domain.world : ok=8 changed=3 unreachable=0 failed=0
Provisioning done.
[/simterm]
Готово.




