summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog2
-rw-r--r--cloudinit/CloudConfig/cc_update_etc_hosts.py41
-rw-r--r--config/cloud.cfg1
-rw-r--r--doc/examples/cloud-config.txt7
-rw-r--r--templates/hosts.tmpl17
5 files changed, 66 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 705948ab..782750fe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -19,6 +19,8 @@
- move from '#opt_include' in config file format to conf_d.
ie, now files in /etc/cloud.cfg.d/ is read rather than reading
'#opt_include <filename>' or '#include <filename>' in cloud.cfg
+ - allow /etc/hosts to be written from hosts.tmpl. which allows
+ getting local-hostname into /etc/hosts (LP: #720440)
0.6.0:
- change permissions of /var/log/cloud-init.log to accomodate
syslog writing to it (LP: #704509)
diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py
new file mode 100644
index 00000000..856cbae1
--- /dev/null
+++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py
@@ -0,0 +1,41 @@
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2011 Canonical Ltd.
+#
+# Author: Scott Moser <scott.moser@canonical.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3, as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import cloudinit.util as util
+from cloudinit.CloudConfig import per_always
+
+frequency = per_always
+
+def handle(name,cfg,cloud,log,args):
+ if not util.get_cfg_option_bool(cfg,"manage_etc_hosts",False):
+ log.debug("manage_etc_hosts is not set. not modifying /etc/hosts")
+ return
+
+ try:
+ hostname = util.get_cfg_option_str(cfg,"hostname",cloud.get_hostname())
+ if not hostname:
+ hostname = cloud.get_hostname()
+
+ if not hostname:
+ log.info("manage_etc_hosts was set, but no hostname found")
+ return
+
+ util.render_to_file('hosts', '/etc/hosts', { 'hostname' : hostname })
+
+ except Exception as e:
+ log.warn("failed to update /etc/hosts")
+ raise
diff --git a/config/cloud.cfg b/config/cloud.cfg
index d9f352e8..f8e18ac4 100644
--- a/config/cloud.cfg
+++ b/config/cloud.cfg
@@ -8,6 +8,7 @@ cloud_init_modules:
- resizefs
- set_hostname
- update_hostname
+ - update_etc_hosts
- rsyslog
cloud_config_modules:
diff --git a/doc/examples/cloud-config.txt b/doc/examples/cloud-config.txt
index 22be848d..c1d0b278 100644
--- a/doc/examples/cloud-config.txt
+++ b/doc/examples/cloud-config.txt
@@ -402,4 +402,9 @@ ssh_pwauth: True
# this option to 'True', and maintain (remove) that link before the image
# will be booted as a new instance.
# default is False
-manual_cache_clean = False
+manual_cache_clean: False
+
+# if you wish to have /etc/hosts written from /etc/cloud/templates/hosts.tmpl
+# on a per-always basis (to account for ebs stop/start), then set
+# manage_etc_hosts to True. The default is 'False'
+manage_etc_hosts: False
diff --git a/templates/hosts.tmpl b/templates/hosts.tmpl
index 642e7a7e..36db43b5 100644
--- a/templates/hosts.tmpl
+++ b/templates/hosts.tmpl
@@ -1,5 +1,20 @@
-127.0.0.1 localhost
+## This file (/etc/cloud/templates/hosts.tmpl) is only utilized
+## if enabled in cloud-config. Specifically, in order to enable it
+## you need to add the following to config:
+## manage_etc_hosts: True
+##
+## Note, double-hash commented lines will not appear in /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.tmpl
+# b.) change or remove the value of 'manage_etc_hosts' in
+# /etc/cloud/cloud.cfg or cloud-config from user-data
+#
+## The value '$hostname' will be replaced with the local-hostname
127.0.1.1 $hostname
+127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback