大网数据广告活动
服务器运维问答

Kickstart CentOS 7 with pre-defined static IP and hostname (%pre) and used in KS, possible?

· 52CE 我爱测

I don't know if this is possible, but going to ask anyways. I am trying to Kickstart newer servers of us with CentOS 7. So far I got most working, however for some reason if I use variables in the %pre section of my Kickstart scr…

问题原文

原帖链接 · HudsonHawk · CC BY-SA 4.0

I don't know if this is possible, but going to ask anyways.

I am trying to Kickstart newer servers of us with CentOS 7. So far I got most working, however for some reason if I use variables in the %pre section of my Kickstart script, then it does not get parsed over to the configuration at all. Therefor I am starting to think this is not even possible?

This is what I have in the %pre section of my Kickstart:

%pre --log /tmp/pre-install.log

hostname=serverA120
ip=100.110.120.130
IFS=. read ip1 ip2 ip3 ip4 <<< "$ip"

Than during the Kickstart it should use the above filled-in information:

network  --bootproto=static --device=eth0 --gateway=100.110.$ip3.1 --ip=$ip --nameserver=1.1.1.1 --netmask=255.255.255.0
network  --hostname=$hostname.example.com

And in the %post section I also try to use the variables from %pre:

sed -i'' -e '/HOSTNAME=/d' /etc/sysconfig/network
echo HOSTNAME=$hostname.example.com >> /etc/sysconfig/network
echo GATEWAY=100.110.$ip3.1 >> /etc/sysconfig/network

echo BOOTPROTO=static >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo IPADDR=$ip >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo NETMASK=255.255.255.0 >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo BROADCAST=100.110.$ip3.255 >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo NETWORK=100.110.$ip3.0 >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo GATEWAY=100.110.$ip3.1  >> /etc/sysconfig/network-scripts/ifcfg-eth0

echo $hostname.example.com >> /etc/hostname
sudo hostnamectl set-hostname $hostname.example.com

I checked the ifcfg-eth0 physically after I couldn't reach the server anymore and it showed the following:

IPADDR=
BROADCAST=100.110..255
NETWORK=100.110..0
GATEWAY=100.110..1

I also checked /etc/hostname:

$hostname.example.com

So variables are not being parsed into the Kickstart. Am I doing something wrong or is it simply not possible? And is there an alternative solution perhaps for this?

Ofcourse I can manually adjust all lines manually before and after, but I wanted to make this as easy as possible without editing everything manually. In other words, I just want to fill in 2 lines and that rest gets configured as I want. This would make things easier with Kickstarting multiple servers on the long run.

I have no clue what I am doing wrong, but checking ifcfg-eth0 and hostname, it clearly shows the pre-entered variables are not being used? But like I said above, maybe it's simply not possible, that or I am using it incorrectly.

And no I don't want to use DHCP or similar. Because I still have to change it manually afterwards. I just want to fill in the first two lines in %pre (hostname and ip) and that it gets used automatically during the complete installation (and in %post).

已采纳解答

原帖链接 · MatteoBee · CC BY-SA 4.0

You can't use variables from one block with another one as they are separate scripts executed at different points in time; These code sections will be split and placed into single separate files and then executed (not even) in the order you write them, meaning that if you have 3 %pre sections, they won't get necessarily be executed in order, so keep that into account. Also, you cannot use these variables with any other section of the kickstart.

One idea would be to use satellite files where to keep your data (like you write a /tmp/file in your %pre and then get it back in your %post... that you can do)

I went a different way, I literally have created a full bait-and-switch for anaconda that allows me to place a fake kickstart on disk, and then run a script in place to substitute variables in the likes of %%IP%% or %%HOSTNAME%% with a sed.

Anyway you don't need to do any of this, you can just place the setting of ip1 ip2 ip3 ip4 at the top of your %post block, this way you're gonna have the variables set.

then you can simply write your files as you were doing before in your post block.

Skip the "network" lines in the kickstart and just use network --activate [--device=DEVICENAME] (Add --device if you have multiple and want to just use 1)

This below is just FYI:

when using %pre %post you don't have access to BASH, it's just Bourne shell.

the kickstart allows you to use "--interpreter=/bin/bash" in your %pre/%post line but after extensive testing in the past I've found it to be wonky and not working all the time, so usually I add the shebang right at the first line of the block.

    %post --log=/root/post.log
    #!/bin/bash
    ##
    [... code ...]
    %end

this should allow you to use BASH instead of Bourne shell.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。