---
- hosts: gitreceive-openbsd*
  gather_facts: false
  roles:
      - openbsd-bootstrap
      - role: users
        users: []
        users_use_sudo: True
        users_lock_root_ssh: False

- hosts: [gitreceive-xenial, gitreceive-wheezy]
  gather_facts: false
  roles:
      - debian-bootstrap
      - role: users
        users: []
        users_use_sudo: True
        users_lock_root_ssh: False

- hosts: all
  tasks:
      - name: Create SSH keypair
        become: False
        local_action: command ssh-keygen -t rsa -N '' -f files/id_rsa
        run_once: True
        args:
            creates: files/id_rsa

      - name: Install SSH server
        when: ansible_pkg_mgr == 'apt'
        apt:
            name: openssh-server
            state: present

      - name: Create sshd run directory
        when: ansible_os_family == 'Debian'
        file:
            path: /var/run/sshd
            owner: root
            group: root
            mode: 0o0755
            state: directory

      - name: Start SSH service
        when: ansible_pkg_mgr == 'apt'
        service:
            name: ssh
            state: started

- hosts: all
  roles:
      - role: gitreceive
        gitreceive_public_keys: ['{{ lookup("file", "id_rsa.pub") }}']
        gitreceive_receiver_script: '{{ lookup("file", "files/receiver.sh") }}'
  post_tasks:
      - name: Create .ssh directory
        file:
            path: /root/.ssh
            owner: root
            group: 0
            mode: 0o0700
            state: directory

      - name: Copy SSH keypair
        with_items:
            - id_rsa
            - id_rsa.pub
        copy:
            src: '{{ item }}'
            dest: '/root/.ssh/{{ item }}'
            owner: root
            group: 0
            mode: 0o0400

      - name: Add localhost host keys to known hosts
        shell: ssh-keyscan localhost > /root/.ssh/known_hosts
        args:
            creates: /root/.ssh/known_hosts

      - name: Clone gitreceive test repo
        git:
            dest: /root/gitreceive-test
            force: yes
            update: yes
            repo: https://www.shore.co.il/git/gitreceive-test
            version: master

      - name: Add localhost as a git remote
        blockinfile:
            dest: /root/gitreceive-test/.git/config
            block: |
                [remote "test"]
                    url = git@localhost:test
                    fetch = +refs/heads/*:refs/remotes/test/*
