Cannot set hostname on Arch Linux

Bug #1705306 reported by Jon Gjengset on 2017-07-19
This bug affects 1 person
Affects Status Importance Assigned to Milestone

Bug Description

cloud-init fails to set the hostname on Arch Linux with the following error:

[CLOUDINIT][DEBUG]: Failed to set the hostname to dev1.localdomain (dev1)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/site-packages/cloudinit/config/", line 33, in handle
      cloud.distro.set_hostname(hostname, fqdn)
    File "/usr/lib/python2.7/site-packages/cloudinit/distros/", line 96, in set_hostname
      self._write_hostname(writeable_hostname, self.hostname_conf_fn)
    File "/usr/lib/python2.7/site-packages/cloudinit/distros/", line 132, in _write_hostname
      util.write_file(out_fn, conf, 0o644)
    File "/usr/lib/python2.7/site-packages/cloudinit/", line 1662, in write_file
      content = encode_text(content)
    File "/usr/lib/python2.7/site-packages/cloudinit/", line 97, in encode_text
      return text.encode(encoding)
  AttributeError: 'HostnameConf' object has no attribute 'encode'

The bug is that _write_hostname passes conf instead of str(conf) to util.write_file (compare distros/ and distros/

Related branches

Jon Gjengset (jonhoo) on 2017-07-19
description: updated
Jon Gjengset (jonhoo) wrote :
Joshua Powers (powersj) wrote :

Hi Jon! Thanks for taking the time to file this bug.

I tried to reproduce this using a lxc of arch and was unable to. Can you give me 1) the version of cloud-init you are using, 2) your cloud-config, 3) as well as your cloud-init log? Below is what I tried:

$ lxc launch images:archlinux arch
$ lxc exec arch bash
$ pacman -S cloud-init
# updated 05_logging.cfg per
# - [ *log_base, *log_file ]
- [ *log_base ]
# And my user-data:
hostname: foobar
$ cloud-init --file user-data single --name=set_hostname --frequency=always
$ hostname

Changed in cloud-init:
status: New → Incomplete
Jon Gjengset (jonhoo) wrote :
Download full text (4.2 KiB)

After doing some more digging, you also need to set

   distro: arch

in /etc/cloud/cloud.cfg, as Arch includes the upstream cloud.cfg which has ubuntu set as the distro (which uses str(conf) and thus doesn't have the issue).

I'm using the NoCloud provider with

$ cat meta-data
instance-id: iid-local01
local-hostname: arch-cloud

and nothing interesting in user-data. The execution log on the server looks like this.

$ sudo pacman -Qi cloud-init | grep Version
Version : 0.7.9-1
$ sudo cloud-init single --name=set_hostname --frequency=always
Cloud-init v. 0.7.9 running 'single' at Fri, 21 Jul 2017 17:08:18 +0000. Up 433.04 seconds.
2017-07-21 17:08:18,193 -[DEBUG]: Cloud-init v. 0.7.9 running 'single' at Fri, 21 Jul 2017 17:08:18 +0000. Up 433.04 seconds.
2017-07-21 17:08:18,194 -[DEBUG]: Using distro class <class 'cloudinit.distros.arch.Distro'>
2017-07-21 17:08:18,194 -[DEBUG]: Running module set_hostname (<module 'cloudinit.config.cc_set_hostname' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_hostname.pyc'>) with frequency always
2017-07-21 17:08:18,194 -[DEBUG]: Running config-set_hostname using lock (<cloudinit.helpers.DummyLock object at 0x7fe51cf5d690>)
2017-07-21 17:08:18,194 -[DEBUG]: Setting the hostname to arch-cloud.localdomain (arch-cloud)
2017-07-21 17:08:18,195 -[DEBUG]: Reading from /etc/hostname (quiet=False)
2017-07-21 17:08:18,195 -[WARNING]: Failed to set the hostname to arch-cloud.localdomain (arch-cloud)
2017-07-21 17:08:18,197 -[DEBUG]: Failed to set the hostname to arch-cloud.localdomain (arch-cloud)
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cloudinit/config/", line 47, in handle
    cloud.distro.set_hostname(hostname, fqdn)
  File "/usr/lib/python2.7/site-packages/cloudinit/distros/", line 84, in set_hostname
    self._write_hostname(writeable_hostname, self.hostname_conf_fn)
  File "/usr/lib/python2.7/site-packages/cloudinit/distros/", line 120, in _write_hostname
    util.write_file(out_fn, conf, 0o644)
  File "/usr/lib/python2.7/site-packages/cloudinit/", line 1747, in write_file
    content = encode_text(content)
  File "/usr/lib/python2.7/site-packages/cloudinit/", line 154, in encode_text
    return text.encode(encoding)
AttributeError: 'HostnameConf' object has no attribute 'encode'
2017-07-21 17:08:18,198 -[WARNING]: Running module set_hostname (<module 'cloudinit.config.cc_set_hostname' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_hostname.pyc'>) failed
2017-07-21 17:08:18,199 -[DEBUG]: Running module set_hostname (<module 'cloudinit.config.cc_set_hostname' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_hostname.pyc'>) failed
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cloudinit/", line 776, in _run_modules
  File "/usr/lib/python2.7/site-packages/cloudinit/", line 54, in run
    return, functor, args, freq, clear_on_fail)
  File "/usr/lib/python2.7/site-packages/cloudinit/he...


Jon Gjengset (jonhoo) wrote :

As a side-note, I have filed a bug with Arch to make them change "system_info.distro" in the bundled config:

Joshua Powers (powersj) wrote :

Thanks for the updates Jon, really appreciate it!

Changed in cloud-init:
status: Incomplete → Confirmed
Jon Gjengset (jonhoo) wrote :

Happy to help! Let me know if there's anything else I can do.
Oh, and while I have you here — could you also take a look at ?

Scott Moser (smoser) on 2017-07-24
Changed in cloud-init:
importance: Undecided → Medium
Joshua Powers (powersj) on 2017-08-25
Changed in cloud-init:
status: Confirmed → Fix Committed

This bug is believed to be fixed in cloud-init in 17.1. If this is still a problem for you, please make a comment and set the state back to New

Thank you.

Changed in cloud-init:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers