On Thu, Jan 23, 2020 at 06:59:53AM GMT, Vaarlion wrote:
>
> Hi ;)
> I'm not running salt-cloud, but instead i'm doing manual template
> deployment from vmware template.
> i found out that i needed to do some cleanup after a clone, so i havea
> script who run at the first boot. Maybe you could find a way to put it in
> salt-cloud process
>
Hi Vaarlion,
If you don't mind, a couple of tips regarding the below ;^)
> rename_host() {
>
> echo "Type new hostname :"
This is purely for aesthetics - if you'd like the read to happen
on the same line, use printf(1) instead of echo(1).
> read newhostname
>
> oldhostname=$(hostname)
hostname(1) behaves differently depending on the OS, i.e. on Linux
it displays *only* the short _hostname_ part while on the macOS and
the *BSDs it prints the full name - the FQDN. Not an issue if you
only use Linux but if you'd like to make sure it is *only* the
_hostname_ part that you'd like to display, regardless of the OS,
use '-s'.
> for file in \
> /etc/hostname \
> /etc/hosts \
> /etc/motd \
> /etc/postfix/
main.cf \
> /etc/mailname \
^^
This is redundant before 'do'.
>
> do
> [ -f $file ] && sed -i.old -e "s:$oldhostname:$newhostname:g" $file
> done
> hostname "$newhostname"
> HOSTNAME=$newhostname
> }
Unless you control your hostnames very tightly, i.e. they aren't
parts of longer words, then the above can cause issues. I'm looking
at Postfix's postconf(5)/
main.cf specifically.
Also, some of the above files (should) use a short hostname while
others use FQDN.
> reset_ssh-hostkey() {
>
> rm -f /etc/ssh/ssh_host_*
> dpkg-reconfigure openssh-server
> }
There's no need to run dpkg-reconfigure if all you need is to
generate new keys and restart the ssh daemon:
ssh-keygen -A && service ssh restart
This also has the benefit of working on Debian-derived Linux
distributions, as well as the ones with roots in Red Hat.
> reset_machine-id() {
>
> rm -f /etc/machine-id
> rm -f /var/lib/dbus/machine-id
> dbus-uuidgen --ensure=/etc/machine-id
> dbus-uuidgen --ensure
> }
>
> reset_salt() {
>
> rm -f /etc/salt/minion_id
> salt-call test.ping >/dev/null 2>&1
> }
>
> I hope i'm not to much out of topic
>
In general, certain steps can be omitted, i.e. files which are
auto-generated at boot or service start, i.e. /etc/salt/minion_id,
could be removed from the template altogether, while other ones
edited beforehand with an unambiguous content to be replaced, i.e.
files containing $HOSTNAME, could have THIS_IS_TO_BE_CHANGED instead
of the old hostname if edited by sed(1), etc.
I hope you'll find some of this useful :^)
Cheers,
Raf