---
- include: netplugin_firewalld.yml
  when: has_firewalld

- include: netplugin_iptables.yml
  when: has_iptables

- name: Netplugin | Ensure localhost entry correct in /etc/hosts
  lineinfile:
    dest: /etc/hosts
    regexp: '^127\.0\.0\.1.*'
    line: '127.0.0.1 localhost {{ ansible_hostname }}'
    state: present

- name: Netplugin | Remove incorrect localhost entry in /etc/hosts
  lineinfile:
    dest: /etc/hosts
    regexp: '^::1. localhost '
    line: '::1 '
    state: absent

- include: ovs.yml
  when: netplugin_driver == "ovs"

- name: Netplugin | Create Netplugin bin symlink
  file:
    src: "{{ contiv_current_release_directory }}/netplugin"
    dest: "{{ bin_dir }}/netplugin"
    state: link


- name: Netplugin | Ensure cni_bin_dir exists
  file:
    path: "{{ cni_bin_dir }}"
    recurse: yes
    state: directory

- name: Netplugin | Create CNI bin symlink
  file:
    src: "{{ contiv_current_release_directory }}/contivk8s"
    dest: "{{ cni_bin_dir }}/contivk8s"
    state: link

- name: Netplugin | Copy CNI loopback bin
  copy:
    src: "{{ cni_download_dir }}/loopback"
    dest: "{{ cni_bin_dir }}/loopback"
    remote_src: True
    mode: 0755

- name: Netplugin | Ensure kube_plugin_dir and cni/net.d directories exist
  file:
    path: "{{ item }}"
    recurse: yes
    state: directory
  with_items:
    - "{{ kube_plugin_dir }}"
    - "/etc/cni/net.d"

- name: Netplugin | Ensure contiv_config_dir exists
  file:
    path: "{{ contiv_config_dir }}"
    recurse: yes
    state: directory

- name: Netplugin | Copy contiv_cni.conf file
  copy:
    src: contiv_cni.conf
    dest: "{{ item }}"
  with_items:
    - "{{ kube_plugin_dir }}/contiv_cni.conf"
    - "/etc/cni/net.d"
# notify: restart kubelet

- name: Netplugin | Setup contiv.json config for the cni plugin
  template:
    src: contiv.cfg.j2
    dest: "{{ contiv_config_dir }}/contiv.json"
  notify: restart netplugin

- name: Netplugin | Copy environment file for netplugin
  template:
    src: netplugin.j2
    dest: /etc/default/netplugin
    mode: 0644
  notify: restart netplugin

- name: Docker | Make sure proxy setting exists
  lineinfile:
    dest: /etc/sysconfig/docker-network
    regexp: '^https_proxy.*'
    line: 'https_proxy={{ https_proxy }}'
    state: present
  register: docker_updated

- name: Netplugin | Copy systemd unit for netplugin
  template:
    src: netplugin.service
    dest: /etc/systemd/system/netplugin.service
  notify: reload systemd

- name: systemd reload
  command: systemctl daemon-reload
  when: docker_updated|changed

- name: Docker | Restart docker
  service:
    name: "{{ openshift.docker.service_name }}"
    state: restarted
  when: docker_updated|changed

- name: Netplugin | Enable Netplugin
  service:
    name: netplugin
    enabled: yes

- name: Netplugin | Start Netplugin
  service:
    name: netplugin
    state: started
  register: netplugin_started
# notify: restart kubelet