After deploy new configuration of Nginx, I would like to verify configuration before Nginx handler for reload is applied. This is output of command line:
nginx -t && echo $?
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
0
Based on output above, I configured Ansible command
task like this:
- name: Verify Nginx config
become: yes
command: nginx -t
register: nginx_verify_config
check_mode: no
changed_when: "'syntax is ok' not in nginx_verify_config.stdout"
- debug:
msg: "{{nginx_verify_config.stdout}}"
changed_when
should show something changed only if output of command is mising syntax is ok
. This does not work and I found out, the message in nginx_verify_config
looks like this:
ok: [DevNgx] => {
"msg": {
"changed": true,
"cmd": [
"nginx",
"-t"
],
"delta": "0:00:00.018083",
"end": "2023-04-14 14:44:16.656261",
"failed": false,
"msg": "",
"rc": 0,
"start": "2023-04-14 14:44:16.638178",
"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": []
}
}
Why is message stored under stderr
sections msg
and stdout
are empty?
I tried to add 2>&1
at the end of the command, but it raised error nginx: invalid option: "2>&1"
.