I have never really tried doing what you are wanting, so I really don't know for certain if it sets the $HOSTNAME variable or not.
I did run across something in my notes from awhile back about getting the hostname in the %pre section, though. It asks for the hostname in a virtual console and set up the network (/etc/sysconfig/network) file.
Code:
%pre
chvt 6
## Query for hostname, then write it to 'network' file
read -p "
What is my hostname?
" NAME /dev/tty6 2>&1
echo "NETWORKING=yes
HOSTNAME=${NAME}" > network
chvt 1
And in the %post to actually write the file out:
Code:
%post --nochroot
# bring in hostname collected from %pre, then source it
cp network /mnt/sysimage/etc/sysconfig/network
. /mnt/sysimage/etc/sysconfig/network
# force hostname change
/mnt/sysimage/bin/hostname $HOSTNAME
# setup variables for use in /etc/hosts
LONG=`/mnt/sysimage/bin/hostname`
SHORT=`/mnt/sysimage/bin/hostname -s`
IP=`/mnt/sysimage/usr/bin/host ${SHORT} | awk {'print $4'}`
echo "${IP}\t\t${LONG} ${SHORT}" >> /mnt/sysimage/etc/hosts