
現在這已經是一個足夠令人惱火的問題了,我想我最終會向整個社區詢問可能的解決方案是什麼。更令人惱火的是,我似乎是唯一遇到這個問題的人。
本質上,在CentOS 7.x 中的任何時候,sshd 配置或sshd 的任何部分都會被修改,並且守護程序在接下來的3 分鐘內的某個“隨機點”重新啟動/重新加載,ssh 連接全部重置,然後該伺服器透過 ssh 幾秒鐘無法存取。
對於 ansible 來說,這尤其是一個問題,因為它有時需要自行對 sshd 進行這些更改,並重新加載它(例如在新的 CentOS 7x 伺服器版本中)。但在未來的遊戲中,它只是隨機地無法連接到 ssh,並且它會炸毀無法聯繫的主機的其餘劇本/遊戲。這對於大型主機模式來說尤其糟糕,因為其中一些會隨機完成,但其他主機會在 sshd 被操縱後在劇本的各個階段失敗。值得注意的是,這種情況在 CentOS 5x、6x 甚至 Solaris 上都不會發生。
為了避免這種情況,我能做的最好的事情就是在對 sshd 進行任何更改後創建 90 秒的等待,即使這也不是完全萬無一失的。如果呼叫 7-8 次,則這些 playbook 需要 20 分鐘以上才能運作。
以下是有關此環境的一些事實:
所有新安裝均來自官方 ISO DVD。每台伺服器都是 hyper-v 2012 guest 每台有此問題的伺服器都是 CentOS 7.x
以下是問題的一些實際輸出和一些陳腐的解決方案:
失敗:
fatal: [voltron]: UNREACHABLE! => {"changed": false, "msg": "All items completed", "results": [{"_ansible_item_result": true, "item": ["rsync", "iotop", "bind-utils", "sysstat.x86_64", "lsof"], "msg": "Failed to connect to the host via ssh: Shared connection to voltron closed.\r\n", "unreachable": true}]}
sshd 變更之一的範例:
- name: Configure sshd to disallow root logins for security purposes on CentOS and Redhat 7x servers.
lineinfile:
backup: yes
dest: /etc/ssh/sshd_config
regexp: '^(#PermitRootLogin)'
line: "PermitRootLogin no"
state: present
when: (ansible_distribution == "CentOS" or "RedHat") and (ansible_distribution_major_version == "7")
notify: sshd reload Linux 7x
以下處理程序:
- name: sshd reload Linux 7x
systemd:
state: restarted
daemon_reload: yes
name: sshd
最後我的貧民窟修復嘗試解決這個問題:
- name: Wait a bit on CentOS/Redhat 7x servers to ensure changes don't mess up ssh and screw up further plays.
pause:
seconds: 90
when: (ansible_distribution == "CentOS" or "RedHat") and (ansible_distribution_major_version == "7")
一定有比我想出的更好的解決方案,很難相信其他人都遇到過這種情況並且也能忍受。我需要在 CentOS 7.x 伺服器中設定一些東西來防止這種情況嗎? ansible 中是否需要一些東西來處理這個問題,例如在第一次失敗時每次嘗試多次 ssh ?
先致謝!
答案1
這似乎是個常見問題。 2016 年起 Ansible ssh 重試補丁
更好的解決方案可能是等待 sshd 準備好連接。 原線使用此 ansible 程式碼解決方案:
[VM 建立任務...]
- name:等待 Kickstart 安裝完成且 VM 重新啟動 local_action:wait_for host={{ vm_hostname }} port=22 delay=30 timeout=1200 state=started
- 名稱:現在設定虛擬機器...
答案2
不要使用該模組,而是systemd
嘗試該service
模組:
- name: Restart secure shell daemon post configuration
service:
name: sshd
state: restarted