ansible/roles/ssh_and_vagrant/tasks/ssh-port.yml

39 lines
938 B
YAML
Raw Normal View History

---
- name: Check if SSH port is already configured
wait_for:
port: 19022
host: "{{ ansible_host }}"
2022-11-13 21:03:45 +00:00
search_regex: "OpenSSH"
connect_timeout: 5
timeout: 10
become: false
delegate_to: localhost
ignore_errors: true
register: ssh_configured
# If running in Vagrant, ansible_port is always 2222
- name: Change Ansible port to 22 if needed
set_fact:
ansible_port: 22
when: ssh_configured is defined and
2022-11-13 23:13:24 +00:00
ssh_configured.state is undefined and
ansible_port != 2222
2022-11-13 21:03:45 +00:00
- name: Change SSH port on host
lineinfile:
dest: "/etc/ssh/sshd_config"
regexp: "^#?Port "
line: "Port 19022"
register: ssh_changed
notify: "Restart sshd"
when: ssh_configured is defined and
2022-11-13 23:13:24 +00:00
ssh_configured.state is undefined
- name: Ensure sshd is reloaded if needed
meta: flush_handlers
- name: Change Ansible port to 19022
set_fact:
ansible_port: 19022
when: ssh_changed is defined