---
- apt_key:
    keyserver: pool.sks-keyservers.net
    id: A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89

- apt_repository:
    repo: deb https://deb.torproject.org/torproject.org stretch main
    state: present

- name: Install tor packages
  apt:
    name: "{{ item }}"
  with_items:
    - deb.torproject.org-keyring
    - python-torctl
    - tor
    - torsocks
    - tor-arm

- name: setup torsocks to accept inbound connections
  lineinfile:
    path: /etc/tor/torsocks.conf
    line: "AllowInbound 1"

- name: enable tor service
  command: systemctl enable tor
  # workaround for systemd module expecting running systemd
  #systemd:
  #  name: tor
  #  enabled: yes
  #  masked: no

---
- apt_key:
    keyserver: pool.sks-keyservers.net
    id: A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89

- apt_repository:
    repo: deb https://deb.torproject.org/torproject.org stretch main
    state: present

- name: Install tor packages
  apt:
    name: "{{ item }}"
  with_items:
    - deb.torproject.org-keyring
    - python-torctl
    - tor
    - torsocks
    - tor-arm

- name: setup torsocks to accept inbound connections
  lineinfile:
    path: /etc/tor/torsocks.conf
    line: "AllowInbound 1"

- name: enable tor service
  systemd:
    name: tor
    enabled: yes
    masked: no
    state: started
  when: not dockerenv.stat.exists

Use the systemd module to enable services
ansible/tor: Use the systemd module to enable services
