salt icon indicating copy to clipboard operation
salt copied to clipboard

Support automatic refreshment of minion id

Open oilbeater opened this issue 12 years ago • 10 comments

Once the salt-minion start, it will automatically allocate a minion id which default is the hostname. However when the hostname is changed, the minion id remains the same as the old hostname. The salt master can function correctly, but the old minion ids displayed in the screen may mislead the engineers.

Delete the /etc/salt/minion_id and restart salt-minion can force the minion to recreate the new minion id , but it is tedious and need repetitive and tedious work. The salt master will hold the old minion id and also displays misleading information.

I believe a command in master side to force refresh the minion id may help a lot.

oilbeater avatar Sep 28 '14 10:09 oilbeater

You should keep in mind that security is based on the minion id.

if you allow minions to change there id then they could perhaps get access to protected piller data.

Anyways I also had issues with my minion id, i found setting:

minion_id_caching: False

and a simple state file that is kinda like:

# example prob wrong
{% if opts['id'] != grains['fqdn'] %}
restart minion:
    service.restart:
        - name: salt-minion
{% endif %}

then use

salt \* state.highstate
salt-key

steverweber avatar Sep 29 '14 20:09 steverweber

Yep, we should definitely support changing a minion's ID from the master. Would be cool to have a runner which renamed the key on the master, added the new ID to the minion config, and restarted the minion.

basepi avatar Sep 30 '14 18:09 basepi

@jfindlay any update on this? We have a number of minions that were imaged from the manufacturers with identical minion ID's.

getabc avatar Aug 14 '15 05:08 getabc

@getabc, no one has worked on this that I know of. It seems like a great idea though.

jfindlay avatar Aug 14 '15 06:08 jfindlay

This is an issue when building machine images with a tool such as http://packer.io. All virtual machines derived from the image have the same hostname on first boot (i.e. packer-57029253-df7d-b062-a581-9bda7675708c) and do not adopt their true hostname (i.e. consul-master-1) which is derived at instance creation time.

ghost avatar Apr 07 '16 10:04 ghost

I have a convoluted solution. Here is a small part of it... might help.

Note you NEED dhcp and networking to be setup correctly or system will go to a reboot loop.

get_osfinger.sh

#!/bin/bash
release=`cat /etc/*release`
case "$release" in
  *"CentOS release 6"*) echo "centos_6" ;;
  *"CentOS Linux 7"*) echo "centos_7" ;;
  *"Fedora 23"*) echo "fedora_23" ;;
  *"Debian GNU/Linux 8"*) echo "debian_8" ;;
  *"Ubuntu 14"*) echo "ubuntu_14" ;;
  *"Ubuntu 16"*) echo "ubuntu_16" ;;
  *"Arch Linux"*) echo "arch" ;;
  *) echo "unknown: $OSTYPE" ;;
esac

set a cron job to run on boot during template sysprep.

sed -i '/check_hostname/d' /etc/crontab
echo "@reboot root /bin/bash ${SCRIPT_DIR}/check_hostname.sh >> /root/ks.log 2>&1" >> /etc/crontab

check_hostname.sh

#!/bin/bash
echo -e "\n# $0 \n#=======================================\n"
set -x
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
OSFINGER=$($SCRIPT_DIR/get_osfinger.sh)

function get_host_fqdn {
    # debian is looking for dhcp to provide dns option 'host-name'
    # I like the nslook method.
    # nslookup, dig, host, resolveip
    case "$OSFINGER" in
        debian_*)
            apt-get install -y dnsutils > /dev/null
            ip=`ip a | \grep 'inet ' | \grep -v '127.0.[0-256].[0-256]/' | head -n1 | tr -s ' ' | cut -d ' ' -f3 | cut -d '/' -f1`
            echo `host $ip | \grep 'name pointer' | head -n 1 | sed -r 's|^.*name pointer ||' | sed 's|\.$||'`
            ;;
        ubuntu_*)
            apt-get install -y dnsutils > /dev/null
            ip=`ip a | \grep 'inet ' | \grep -v '127.0.[0-256].[0-256]/' | head -n1 | tr -s ' ' | cut -d ' ' -f3 | cut -d '/' -f1`
            echo `host $ip | \grep 'name pointer' | head -n 1 | sed -r 's|^.*name pointer ||' | sed 's|\.$||'`
            ;;
        *)
            # centos does a good job using reverse lookup on the dhcp ip
            echo `hostname -f`
            ;;
    esac
}

