# This is a utility play to set a hostname
# on a server

- name: Set hostname
  hosts: all
  become: True
  gather_facts: False
  tasks:
    - name: "Ensure we have a hostname"
      fail: msg="you must pass a hostname_fqdn var into this play"
      when: hostname_fqdn is not defined
    - name: Set hostname
      hostname: name={{ hostname_fqdn.split('.')[0] }}
    - name: Update /etc/hosts
      lineinfile:
        dest: /etc/hosts
        regexp: "^127\\.0\\.1\\.1"
        line: "127.0.1.1{{ '\t' }}{{ hostname_fqdn.split('.')[0] }}{{ '\t' }}{{ hostname_fqdn }}{{ '\t' }}localhost"
        state: present
