- name: "switch centos 7 to vault.centos.org"
  shell: |
    sed -i -e 's%mirror.centos.org%vault.centos.org%g' /etc/yum.repos.d/CentOS-Base.repo
    sed -i -e 's%^mirrorlist=%#mirrorlist=%g'          /etc/yum.repos.d/CentOS-Base.repo
    sed -i -e 's%^#baseurl=%baseurl=%g'                /etc/yum.repos.d/CentOS-Base.repo
  when: ansible_distribution_major_version is defined and ansible_distribution_major_version|int == 7

- name: "run set_admin_password"
  shell: "! test -x /omd/sites/{{site}}/bin/set_admin_password || sudo su - {{ site }} -c 'set_admin_password omd'"

- name: "stop site"
  shell: "ps -fu {{ site }} >/dev/null || omd stop {{ site }}"

- name: "check if xinetd file exists"
  stat:
    path: /opt/omd/sites/{{ site }}/etc/naemon/xinetd.conf
  register: xinetd_livestatus
- name: "increase livestatus xinetd limit, may be hit during tests otherwise"
  lineinfile:
    dest: /opt/omd/sites/{{ site }}/etc/naemon/xinetd.conf
    line: "    cps         = 10000 3"
    regexp: '^\s*cps'
  when: xinetd_livestatus.stat.exists == true

- name: "check if naemon.cfg file exists"
  stat:
    path: /opt/omd/sites/{{ site }}/etc/naemon/naemon.cfg
  register: naemon_conf
- name: reduce naemon worker
  lineinfile:
    dest: /opt/omd/sites/{{ site }}/etc/naemon/naemon.cfg
    line: "check_workers=2"
    regexp: '^check_workers='
  when: naemon_conf.stat.exists == true

- name: increase slow_page_log_threshold
  lineinfile:
    dest: /opt/omd/sites/{{ site }}/etc/thruk/thruk_local.d/test_adjustments.conf
    line: "slow_page_log_threshold = 35"
    regexp: '^slow_page_log_threshold'
    create: true

- name: lower max fcgi processes
  lineinfile:
    dest: /opt/omd/sites/{{ site }}/etc/apache/conf.d/02_fcgid.conf
    line: "MaxProcessCount 5"
    regexp: '^MaxProcessCount'

- name: increase max requests
  lineinfile:
    dest: /opt/omd/sites/{{ site }}/etc/apache/conf.d/02_fcgid.conf
    line: "MaxRequestsPerProcess 100000"
    regexp: '^MaxRequestsPerProcess'

- name: lower max fcgi processes for thruk
  lineinfile:
    dest: /opt/omd/sites/{{ site }}/etc/apache/conf.d/thruk.conf
    insertafter: "FcgidCmdOptions"
    line: "    MaxProcesses 3 \\"
    regexp: 'MaxProcesses'

- name: "create demo script"
  copy:
    dest: /usr/local/bin/demo
    mode: '0755'
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      echo "**************************"
      for IP in $(ip a | grep inet | grep -v inet6 | grep -v 127.0. | awk '{ print $2 }' | sed -e 's/\/.*/\/demo\//'); do
        echo "https://$IP"
      done
      echo "**************************"

      su - {{ site }}

- name: "pimp root bashrc"
  lineinfile:
    dest: /root/.bashrc
    line: "{{ bashrc_line }}"
    state: present
  loop_control:
    loop_var: bashrc_line
  with_items:
    - 'alias d="/usr/local/bin/demo"'
    - 'alias la="ls -la"'
    - 'alias l="ls -la"'
    - 'alias vi="vim"'
    - 'alias psg="ps -efl | grep"'
    - 'alias install_ansible_role="/thruk/t/scenarios/_common/ansible/install_role.sh"'
    - 'export HISTCONTROL="ignoredups:ignorespace:erasedups"'
    - 'export HISTSIZE=100000'
    - 'shopt -s histappend'
    - 'PROMPT_COMMAND="history -a"'
    - 'export HISTFILE=/var/tmp/.bash_history.$UID'
    - 'chmod 777 /var/tmp/.'

- name: "pimp {{ site }} user bashrc"
  lineinfile:
    dest: /omd/sites/{{ site }}/.bashrc
    line: "{{ bashrc_line }}"
    state: present
  loop_control:
    loop_var: bashrc_line
  with_items:
    - 'alias vi="vim"'
    - 'alias psg="ps -efl | grep"'
    - 'export HISTCONTROL="ignoredups:ignorespace:erasedups"'
    - 'export HISTSIZE=100000'
    - 'shopt -s histappend'
    - 'PROMPT_COMMAND="history -a"'
    - 'export HISTFILE=/var/tmp/.bash_history.$UID'
