<commit_before>---
# Meta handler to bounce tor service sanely. If the SSH connection
# for the remote host is over Tor (i.e. host ends in `.onion`), then
# bounce the tor service via fire-and-forget, wait, then reestablish
# the connection after polling for the service to come back up.

# Registering a concise variable for use in conditionals, essentially
# deciding "Are we connected via SSH over Tor or not?"
- name: Register host name to wait for.
  set_fact:
    _hostname_to_wait_for: "{{ remote_host_ref|default(ansible_host) }}"

# If we're not connected over Tor, bounce the service as usual.
- name: restart tor (simple)
  service:
    name: tor
    state: restarted
  when: not _hostname_to_wait_for.endswith('.onion')

# As of Ansible v2.2, the `service` module is not compatible with the `async`
# parameter. This was changed in 2.3.
- name: restart tor (async)
  shell: sleep 5 && service tor restart
  async: 3000
  poll: 0
  when: _hostname_to_wait_for.endswith('.onion')

# It's critical that we eliminate existing SSH connections, otherwise Ansible
# will try to reuse the stale ControlPersist files, which no longer have an
# active socket, causing a connection timeout, and halting execution of the
# playbook with a cryptic message.
- name: Clear out SSH sessions, to prevent reuse of stale ControlPersist file.
  local_action:
    module: file
    args:
      path: /home/amnesia/.ansible/cp
      state: absent
  run_once: yes
  become: no
  when: _hostname_to_wait_for.endswith('.onion')

- name: Waiting for SSH connection (slow)...
  local_action: wait_for
  args:
    host: "{{ _hostname_to_wait_for }}"
    port: "{{ ansible_port|default(22) }}"
    delay: 30
    search_regex: OpenSSH
    state: started
  become: no<commit_msg>Fix SSH connection timeout in Vagrant<commit_after>---
# Meta handler to bounce tor service sanely. If the SSH connection
# for the remote host is over Tor (i.e. host ends in `.onion`), then
# bounce the tor service via fire-and-forget, wait, then reestablish
# the connection after polling for the service to come back up.

# Registering a concise variable for use in conditionals, essentially
# deciding "Are we connected via SSH over Tor or not?"
- name: Register host name to wait for.
  set_fact:
    _hostname_to_wait_for: "{{ remote_host_ref|default(ansible_host) }}"

# If we're not connected over Tor, bounce the service as usual.
- name: restart tor (simple)
  service:
    name: tor
    state: restarted
  when: not _hostname_to_wait_for.endswith('.onion')

# As of Ansible v2.2, the `service` module is not compatible with the `async`
# parameter. This was changed in 2.3.
- name: restart tor (async)
  shell: sleep 5 && service tor restart
  async: 3000
  poll: 0
  when: _hostname_to_wait_for.endswith('.onion')

# It's critical that we eliminate existing SSH connections, otherwise Ansible
# will try to reuse the stale ControlPersist files, which no longer have an
# active socket, causing a connection timeout, and halting execution of the
# playbook with a cryptic message.
- name: Clear out SSH sessions, to prevent reuse of stale ControlPersist file.
  local_action:
    module: file
    args:
      path: /home/amnesia/.ansible/cp
      state: absent
  run_once: yes
  become: no
  when: _hostname_to_wait_for.endswith('.onion')

- name: Waiting for SSH connection (slow)...
  local_action: wait_for
  args:
    host: "{{ _hostname_to_wait_for }}"
    port: "{{ ansible_ssh_port|default(ansible_port|default(22)) }}"
    delay: 30
    search_regex: OpenSSH
    state: started
  become: no
{'fraction_non_alphanumeric': 0.08604260089686099, 'fraction_numerical': 0.007847533632286996, 'mean_word_length': 3.862397820163488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3877146', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1088, 'n_words': 440}