У меня есть Ansible playbook, который используется для проверки доступности нескольких веб-порталов. Ниже моя пьеса:
- hosts: 127.0.0.1
gather_facts: yes
tasks:
- name: Checking the Application availability status...
uri:
url: https://{{ item }}.example.com
with_inventory_hostnames:
- production
ignore_errors: yes
register: uptime
Ниже приведен результат выполнения вышеуказанной задачи:
ok: [127.0.0.1] => {
"uptime": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": true,
"_ansible_item_label": "server1",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"cache_control": "no-cache, no-store, must-revalidate",
"changed": false,
"connection": "close",
"content_type": "text/html;charset=UTF-8",
"cookies": {
"JSESSIONID": "A9058dfgD19850855C02B4"
},
"cookies_string": "JSESSIONID=A9058343534fghjuy50855C02B4",
"date": "Fri, 05 Apr 2019 11:03:18 GMT",
"expires": "Thu, 01 Jan 1970 00:00:00 GMT",
"failed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 30,
"unsafe_writes": null,
"url": "https://myapp.com/Login.xhtml",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": true
}
},
"item": "server1",
"msg": "OK (unknown bytes)",
"pragma": "no-cache",
"redirected": false,
"server": "nginx",
"set_cookie": "JSESSIONID=A90583ggggh9850855C02B4; Expires=Fri, 05-Apr-2019 23:03:18 GMT; Path=/; HttpOnly",
"status": 200,
"strict_transport_security": "max-age=31536000; includeSubdomains;",
"transfer_encoding": "chunked",
"url": "https://myapp.com/Login.xhtml",
"vary": "Accept-Encoding",
"x_frame_options": ""
},
{
"_ansible_ignore_errors": true,
"_ansible_item_label": "server2",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"cache_control": "no-cache, no-store, must-revalidate",
"changed": false,
"connection": "close",
"content_type": "text/html;charset=UTF-8",
"cookies": {
"JSESSIONID": "647948952hgftrfEC51119"
},
"cookies_string": "JSESSIONID=64794cccvf52C4EC51119",
"date": "Fri, 05 Apr 2019 11:03:20 GMT",
"expires": "Thu, 01 Jan 1970 00:00:00 GMT",
"failed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 30,
"unsafe_writes": null,
"url": "https://myapp.com/Login.xhtml",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": true
}
},
"item": "server2",
"msg": "OK (unknown bytes)",
"pragma": "no-cache",
"redirected": false,
"server": "nginx",
"set_cookie": "JSESSIONID=647948952cccc1119; Expires=Fri, 05-Apr-2019 23:03:20 GMT; Path=/; HttpOnly",
"status": 200,
"strict_transport_security": "max-age=31536000; includeSubdomains;",
"transfer_encoding": "chunked",
"url": "://myapp.com/Login.xhtml",
"vary": "Accept-Encoding",
"x_frame_options": ""
}
]
}
}
Я создал еще одну задачу, которая будет извлекать код состояния из зарегистрированной переменной:
- name: Setting up the status code fact...
set_fact:
api_status: "{{ uptime | json_query('[results[*].status]') }}"`
I'm receiving the expected output as below when I `debug`:
`ok: [127.0.0.1] => {
"msg": [
[
200,
200
]
]
}
Теперь я пытаюсь настроить задачу электронной почты, которая будет уведомлять меня, если код ответа отличается от 200
:
- name: Notifying the IT team about the outage...
mail:
host: smtp.server.com
port: 25
username: my@example.com
password: mypassword
subtype: html
from: noreply@company.com
to: admin@company.com
subject: Server Outage
body: Hi {{ uptime | json_query('results[*].item') }} is down
when: api_status !=200
Но оператор when
никогда не работает. Я всегда получаю уведомление по электронной почте независимо от кода состояния. В чем может быть проблема?