Detecting host operating system distribution in chef-solo bash script deployment

When deploying the chef-solo setup, you need to switch between using sudo or not, for example:

bash install.sh 

and

  sudo bash install.sh 

Depending on the distribution on the main server. How can this be automated?

+8
linux bash ubuntu redhat chef-solo
source share
2 answers

ohai already fills these attributes and is easily accessible in your recipe for example

 "platform": "centos", "platform_version": "6.4", "platform_family": "rhel", 

you can refer to them as

  if node[:platform_family].include?("rhel") ... end 

To find out what other attributes ohai sets, simply enter

  ohai 

on the command line.

+20
source share

You can define the distribution on a remote host and deploy accordingly. in deploy.sh:

 DISTRO=`ssh -o 'StrictHostKeyChecking no' ${host} 'bash -s' < bootstrap.sh` 

The DISTRO variable is populated with what is encountered using the bootstrap.sh script that runs on the host machine. So, now we can use bootstrap.sh to determine the distribution parameters or other server parameters that we need and the echo that will be marked in the local script, and you can respond accordingly.

example deploy.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 

example 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 

This will allow you to discover the platform very early in the deployment process.

0
source share

All Articles