##############################################################################
# Copyright (c) 2017 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
---
- name: make nfs dircetory
  file: "dest=/images mode=0777 state=directory"

- name: configure service
  shell: "echo 'nfs        2049/tcp' >>  /etc/services; \
          echo 'nfs        2049/udp' >>  /etc/services"

# yamllint disable rule:line-length
- name: generate ifcfg-if.tenant
  template:
    src: ifcfg-if.tenant
    dest: /etc/sysconfig/network-scripts/ifcfg-{{compu_sys_mappings["tenant"]["interface"]}}.{{compu_sys_mappings["tenant"]["vlan_tag"]}}

- name: generate ifcfg-if.storage
  template:
    src: ifcfg-if.storage
    dest: /etc/sysconfig/network-scripts/ifcfg-{{compu_sys_mappings["storage"]["interface"]}}.{{compu_sys_mappings["storage"]["vlan_tag"]}}

- name: add br-external to interface script
  lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-{{compu_sys_mappings["external"]["interface"]}}
    line: "BRIDGE=br-external"

- name: remove eth0 ip addresss
  lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-eth0
    regexp: "^IPADDR=*"
    state: absent

- name: remove eth0 netmask
  lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-eth0
    regexp: "^NETMASK=*"
    state: absent

- name: add br-mgmt to eth0 script
  lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-eth0
    line: "BRIDGE=br-mgmt"

- name: add br-vlan to interface script
  lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-{{compu_sys_mappings["storage"]["interface"]}}.{{compu_sys_mappings["storage"]["vlan_tag"]}}
    line: "BRIDGE=br-storage"

- name: add br-tenant to interface script
  lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-{{compu_sys_mappings["tenant"]["interface"]}}.{{compu_sys_mappings["tenant"]["vlan_tag"]}}
    line: "BRIDGE=br-tenant"
# yamllint enable rule:line-length

- name: generate ifcfg-br-mgmt script
  template:
    src: ifcfg-br-mgmt
    dest: /etc/sysconfig/network-scripts/

- name: generate ifcfg-br-external script
  template:
    src: ifcfg-br-external
    dest: /etc/sysconfig/network-scripts/

- name: generate ifcfg-br-storage script
  template:
    src: ifcfg-br-storage
    dest: /etc/sysconfig/network-scripts/

- name: generate ifcfg-br-tenant script
  template:
    src: ifcfg-br-tenant
    dest: /etc/sysconfig/network-scripts/

- name: restart the network
  shell: systemctl restart network

- name: Install yum packages
  yum:
    pkg: "{{item}}"
    state: "present"
  with_items:
    - nfs-utils
    - rpcbind

- name: configure NFS
  template:
    src: exports
    dest: /etc/exports

- name: exportfs
  shell: exportfs -r

- name: start nfs
  shell: |
    service rpcbind start;
    service nfs start;
    chkconfig rpcbind on;
    chkconfig nfs on;