---
- name:
  become: yes
  hostname:
    name: "{{ inventory_hostname }}"

- name: add hostname to /etc/hosts
  become: yes
  template:
    src: etc/hosts.j2
    dest: /etc/hosts
    owner: root
    group: root
    mode: 0644
  when:
    - os_family is not 'Darwin'

---
- name:
  become: yes
  hostname:
    name: "{{ inventory_hostname }}"

- name: add hostname to /etc/hosts
  become: yes
  template:
    src: etc/hosts.j2
    dest: /etc/hosts
    owner: root
    group: root
    mode: 0644

Remove when condition, host modules does not work
Remove when condition, host modules does not work
