Ansible-使用变量的when语句

时间:2019-04-05 11:30:40

标签: python json linux ansible

我有一本Ansible剧本,用于检查多个Web门户的可用性。以下是我的剧本:

.gitignore

以下是上述任务的输出:

B

我创建了另一个任务,该任务将从已注册的变量中提取状态代码:

- 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语句永远无效。无论状态代码如何,我总是会收到电子邮件通知。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

最简单的解决方案可能是摆脱json_query位,而仅循环执行uri任务的结果,并在相应项的状态不是200时生成电子邮件。 / p>

您需要通读using register in a loop上的Ansible文档,以了解发生了什么情况。

您将使用以下内容:

- 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 {{ item.item }}"
    body: "{{ item.item }} is down"
  when: item.status != 200
  loop: "{{ uptime.results }}"

此操作遍历uptime.results,其中包含您在初始循环中执行的每个uri任务的结果。在此任务的每次迭代中,itemuri任务的结果,而item.itemuri任务中输入的相应主机名。