function get_domain {
    # using the domain search path, find the largest domain string that matches the fqdn
    domains=`cat /etc/resolv.conf | \grep '^search' | sed 's|^search ||'`
    domain=''
    domain_len=0
    for d in $domains
    do
        t_domain=`echo "$d" | sed 's|\.$||'`
        t_len=${#t_domain}
        if test $t_len -gt $domain_len
        then
            if `echo "$1" | \grep -q "$t_domain\$"`
            then
                domain=$t_domain
                domain_len=${#t_domain}
            fi
        fi
    done
    echo $domain
}

function get_short {
    # substract domainname from fqdn
    echo "$1" | sed "s|$2\$||" | sed 's|\.$||'
}

function is_whitelisted {
    if [ 'x' == "x$1" ] ; then
        return 1
    fi
    for blacklist in 'localhost.localdomain' 'localhost.my.domain' 'localhost'
    do
        if [ "$blacklist" == "$1" ] ; then
            return 1
        fi
    done
    return 0
}


host_fqdn="$(get_host_fqdn)"
host_domain=`get_domain "$host_fqdn"`
host_short=`get_short "$host_fqdn" "$host_domain"`
echo "get_domain: '$host_domain'"
echo "get_host_fqdn: '$host_fqdn'"
echo "get_host_short '$host_short'"

if is_whitelisted $host_fqdn
then
    $SCRIPT_DIR/check_hostname_hook_passed.sh "$host_short" "$host_domain" "$host_fqdn"
else
    echo "Retry in 30 seconds.."
    sleep 30
    host_fqdn=$(get_host_fqdn)
    host_domain=`get_domain "$host_fqdn"`
    host_short=`get_short "$host_fqdn" "$host_domain"`
    echo "checking hostname 2nd try: '$host_fqdn'"
    if is_whitelisted $host_fqdn
    then
        $SCRIPT_DIR/check_hostname_hook_passed.sh "$host_short" "$host_domain" "$host_fqdn"
    else
        $SCRIPT_DIR/check_hostname_hook_failed.sh "$host_short" "$host_domain" "$host_fqdn"
    fi
fi

check_hostname_hook_failed.sh

cat > /etc/hosts <<\EOF2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
EOF2

# force hostname from dhcp on boot
cat > /etc/sysconfig/network <<\EOF2
NETWORKING=yes
HOSTNAME=localhost.localdomain
EOF2

hostnamectl set-hostname ""
rm /etc/hostname
unlink /etc/udev/rules.d/70-persistent-net.rules
unlink /etc/resolve.conf
rm -f /var/lib/dhcp/*.leases
rm -f /var/lib/dhclient/*.leases
rm -f /var/lib/NetworkManager/*.leases
rm -f /var/lib/NetworkManager/*.lease

reboot

check_hostname_hook_passed.sh

echo "Accepted hostname: $1 domain: $2 fqdn: $3"

# -- clear cronjob
sed -i '/check_hostname/d' /etc/crontab

# -- set the hostname file using the fqdn
# echo "$3" > /etc/hostname

# -- set the hostname using short name
echo "$1" > /etc/hostname
hostname "$1"

if which systemd-machine-id-setup ; then 
    umount /etc/machine-id
    systemd-machine-id-setup
fi

# -- (debian) ensure ssh_host files in /etc/ssh/ are created
if which dpkg-reconfigure ; then dpkg-reconfigure openssh-server; fi

# -- (vmware-guest) vmware-tools
if lscpu | grep -qo -i vmware ; then
    if which yum ; then yum install -y open-vm-tools; fi
    if which apt-get ; then apt-get -y --force-yes install open-vm-tools; fi
fi

# -- salt-minion config
mkdir -p /etc/salt/minion.d/
echo "$3" > /etc/salt/minion_id
cat > /etc/salt/minion.d/99-template.conf <<\EOF2
master: salt.math.private.xxxxxxxx.ca
minion_id_caching: True
rejected_retry: True
auth_timeout: 60
random_reauth_delay: 10
auth_tries: 2
EOF2
id: $1


# -- install salt-minion
if which yum ; then yum install -y salt-minion; fi
if which apt-get ; then apt-get install -y  --force-yes salt-minion; fi
# start/enable service
if which service; then service salt-minion start; rm /etc/init/salt-minion.override; fi
if which systemctl ; then systemctl enable salt-minion.service; fi

steverweber avatar Apr 07 '16 12:04 steverweber

in the hostname_check.sh be sure to include packer-57029253-df7d-b062-a581-9bda7675708c as a blacklist item.

steverweber avatar Apr 07 '16 12:04 steverweber

If you cant edit the template image then well that's a problem :)

steverweber avatar Apr 07 '16 12:04 steverweber

~~Using salt-minion 2017.7.2 (Nitrogen) pre-installed on a CentOS 7 VM image. Once deployed, despite wiping out the minion cache and keys on the template VM, new VMs still stubbornly use the template name, not the current hostname, unless I disable minion_id_caching. Really annoyed this band-aid resolves/causes similar issues.~~

Nevermind. I had not cleared the /etc/salt/minion_id file. Which, to be fair, is not referenced in the docs.

jantonacci avatar Jan 04 '18 17:01 jantonacci

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Apr 29 '19 19:04 stale[bot]