免费的Ansible / Puppet / Chef替代品

发布于 2025-01-18 17:09:49 字数 3797 浏览 3 评论 0原文

这是关于您对我写的脚本的看法的问题。

在我的工作场所,我写了一个bash-script,以在Linux-Systems(RedHat)上执行命令。由于我们获得了一个木偶,木偶没有人在剧本中被融入。是真的:如果您有可能或木偶,并且您在团队中工作,则不建议使用这种解决方案...但是(这就是为什么我想在这里发布它并要求您对此有意见/改进)如果您只想管理一些Linux主机,而不想购买任何许可证等。此脚本可能会为您提供帮助。

该脚本称为“旋转器”,它的工作原理:

它需要服务器列表和功能文件。它将服务器列表分为给定的最大数量TMP列表,并在后台执行功能文件中的命令。然后,它等待所有并行过程完成。最后,它检查是否访问了所有主机。

代码

\#!/bin/bash

usage ()
{
echo ""
echo ""
echo ""
echo "ERROR: $1"
echo ""
echo "You need to provide a serverlist and a"
echo "function file as parameter."
echo "e.g.: sh check_rpm.sh \<server.list\> \<funktions-file.sh\>"
echo "note that the master pushes the config to the client mashines"
echo ""
echo ""
exit 1
}

if \[\[ $# -eq 0 || "$#" == "-h" || "$#" == "--help" || "$#" == "-?" \]\]
then
usage "missing parameter"
exit 1
fi

\######################

# Variablendeklaration

\######################

basedir=$(dirname $0)
hosts=$(cat $1)
funcfile=$2
myhostname=$(hostname -a)
maxlists=100
i=0
j=0
templist=${basedir}/tmp/templist${j}

num=$(cat $1 | wc -l)
length=0
divisor=1

\###################################

# determine num of temp-lists

\###################################

if \[\[ num -gt $maxlists \]\]
then
length=$((num/$maxlists))
((length++))
else
length=1
fi

\######################################

# delete existing old temp lists

\######################################

if \[ ! -z "$(ls -A tmp)" \];
then
rm ./tmp/\*
fi

\######################

# create temp lists

\######################

echo "cutting hostlist"

for host in $hosts;
do
if \[\[ ! "${host^^}" == "${myhostname^^}" \]\]
then
echo $host \>\> $templist
((i++))
fi

if \[\[ $i -eq $length \]\]
then
((j++))
templist=${basedir}/tmp/templist${j}
i=0
fi
done

if \[\[ $j -eq 0 \]\]
then
j=1
fi

\##################################################

# start func-file for all lists and remember PID

\##################################################

k=0
for ((i=0;i\<$j;i++));
do
sleep 0.2
sh $funcfile ${basedir}/tmp/templist${i} &
pids\[${k}\]=$!
((k++))
done

\######################################

# wait till all processes terminate

\######################################

echo "waiting till all processes are done"

for pid in ${pids\[\*\]};
do
wait $pid
done

\#######################

# cleanup temp lists

\#######################

for ((i=0;i\<$j;i++));
do
rm -f ${basedir}/tmp/templist${i}
done

\##########################################

# determine if all hosts were visited

\##########################################

echo "determine if all hosts were visited..."

checkhosts=$(cat tmp/check.list)
done=false
absenthosts=""

for host in $hosts
do
for server in $checkhosts
do
host=$(echo ${host^^})
server=$(echo ${server^^})
if \[\[ "$host" == "$server" \]\]
then
done=true
fi
done

if \[\[ "$done" == "false" \]\]
then
absenthosts+="$host "
else
done=false
fi
done

rm tmp/check.list

\############################################

# tell user which hosts were not visited

\############################################

echo "the following hosts werent visited and need to be handled manually"
echo ""

for host in $absenthosts
do
echo "$host"

done

**# END OF SCRIPT**

旋转器

\#!/bin/bash

# 

# 

# 

# ================||

# PLEASE BE AWARE ||

# ================||

# Add the code, which should be executed within the marked space

# instead of using "ssh" use $SSHOPTS

# 

# 

hosts=$(cat $1)
SSHOPTS="ssh -q -o BatchMode=yes -o ConnectTimeout=10"

for host in $hosts
do
$SSHOPTS $host "hostname" \>\> /opt/c2tools/serverliste_kontrollskripten/rotator/tmp/check.list

# 

# MARK: add code after this mark

# 

# 

# MARK: add code before this mark

# 

done

**# END OF SCRIPT**

。您可以用bash脚本语言编写功能文件。

请分享您的意见

,但可以改进

This is a question about your opinion on a script I wrote.

At my workplace I wrote a bash-script to execute commands on Linux-Systems (redhat). Since we got Ansible and Puppet nobody was interessted in the script. And to be true: if you got Ansible or Puppet and you are working in a team, it isnt advised to use such a solution... BUT (and thats why I want to post it here and ask for your opinion/improvements on it) if you just want to manage a few Linux hosts and dont want to buy any licence etc. this script may help you out.

the script is called "rotator" and it works like this:

It takes a serverlist and a functionfile. It breaks the Serverlist up into a given maximum number of tmp-lists and executes the commands in the functionfile in the background. Then it waits for all the parallel processes to finish. At the end it checks if all the hosts were visited.

code of rotator.sh:

\#!/bin/bash

usage ()
{
echo ""
echo ""
echo ""
echo "ERROR: $1"
echo ""
echo "You need to provide a serverlist and a"
echo "function file as parameter."
echo "e.g.: sh check_rpm.sh \<server.list\> \<funktions-file.sh\>"
echo "note that the master pushes the config to the client mashines"
echo ""
echo ""
exit 1
}

if \[\[ $# -eq 0 || "$#" == "-h" || "$#" == "--help" || "$#" == "-?" \]\]
then
usage "missing parameter"
exit 1
fi

\######################

# Variablendeklaration

\######################

basedir=$(dirname $0)
hosts=$(cat $1)
funcfile=$2
myhostname=$(hostname -a)
maxlists=100
i=0
j=0
templist=${basedir}/tmp/templist${j}

num=$(cat $1 | wc -l)
length=0
divisor=1

\###################################

# determine num of temp-lists

\###################################

if \[\[ num -gt $maxlists \]\]
then
length=$((num/$maxlists))
((length++))
else
length=1
fi

\######################################

# delete existing old temp lists

\######################################

if \[ ! -z "$(ls -A tmp)" \];
then
rm ./tmp/\*
fi

\######################

# create temp lists

\######################

echo "cutting hostlist"

for host in $hosts;
do
if \[\[ ! "${host^^}" == "${myhostname^^}" \]\]
then
echo $host \>\> $templist
((i++))
fi

if \[\[ $i -eq $length \]\]
then
((j++))
templist=${basedir}/tmp/templist${j}
i=0
fi
done

if \[\[ $j -eq 0 \]\]
then
j=1
fi

\##################################################

# start func-file for all lists and remember PID

\##################################################

k=0
for ((i=0;i\<$j;i++));
do
sleep 0.2
sh $funcfile ${basedir}/tmp/templist${i} &
pids\[${k}\]=$!
((k++))
done

\######################################

# wait till all processes terminate

\######################################

echo "waiting till all processes are done"

for pid in ${pids\[\*\]};
do
wait $pid
done

\#######################

# cleanup temp lists

\#######################

for ((i=0;i\<$j;i++));
do
rm -f ${basedir}/tmp/templist${i}
done

\##########################################

# determine if all hosts were visited

\##########################################

echo "determine if all hosts were visited..."

checkhosts=$(cat tmp/check.list)
done=false
absenthosts=""

for host in $hosts
do
for server in $checkhosts
do
host=$(echo ${host^^})
server=$(echo ${server^^})
if \[\[ "$host" == "$server" \]\]
then
done=true
fi
done

if \[\[ "$done" == "false" \]\]
then
absenthosts+="$host "
else
done=false
fi
done

rm tmp/check.list

\############################################

# tell user which hosts were not visited

\############################################

echo "the following hosts werent visited and need to be handled manually"
echo ""

for host in $absenthosts
do
echo "$host"

done

**# END OF SCRIPT**

Here is an example of a function-file:

\#!/bin/bash

# 

# 

# 

# ================||

# PLEASE BE AWARE ||

# ================||

# Add the code, which should be executed within the marked space

# instead of using "ssh" use $SSHOPTS

# 

# 

hosts=$(cat $1)
SSHOPTS="ssh -q -o BatchMode=yes -o ConnectTimeout=10"

for host in $hosts
do
$SSHOPTS $host "hostname" \>\> /opt/c2tools/serverliste_kontrollskripten/rotator/tmp/check.list

# 

# MARK: add code after this mark

# 

# 

# MARK: add code before this mark

# 

done

**# END OF SCRIPT**

I would say, that if you combine the rotator with cron-job, you could more or less use it as an free alternative to puppet or ansible, with the bonus that you can write the function-files in the bash scripting language.

Please share your opinion

The script runs, but could be improved

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文