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

- name: get hostname by sysctl
  shell: sysctl kernel.hostname | cut -f 3 -d " "
  register: sysctl_hostname
  check_mode: no
  changed_when: False

- name: set hostname by sysctl
  shell: sysctl kernel.hostname="{{ inventory_hostname }}"
  become: true
  when: not sysctl_hostname.stdout == inventory_hostname

- name: get domainname by sysctl
  shell: sysctl kernel.domainname | cut -f 3 -d " "
  register: sysctl_domainname
  check_mode: no
  changed_when: False
  become: true
  become_user: root

- name: set domainname by sysctl
  shell: sysctl kernel.domainname="{{ domain }}"
  become: true
  when: not sysctl_domainname.stdout  ==  domain 

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

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

- name: set preserve hostname on CentOS
  lineinfile: 
  args:
    dest: /etc/cloud/cloud.cfg 
    line: "preserve_hostname: True"
  become: 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
