title: Set hostname on CentOS using cloud-init
body: <p>I want to use generic configuration in my cloud config file, to manually set the hostname for the deployed OS. cloud-init's &quot;hostname&quot; works just fine for ubuntu images, but cant get it to work for Centos cloud images(CentOS 7.8) to be specific. I see the entries are being made in /etc/hosts file, but 'hostname' command still returns 'localhost.localdomain'</p>
  <p>Here is my cloud-config file:</p>
  <pre><code>preserve_hostname: true
  hostname: worker1
  fqdn: worker1.local
  manage_etc_hosts: true
  </code></pre>
  <p>Here is the output of 'hostname' command:</p>
  <pre><code>[root@localhost ~]# hostname
  localhost.localdomain
  </code></pre>
  <p>Here are the contents of /etc/hosts:</p>
  <pre><code>[root@localhost ~]# cat /etc/hosts
  # Your system has configured 'manage_etc_hosts' as True.
  # As a result, if you wish for changes to this file to persist
  # then you will need to either
  # a.) make changes to the master file in /etc/cloud/templates/hosts.redhat.tmpl
  # b.) change or remove the value of 'manage_etc_hosts' in
  #     /etc/cloud/cloud.cfg or cloud-config from user-data
  #
  # The following lines are desirable for IPv4 capable hosts
  127.0.0.1 worker1.local worker1
  127.0.0.1 localhost.localdomain localhost
  127.0.0.1 localhost4.localdomain4 localhost4
  </code></pre>
  <p>I do not want to take the approach of 'hostnamectl' for setting hostname, as it will not be applicable to ubuntu/other OS.</p>
  <p>So how can we achieve this using cloud-init?</p>
  <p>[Version Info:
  CentOS: 7.8.2003
  cloud-init: 18.5 ]</p>