--- - name: Check if SSH port is already configured wait_for: port: 19022 host: "{{ ansible_host }}" connect_timeout: 5 timeout: 10 become: false delegate_to: localhost ignore_errors: true register: ssh_configured # If we're running in Vagrant, ansible_port is 2222 - name: Change Ansible port to 22 if needed set_fact: ansible_port: 22 when: ssh_configured is defined and (ssh_configured.state is undefined or (ssh_configured.state is defined and ssh_configured.state != "started")) and ansible_port != 2222 - name: Change SSH port lineinfile: dest: "/etc/ssh/sshd_config" regexp: "^#?Port" line: "Port 19022" register: ssh_changed notify: "Restart sshd" when: ssh_configured is defined and (ssh_configured.state is undefined or (ssh_configured.state is defined and ssh_configured.state != "started")) - 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