Обновить test-playbooks/change_ssh_port_win.yml

This commit is contained in:
2025-12-05 09:08:46 +00:00
parent 01332dfd53
commit 2122824b6d

View File

@@ -2,15 +2,20 @@
- name: Change SSH Port on Windows - name: Change SSH Port on Windows
hosts: windows hosts: windows
gather_facts: no gather_facts: no
# ДОБАВИТЬ ЭТОТ БЛОК:
collections:
- ansible.windows
- community.windows
vars: vars:
new_ssh_port: 22233 new_ssh_port: 22233
sshd_config_path: 'C:\ProgramData\ssh\sshd_config' sshd_config_path: 'C:\ProgramData\ssh\sshd_config'
tasks: tasks:
# 1. Открываем порт в Windows Firewall (Defender)
# Делаем это первым делом, чтобы не потерять доступ после рестарта
- name: Allow new SSH port in Windows Firewall - name: Allow new SSH port in Windows Firewall
community.windows.win_firewall_rule: # Теперь можно писать короче, так как коллекции подключили выше
win_firewall_rule:
name: "OpenSSH-Server-Custom-Port" name: "OpenSSH-Server-Custom-Port"
localport: "{{ new_ssh_port }}" localport: "{{ new_ssh_port }}"
action: allow action: allow
@@ -20,10 +25,8 @@
state: present state: present
enabled: yes enabled: yes
# 2. Меняем порт в конфиге sshd_config
# Ищет строку "Port 22" или "#Port 22" и меняет на новый порт
- name: Update Port in sshd_config - name: Update Port in sshd_config
ansible.windows.win_lineinfile: win_lineinfile:
path: "{{ sshd_config_path }}" path: "{{ sshd_config_path }}"
regexp: '^#?Port\s+\d+' regexp: '^#?Port\s+\d+'
line: "Port {{ new_ssh_port }}" line: "Port {{ new_ssh_port }}"
@@ -32,6 +35,6 @@
handlers: handlers:
- name: Restart Windows SSH - name: Restart Windows SSH
ansible.windows.win_service: win_service:
name: sshd name: sshd
state: restarted state: restarted