在chef-solo deploy bash脚本中检测主机操作系统发行版

off*_*ite 7 linux bash ubuntu redhat chef-solo

在部署chef-solo设置时,您需要在使用sudo之间切换,例如:

    bash install.sh  
Run Code Online (Sandbox Code Playgroud)

    sudo bash install.sh
Run Code Online (Sandbox Code Playgroud)

取决于主机服务器上的发行版.如何自动化?

Lit*_*mus 25

ohai已经填充了这些属性,并且在您的食谱中很容易获得,例如,

"platform": "centos",
"platform_version": "6.4",
"platform_family": "rhel",
Run Code Online (Sandbox Code Playgroud)

你可以参考这些

 if node[:platform_family].include?("rhel")
    ...
 end
Run Code Online (Sandbox Code Playgroud)

要查看ohai设置的其他属性,请输入

 ohai
Run Code Online (Sandbox Code Playgroud)

在命令行上.

  • 五年没有接受答案?真冷 (2认同)

off*_*ite 0

您可以检测远程主机上的发行版并进行相应的部署。在部署.sh 中:

DISTRO=`ssh -o 'StrictHostKeyChecking no' ${host} 'bash -s' < bootstrap.sh`
Run Code Online (Sandbox Code Playgroud)

DISTRO 变量由在主机上运行的 bootstrap.sh 脚本回显的内容填充。因此,我们现在可以使用 bootstrap.sh 来检测发行版或我们需要的任何其他服务器设置并进行回显,这将冒泡到本地脚本,您可以做出相应的响应。

示例部署.sh:

#!/bin/bash

# Usage: ./deploy.sh [host]

host="${1}"

if [ -z "$host" ]; then
    echo "Please provide a host - eg: ./deploy root@my-server.com"
    exit 1
fi

echo "deploying to ${host}"

# The host key might change when we instantiate a new VM, so
# we remove (-R) the old host key from known_hosts
ssh-keygen -R "${host#*@}" 2> /dev/null

# rough test for what distro the server is on
DISTRO=`ssh -o 'StrictHostKeyChecking no' ${host} 'bash -s' < bootstrap.sh`

if [ "$DISTRO" == "FED" ]; then
    echo "Detected a Fedora, RHEL, CentOS distro on host"

    tar cjh . | ssh -o 'StrictHostKeyChecking no' "$host" '
    rm -rf /tmp/chef &&
    mkdir /tmp/chef &&
    cd /tmp/chef &&
    tar xj &&
    bash install.sh'

elif [ "$DISTRO" == "DEB" ]; then
    echo "Detected a Debian, Ubuntu distro on host"

    tar cj . | ssh -o 'StrictHostKeyChecking no' "$host" '
    sudo rm -rf ~/chef &&
    mkdir ~/chef &&
    cd ~/chef &&
    tar xj &&
    sudo bash install.sh'
fi
Run Code Online (Sandbox Code Playgroud)

示例 bootstrap.sh:

#!/bin/bash
# Fedora/RHEL/CentOS distro
 if [ -f /etc/redhat-release ]; then
    echo "FED"
# Debian/Ubuntu
elif [ -r /lib/lsb/init-functions ]; then
    echo "DEB"
fi
Run Code Online (Sandbox Code Playgroud)

这将使您能够在部署过程的早期检测到平台。