如何使用ansible查询本地网站?

问题描述 投票:0回答:1

我正在尝试对包含多个网站的网络主机进行健康检查。是否可以使用 URI 模块使用同一端口在不同站点之间循环?

我有这个代码:

# Check site directory is present

- name: "Check 'Site 1' site directory"
  ansible.windows.win_stat:
    path: C:\WWW\site1
  register: check_site_1

- name: "Check 'Site 2' site directory"
  ansible.windows.win_stat:
    path: C:\WWW\site2
  register: check_site_2

- name: "Check 'Site 3' site directory"
  ansible.windows.win_stat:
    path: C:\WWW\site3
  register: check_site_3

# Query Sites

# site-1.example.com
- name: "Check 'Site 1' site"
  uri:
    url: "http://{{ ansible_host }}:80/"
    status_code: 
      - 200 
      - 301 
      - 302
  register: result
    delay: 10
    failed_when: result.status == 404
  when: check_site_1.stat.exists

# site-2.example.com
- name: "Check 'Site 2' site"
  uri:
    url: "http://{{ ansible_host }}:80/"
    status_code: 
      - 200 
      - 301 
      - 302
  register: result
    delay: 10
    failed_when: result.status == 404
  when: check_site_2.stat.exists

# site-3.example.com
- name: "Check 'Site 3' site"
  uri:
    url: "http://{{ ansible_host }}:8080/"
    status_code: 
      - 200 
      - 301 
      - 302
  register: result
    delay: 10
    failed_when: result.status == 404
  when: check_site_3.stat.exists

问题是,当站点使用相同端口时,我想将主机名传递给站点,以使用 ansible_host 而不是定义的 url site-1.example.com 查询本地主机网站,因为这是一个活动的本地平衡器 url并且可能不指向同一主机。有没有人尝试过设置主机标头以使用 uri 模块在本地查询站点?

他,

x

ansible uri health-check
1个回答
0
投票

正如您所提到的 - 尝试将 headers 参数与 Host 一起添加到您的设置中:

# site-1.example.com
- name: "Check 'Site 1' site"
  uri:
    url: "http://{{ ansible_host }}:80/"
    headers:
      Host: "site-1.example.com"
    status_code: 
      - 200 
      - 301 
      - 302
  register: result
    delay: 10
    failed_when: result.status == 404
  when: check_site_1.stat.exists

# site-2.example.com
- name: "Check 'Site 2' site"
  uri:
    url: "http://{{ ansible_host }}:80/"
    headers:
      Host: "site-2.example.com"
    status_code: 
      - 200 
      - 301 
      - 302
  register: result
    delay: 10
    failed_when: result.status == 404
  when: check_site_2.stat.exists
© www.soinside.com 2019 - 2024. All rights reserved.