---

- name: shadowsocks-libev deployment for ubuntu/debian
  
  hosts: vps

  gather_facts: yes

  vars:
    dns_addr: 8.8.8.8
    method: chacha20
    password: '******'
    port: 8388

  tasks:

    - fail: msg='ensure os is debian/ubuntu'
      when: 'ansible_distribution not in ["Debian", "Ubuntu"]'

    - name: ensure docker is installed
      shell: 'curl -sSL https://get.docker.com | sh'
      args:
        creates: /usr/bin/docker

    - name: ensure docker is running
      service:
        name: docker
        state: started

    - name: ensure pip is installed
      shell: 'curl -sSL https://bootstrap.pypa.io/get-pip.py | python2'
      args:
        creates: /usr/local/bin/pip2

    - name: ensure docker-py is installed
      pip:
        name: docker-py
        executable: pip2
        state: present

    - name: ensure shadowsocks is started
      docker:
        name: shadowsocks_shadowsocks_1
        image: vimagick/shadowsocks-libev
        ports:
          - "{{port}}:8388"
        env:
          DNS_ADDR: "{{dns_addr}}"
          METHOD: "{{method}}"
          PASSWORD: "{{password}}"
        restart_policy: always
        state: started

    - name: wait for shadowsocks
      wait_for:
        host: "{{ansible_ssh_host|d(inventory_hostname)}}"
        port: "{{port}}"
        delay: 5

