---
- name: "make /etc/rc.local"
  template:
    src: "rc.local"
    dest: "/etc/rc.local"
    owner: root
    group: root
    mode: 0755

- name: "make systemd service"
  template:
    src: "rc-local.service"
    dest: "/etc/systemd/system/rc-local.service"
    owner: root
    group: root
    mode: 0755

- name: "reload systemd"
  command: systemctl daemon-reload

- name: add to startup
  service:
    name: "rc-local"
    enabled: yes

- name: "check mysqld"
  shell: command -v mysqld >/dev/null 2>&1
  register: mysqld_result
  ignore_errors: yes
  failed_when: false

- name: "check if socket script is already inserted"
  shell: grep -c "mbstat_mysql_socket.sh" /etc/rc.local || true
  register: mysql_socket

- name: "add socket script if necessary"
  lineinfile:
    dest: /etc/rc.local
    line: "/bin/bash {{ billing.dirs.contrib }}/mbstat/scripts/mbstat_mysql_socket.sh &"
  when:
    - mysqld_result.rc == 0
    - mysql_socket.stdout == "0"

- name: "chmod rc.local"
  file: dest=/etc/rc.local mode=a+x
