經過漫長的旅程才做了簡單的健康檢查,我找到了解決方案。
首先,我們要獲得多克爾Container's IP地址,可以在一個PowerShell可以輕鬆完成這個命令:
docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' <yourInstanceIdHere>
我們只需要使用win_shell module。
但是因爲這個使用Docker模板機制,所以Jinja2模板不知道,它不應該這次解釋這些命令。我們必須正確地擺脫大括號,這在this so q&a already中列出。您可以使用建議的解決方案之一:
"{%raw%}"{{ .NetworkSettings.Networks.nat.IPAddress }}"{%endraw%}"
或
"{{'{{'}} .NetworkSettings.Networks.nat.IPAddress {{'}}'}}"
- 都將爲我們在這裏工作。
現在獲得從這個輸出的IP地址,我想剛剛註冊的結果,使健康檢查。可悲的是這個doesn't工作,因爲返回stdout
和stdout_lines
確實包含您的IP,同時也再次泊塢窗模板 - 但這次沒有逃逸序列,這將反過來讓任務this so answer失敗(如評論從Davide Guerri已經報道)。
從lanwen這樣的評論給了一個建議救援:我們能管的第一win_shell輸出到一個臨時文本文件container_ip.txt
,然後 - 在第二win_shell任務 - 我們剛纔讀的文件的內容,並註冊一個輸出 - 變量。
這似乎很容易,我們再次使用win_shell:
win_shell: cat container_ip.txt
register: win_shell_txt_return
但是,嘿,that's並不是故事的全部 - >因爲在Windows中,有nice carriage return line feeds :),這將\r\n
污染我們的IP地址最終會讓我們的健康檢查再次失敗。
但同樣,there's幫助:Ansible有一個很好的splitlines功能(這是稍微未記錄的...)我們只是有一個尾隨[0]
做到這一點,以獲得IP:
"{{ win_shell_txt_return.stdout.splitlines()[0] }}"
現在我們能夠做我們的健康檢查,因爲我們想要擺在首位。這是完整的解決方案:
- name: Obtain the Docker Container´s internal IP address (because localhost doesn´t work for now https://github.com/docker/for-win/issues/458)
win_shell: "docker inspect -f {% raw %}'{{ .NetworkSettings.Networks.nat.IPAddress }}' {% endraw %} {{spring_boot_app_name}} {{ '>' }} container_ip.txt"
- name: Get the Docker Container´s internal IP address from the temporary txt-file (we have to do this because of templating problems, see https://stackoverflow.com/a/32279729/4964553)
win_shell: cat container_ip.txt
register: win_shell_txt_return
- name: Define the IP as variable
set_fact:
docker_container_ip: "{{ win_shell_txt_return.stdout.splitlines()[0] }}"
- debug:
msg: "Your Docker Container has the internal IP {{ docker_container_ip }} --> Let´s do a health-check against this URI: 'http://{{ docker_container_ip }}:{{spring_boot_app.port}}/{{spring_boot_app.health_url_ending}}'"
- name: Wait until our Spring Boot app is up & running
win_uri:
url: "http://{{ docker_container_ip }}:8080/health"
method: GET
register: health_result
until: health_result.status_code == 200
retries: 5
delay: 5