---

- name: create ssl cert
  include_role:
    name: certificate
  vars:
    domains: ["{{ mirror_domain }}"]
  when: 'mirror_domain is defined'

- name: install rsync
  pacman: name=rsync state=present

- name: install syncrepo script
  copy: src=syncrepo dest=/usr/local/bin/syncrepo owner=root group=root mode=0755

- name: install syncrepo units
  copy: src={{ item }} dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
  with_items:
    - syncrepo.timer
    - syncrepo.service

- name: install rsyncd config
  copy: src=rsyncd.conf dest=/etc/rsyncd.conf owner=root group=root mode=0644

- name: start and enable syncrepo units
  systemd:
    name: "{{ item }}"
    enabled: true
    state: started
    daemon_reload: true
  with_items:
    - syncrepo.timer
    - rsyncd.socket

- name: set local mirror as cachedir
  lineinfile:
    dest: /etc/pacman.conf
    insertafter: '^#CacheDir'
    regexp: '^CacheDir'
    line: 'CacheDir     = /var/cache/pacman/pkg/ /srv/ftp/pool/packages/ /srv/ftp/pool/community/'
    mode: 0644
    owner: root
    group: root

- name: make nginx log dir
  file: path=/var/log/nginx/{{ mirror_domain }} state=directory owner=root group=root mode=0755
  when: 'mirror_domain is defined'

- name: set up nginx
  template: src=nginx.d.conf.j2 dest=/etc/nginx/nginx.d/syncrepo.conf owner=root group=root mode=0644
  notify:
    - reload nginx
  when: 'mirror_domain is defined'
  tags: ['nginx']

- name: open firewall holes
  ansible.posix.firewalld: service=rsyncd permanent=true state=enabled immediate=yes
  when: configure_firewall
  tags:
    - firewall
