- name: get_groups_json
  template: dest=/tmp/groups src=groups.j2
  run_once: True
  delegate_to: 127.0.0.1

- name: copy script
  copy: src=makehosts.py dest=/tmp/makehosts.py mode=755
  delegate_to: 127.0.0.1
  run_once: True

- name: make hosts data
  shell: source /root/.bash_profile; /tmp/makehosts.py /tmp/groups {{ domain }}
  delegate_to: 127.0.0.1
  run_once: True
  sudo: true
  register: hosts_data
               
- name: write hosts file
  lineinfile:
  args:
    dest: /etc/hosts
    line: "{{ item }}"
    state: present
  sudo: true
  with_items: hosts_data.stdout_lines

- name: set hostname by sysctl
  shell: sysctl kernel.hostname="{{ ansible_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={{ ansible_hostname }}' regexp='^HOSTNAME'
  sudo: 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'
  sudo: true
  when: ansible_distribution == "Debian" and ansible_distribution_major_version == "8"

