- name: install hosts file
  copy: src=files/etcHosts dest=/etc/hosts owner=root mode=644
  sudo: true

- name: set hostname by sysctl
  shell: sysctl kernel.hostname="{{ inventory_hostname }}"
  sudo: true

- name: set domainname by sysctl
  shell: sysctl kernel.domainname="{{ domain }}"
  sudo: true

- name: set /etc/sysconfig/network on CentOS 6
  lineinfile: dest=/etc/sysconfig/network line='HOSTNAME={{ inventory_hostname }}' regexp='^HOSTNAME'
  sudo: true
  when: ansible_distribution == "CentOS"

- name: set /etc/sysctl.conf on Debian 8
  lineinfile: dest=/etc/sysctl.conf line='kernel.domainname = {{ domain }}' regexp='^#kernel.domainname'
  sudo: true
  when: ansible_distribution == "Debian" and ansible_distribution_major_version == "8"

- name: set preserve hostname on CentOS
  lineinfile: dest=/etc/cloud/cloud.cfg line='preserve_hostname=True'
  sudo: true
  when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

- name: set /etc/hostname
  template: src=hostname dest=/etc/hostname
  become: true
  become_user: root
