- name: check pip is installed
  package: name=python-pip
           state=present

- name: Install xs-authserver from pypi
  pip: name=xs-authserver
  when: internet_available | bool

- name: install gunicorn
  package: name=python-gunicorn
           state=present

- name: Configure xs-authserver
  template: backup=yes
            src={{ item.src }}
            dest={{ item.dest }}
            owner=root
            group=root
            mode={{ item.mode }}
  with_items:
    - src: xs-authserver.env.j2
      dest: /etc/sysconfig/xs-authserver
      mode: 0644
    - src: xs-authserver.service.j2
      dest: /etc/systemd/system/xs-authserver.service
      mode: 0644

- name: create database folder
  file: state=directory
        path=/var/lib/xs-authserver/
        owner=root
        group=root
        mode=0644

- name: init database
  command: xs-authserverctl initdb
  ignore_errors: yes
  environment:
    XS_AUTHSERVER_DATABASE: /var/lib/xs-authserver/data.db

- name: Stop authserver service
  service: name=xs-authserver
           state=stopped
           enabled=no
  when: not authserver_enabled

- name: Start xs-authserver service
  service: name=xs-authserver
           state=restarted
           enabled=yes
  when: authserver_enabled | bool

- name: Add 'authserver' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: authserver
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: XS-authserver
    - option: description
      value: '"authserver (xs-authserver) implements a seamless web authentication
             service using XO laptop registration capabilities.  It is heavily
             inspired by the Moodle OLPC-XS authentication plugin"'
    - option: port
      value: 5000
    - option: path
      value: /
