Vis*_*ity 10 scripting centos sshd ansible
这已经是一个令人恼火的问题,现在我想我最终会向整个社区询问可能的解决方案是什么。更令人恼火的是,我似乎是唯一遇到此问题的人。
本质上,任何时候在 CentOS 7.x、sshd 配置或 sshd 的任何部分被修改,守护进程在接下来的 3 分钟内在某个“随机点”重新启动/重新加载,ssh 连接全部重置,然后该服务器是通过 ssh 几秒钟无法访问。
这对于 ansible 来说尤其是一个问题,因为它有时需要自己对 sshd 进行这些更改,并且还需要重新加载它(例如在新的 CentOS 7x 服务器构建中)。但是在将来播放时,它只是随机无法连接到 ssh,并且它会炸毁该主机的其余剧本/播放,但未能联系到该主机。这对于大型主机模式尤其不利,因为其中一些会随机完成,但在 sshd 被操纵后,其他的会在剧本的各个阶段失败。值得注意的是,在 CentOS 5x、6x 甚至 Solaris 上都不会发生这种情况。
为了避免这种情况,我能做的最好的事情是在对 sshd 进行任何更改后创建 90 秒的等待时间,即使这也不是完全万无一失的。如果调用 7-8 次,它会使这些剧本需要 20 多分钟才能运行。
以下是有关此环境的一些事实:
所有新安装都来自官方 ISO DVD。每台服务器都是 hyper-v 2012 来宾每台有这个问题的服务器都是 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}]}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
以下处理程序:
- name: sshd reload Linux 7x
systemd:
state: restarted
daemon_reload: yes
name: sshd
Run Code Online (Sandbox Code Playgroud)
最后我的贫民窟修复尝试解决这个问题:
- 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")
Run Code Online (Sandbox Code Playgroud)
必须有比我想出的更好的解决方案,很难相信其他人都遇到过这种情况并且也能忍受。我需要在 CentOS 7.x 服务器中配置什么来防止这种情况发生吗?是否需要在 ansible 中处理此问题,例如在第一次失败时每次播放多次 ssh 尝试?
提前致谢!
这似乎是一个常见问题。\n 2016 年起的 Ansible ssh 重试补丁
\n\n更好的解决方案可能是等待 sshd 准备好连接。\n具有此 ansible 代码解决方案的原始线程:
\n\n[VM 创建任务...]
\n\n\xc2\xa0 - 名称:等待 Kickstart 安装完成并重新启动 VM\n\xc2\xa0\xc2\xa0\xc2\xa0 local_action: wait_for host={{ vm_hostname }} port=22 延迟=30 超时=1200 状态=开始
\n\n\xc2\xa0 - 名称:现在配置虚拟机...
\n