---
- name: Remove Primary Server from Known Hosts
  become: yes
  become_user: jingrow
  known_hosts:
    name: "{{ secondary_private_ip }}"
    state: absent

- name: Add Primary Server to Known Hosts
  become: yes
  become_user: jingrow
  shell: ssh-keyscan -p {{ secondary_ssh_port }} {{ secondary_private_ip }}  >> /home/jingrow/.ssh/known_hosts

- name: Install Lsyncd
  apt:
    state: present
    pkg:
      - lsyncd

- name: Create Lsyncd Directory
  become: yes
  become_user: jingrow
  file:
    dest: /home/jingrow/lsyncd
    state: directory

- name: Create Lsyncd Log and Status File
  become: yes
  become_user: jingrow
  file:
    dest: /home/jingrow/lsyncd/{{ item }}
    state: touch
  with_items:
    - lsyncd.logs
    - lsyncd.status

- name: Create Lsynd Config File
  become: yes
  become_user: jingrow
  template:
    src: lsyncd.conf
    dest: /home/jingrow/lsyncd/lsyncd.conf.lua

- name: Setup lsyncd service for jingrow user
  template:
    src: lsyncd.service
    dest: /etc/systemd/system/lsyncd.service
    owner: root
    group: root
    mode: 0644

- name: Get Number of Files in Benches Directory
  shell: find /home/jingrow/benches -type f | wc -l
  register: file_count

- name: Update Number of Maximum Watches
  sysctl:
    name: fs.inotify.max_user_watches
    value: "{{ [file_count.stdout|int, 1048576] | max }}"
    state: present

- name: Restart Lsyncd service
  systemd:
    daemon_reload: true
    name: lsyncd
    state: restarted
    enabled: True
