Vagrant and Ansible

Showing some basic concepts of Ansible using a VM provisioned with Vagrant. For details see the Ansible and Vagrant documentation.


As little demo project I want to create an Ansible role ‘torproxy’ which installs and runs tor, listening for proxy clients on an IP which is accessible on the LAN.

A bit of background (caution: very simplistic): Tor is a network of nodes, which is used to disguise the origin of requests. You can use tor as proxy in the webbrowser. If you then access a website, the request is passed through several nodes in a practically untraceable way. Only the last node (the ‘exit node’) will access the website, requests the information and passes it back to you through the tor network again. You can also use tor to provide a service within the tor network itself. This service is run completely anonymously without revealing any information about the physical host itself. I. e. you can basically set up tor for four different purposes:

  • As internal node only to support the network anonymously.
  • As exit node to support the network. But your IP address will be visible to the outside, and recognizable as part of the tor network. Caution: This can lead to problems.
  • Running an own service within the tor network.
  • Providing a proxy to the tor network, e.g. to use with your browser.

I want to do the latter, set up a machine which provides a proxy to the tor network usable within the LAN.

Note: Alternatively you can use torbrowser which bundles a ‘security hardened’ Firefox with a tor proxy running in the back. Although more secure, I don’t really like it. I prefer to use my own browser with a separate tor proxy.

Stuff you need

Set up a VM with Vagrant

First create a directory for our project called ‘torproxy’.

Then create a Vagrant config which spins up a Debian/Stretch virtual machine using VirtualBox, with 1GB RAM and 1 CPU. Assign IP to it. Copy over the local public SSH ID so that we (and Ansible) can simply login via vagrant@ If you don’t have one, create it now with ssh-keygen!


Vagrant.configure("2") do |config| = "debian/stretch64" "private_network", ip: ""

  config.vm.provider "virtualbox" do |vb| = "torproxy_vm"
    vb.memory = 1024
    vb.cpus = 1

  config.vm.provision "shell" do |s|
    ssh_pub_key = File.readlines("#{Dir.home}/.ssh/").first.strip
    s.inline = <<-SHELL
      echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys

Create Ansible role ‘torproxy’

We could put everything in a playbook, but it’s cleaner and reusable if we create an Ansible role for our task.

Create the directory structure roles/torproxy/tasks. Then put everything we need to install tor into main.yml:


- name: Install some required packages, apt-transport-https
  become: true
    name: apt-transport-https
    update_cache: yes

- name: Install some required packages, policykit-1
  become: true
    name: policykit-1

- name: Add tor apt key
  become: true
    id: A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89

- name: Add tor apt repository
  become: true
    repo: deb stretch main

- name: Install tor
  become: true
    name: tor
    update_cache: yes

- name: Enable SOCKS proxy
  become: true
    path: /etc/tor/torrc
    regexp: '^#SocksPort 192'
    insertafter: '^#SocksPort 192'
    line: SocksPort :

- name: Allow only local network access
  become: true
     path: /etc/tor/torrc
     regexp: '^#SocksPolicy rej'
     insertafter: '^#SocksPolicy rej'
     line: SocksPolicy accept /24
    - restart tor

The last line in the task’s main.yml triggers a ‘restart tor’ handler. We still have to create that one. Create the roles/torproxy/handlers directory and add the following to the main.yml:


- name: restart tor
  become: true
    name: tor
    state: restarted

Create Ansible playbook


- hosts: torproxy_vm
    - role: torproxy
        proxy_port: 8888

Add the VM to the Ansible hosts




Run it

In the torproxy project directory

vagrant up # Spin up the VM
ansible-playbook playbook.yml # Run the tor proxy installation

Then set the proxy settings of your browser to Socks5 proxy port 8888 and enjoy the anonymity.