summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/CloudConfig/cc_update_etc_hosts.py36
-rw-r--r--doc/examples/cloud-config.txt106
2 files changed, 89 insertions, 53 deletions
diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py
index 6012b8a3..1dc9c1e9 100644
--- a/cloudinit/CloudConfig/cc_update_etc_hosts.py
+++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py
@@ -24,25 +24,29 @@ frequency = per_always
def handle(name,cfg,cloud,log,args):
( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud)
- use_template = util.get_cfg_option_bool(cfg,"manage_etc_hosts", False)
- if not use_template:
- # manage_etc_hosts not true, update the 127.0.1.1 entry via update_etc_hosts
- log.debug("manage_etc_hosts is not set, checking sanity of /etc/hosts")
+ manage_hosts = util.get_cfg_option_bool(cfg,"manage_etc_hosts", False)
+ if manage_hosts in ("True", "true", True, "template"):
+ # render from template file
+ try:
+ if not hostname:
+ log.info("manage_etc_hosts was set, but no hostname found")
+ return
+
+ util.render_to_file('hosts', '/etc/hosts', \
+ { 'hostname' : hostname, 'fqdn' : fqdn })
+ except Exception as e:
+ log.warn("failed to update /etc/hosts")
+ raise
+ elif manage_hosts == "localhost":
+ log.debug("managing 127.0.1.1 in /etc/hosts")
update_etc_hosts(hostname, fqdn, log)
return
+ else:
+ if manage_hosts not in ("False", False):
+ log.warn("Unknown value for manage_etc_hosts. Assuming False")
+ else:
+ log.debug("not managing /etc/hosts")
- # manage_etc_hosts is set, render from template file
- try:
- if not hostname:
- log.info("manage_etc_hosts was set, but no hostname found")
- return
-
- util.render_to_file('hosts', '/etc/hosts', \
- { 'hostname' : hostname, 'fqdn' : fqdn })
-
- except Exception as e:
- log.warn("failed to update /etc/hosts")
- raise
def update_etc_hosts(hostname, fqdn, log):
with open('/etc/hosts', 'r') as etchosts:
diff --git a/doc/examples/cloud-config.txt b/doc/examples/cloud-config.txt
index e73b8743..3c9e4b2e 100644
--- a/doc/examples/cloud-config.txt
+++ b/doc/examples/cloud-config.txt
@@ -341,48 +341,80 @@ rsyslog:
resize_rootfs: True
## hostname and /etc/hosts management
-# cloud-init will do its best to set up a sane hostname and corresponding
-# entries in /etc/hosts.
-#
-# if you do nothing, you should get the system generally correctly
-# configured.
-# * /etc/hostname (and thus `hostname` output) set with hostname (not fqdn)
-# * an entry in /etc/hosts for both hostname and fqdn
-# that are obtained from the metadata service
-# * On each boot, the above will again be set
-# * cloud-init generally "owns" the 127.0.1.1 entry. The
-# rest of the file will not be modified
+# cloud-init can handle updating some entries in /etc/hosts,
+# and can set your hostname for you.
+#
+# if you do nothing you'll end up with:
+# * /etc/hostname (and `hostname`) managed via: 'preserve_hostame: false'
+# if you do not change /etc/hostname, it will be updated with the cloud
+# provided hostname on each boot. If you make a change, then manual
+# maintenance takes over, and cloud-init will not modify it.
+#
+# * /etc/hosts managed via: 'manage_etc_hosts: false'
+# cloud-init will not manage /etc/hosts at all. It is in full manual
+# maintenance mode.
#
# You can change the above behavior with the following config variables:
# Remember that these can be set in cloud-config via user-data,
# /etc/cloud/cloud.cfg or any file in /etc/cloud/cloud.cfg.d/
#
-# hostname:
-# this option will be used wherever the 'hostname' is needed
-# simply substitute it in the description above.
-# ** If you wish to set your hostname, set it here **
-# default: 'hostname' as returned by the metadata service
-# on EC2, the hostname portion of 'local-hostname' is used
-# which is something like 'ip-10-244-170-199'
-#
-# fqdn:
-# this option will be used wherever 'fqdn' is needed.
-# simply substitue it in the description above.
-# default: fqdn as returned by the metadata service. on EC2 'hostname'
-# is used, so this is like: ip-10-244-170-199.ec2.internal
-#
-# manage_etc_hosts:
-# default: false
-# Setting this config variable to 'true' will mean that on every
-# boot, /etc/hosts will be re-written from /etc/cloud/templates/hosts.tmpl
-# The strings '$hostname' and '$fqdn' are replaced in the template
-# with the appropriate values.
-#
-# preserve_hostname:
-# default: False
-# If this option is set to True, then /etc/hostname will never updated
-# The default behavior is to update it if it has not been modified by
-# the user.
+# == Hostname management (via /etc/hostname) ==
+# * preserve_hostname:
+# default: False
+# If this option is set to True, then /etc/hostname will never updated
+# The default behavior is to update it if it has not been modified by
+# the user.
+#
+# * hostname:
+# this option will be used wherever the 'hostname' is needed
+# simply substitute it in the description above.
+# ** If you wish to set your hostname, set it here **
+# default: 'hostname' as returned by the metadata service
+# on EC2, the hostname portion of 'local-hostname' is used
+# which is something like 'ip-10-244-170-199'
+#
+# * fqdn:
+# this option will be used wherever 'fqdn' is needed.
+# simply substitue it in the description above.
+# default: fqdn as returned by the metadata service. on EC2 'hostname'
+# is used, so this is like: ip-10-244-170-199.ec2.internal
+#
+# == /etc/hosts management ==
+#
+# The cloud-config variable that covers management of /etc/hosts is
+# 'manage_etc_hosts'
+#
+# By default, its value is 'false' (boolean False)
+#
+# * manage_etc_hosts:
+# default: false
+#
+# false:
+# cloud-init will not modify /etc/hosts at all.
+# * Whatever is present at instance boot time will be present after boot.
+# * User changes will not be overwritten
+#
+# true or 'template':
+# on every boot, /etc/hosts will be re-written from
+# /etc/cloud/templates/hosts.tmpl.
+# The strings '$hostname' and '$fqdn' are replaced in the template
+# with the appropriate values.
+# To make modifications persistant across a reboot, you must make
+# modificatoins to /etc/cloud/templates/hosts.tmpl
+#
+# localhost:
+# This option ensures that an entry is present for fqdn as described in
+# section 5.1.2 of the debian manual
+# http://www.debian.org/doc/manuals/debian-reference/ch05.en.html
+#
+# cloud-init will generally own the 127.0.1.1 entry, and will update
+# it to the hostname and fqdn on every boot. All other entries will
+# be left as is. 'ping `hostname`' will ping 127.0.1.1
+#
+# If you want a fqdn entry with aliases other than 'hostname' to resolve
+# to a localhost interface, you'll need to use something other than
+# 127.0.1.1. For example:
+# 127.0.1.2 myhost.fqdn.example.com myhost whatup.example.com
# final_message
# default: cloud-init boot finished at $TIMESTAMP. Up $UPTIME seconds