---
- name: Setting Hostname Fact
  set_fact:
    new_hostname: "{{ custom_hostname | default(inventory_hostname_short) }}"

- name: Setting FQDN Fact
  set_fact:
    new_fqdn: "{{ new_hostname }}.{{ full_dns_domain }}"

# Ansible 2.5 fixed the hostname module for RHEL 7.5:  https://github.com/ansible/ansible/pull/31839
- name: Checking if this version of Ansible has a fix for the hostname module
  set_fact:
    hostname_module_fixed: "{{ ansible_version['full'] | version_compare('2.5', '>=') }}"

# The hostname module does not work on RHEL version 7.5 with Ansible versions < 2.5
- name: Checking if this version of RHEL is affected by the hostname problem
  set_fact:
    rhel75: "{{ (ansible_distribution == 'RedHat') and (ansible_distribution_version | version_compare('7.5', '>=')) }}"

- name: Setting hostname and DNS domain
  hostname: name="{{ new_fqdn }}"
  # Use the hostname module when not on RHEL 7.5 or on the version of Ansible that fixed the hostname module.
  when: not rhel75 or hostname_module_fixed

- name: Setting hostname and DNS domain using the command module
  command: "hostname {{ new_fqdn }}"
  # Use the command module when RHEL 7.5 or later, and when Ansible does not contain the hostname module fix.
  when: rhel75 and not hostname_module_fixed

- name: Check for cloud.cfg
  stat: path=/etc/cloud/cloud.cfg
  register: cloud_cfg

- name: Prevent cloud-init updates of hostname/fqdn (if applicable)
  lineinfile:
    dest: /etc/cloud/cloud.cfg
    state: present
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
  with_items:
    - { regexp: '^ - set_hostname', line: '# - set_hostname' }
    - { regexp: '^ - update_hostname', line: '# - update_hostname' }
  when: cloud_cfg.stat.exists == True
