Ansible等待连接有效

Ansible to wait until connection is valid

我正在尝试使用 ansible 从 API 下载证书,这是我正在使用的:

- name: Get Shipa API address
  shell: kubectl --namespace=shipa-system get svc shipa-ingress-nginx -o jsonpath="{.status.loadBalancer.ingress[0].ip}"
  environment:
    TERM: dumb      
  register: shipa_api 
  until: shipa_api.stdout | ipaddr
  retries: 5
  delay: 90

等到 API IP 可用时效果很好,在下一步中,我这样做:

- name: Add Shipa as a target on CLI
  shell: shipa target add {{ cluster_name }} {{ shipa_apiaddr }} -s
  args:
    executable: /bin/bash
  no_log: False

此步骤调用 API IP 并尝试从 IP 检索证书,但问题是即使 IP 可用,API 服务也可能不可用 运行 但是,所以我得到这个作为回应:

fatal: [localhost]: FAILED! => {"changed": true, "cmd": "shipa target add s12-rc4-t2 35.247.97.88 -s", "delta": "0:00:00.164987", "end": "2020-12-18 11:53:23.177987", "msg": "non-zero return code", "rc": 1, "start": "2020-12-18 11:53:23.013000", "stderr": "Error: Get \"https://35.247.97.88:8081/certificates/ca\": dial tcp 35.247.97.88:8081: connect: connection refused", "stderr_lines": ["Error: Get \"https://35.247.97.88:8081/certificates/ca\": dial tcp 35.247.97.88:8081: connect: connection refused"], "stdout": "", "stdout_lines": []}

在尝试下一步“将 Shipa 添加为 CLI 上的目标”之前,Ansible 是否可以检查 API 服务是否可用,以免失败?

您可以使用模块 wait_for 来等待特定的 IP/port 对接受连接。

所以像这样:

- wait_for:
    host: 35.247.97.88
    port: 8081
    delay: 10

所以在你的情况下,我猜:

- wait_for:
    ## Depending on what is in the variable shipa_apiaddr 
    ## (just the ip or ip:port)
    # host: "{{ shipa_apiaddr.split(':').0 }}"  
    host: "{{ shipa_apiaddr }}" 
    ## Depending on what is in the variable shipa_apiaddr 
    ## (just the ip or ip:port)
    ## Not sure how you should prevent hardcoding it based on your question 
    # port: "{{ shipa_apiaddr.split(':').1 }}" 
    port: 8081 
    delay: 10