Skip to content

Instantly share code, notes, and snippets.

@sethlyons
Created May 7, 2020 03:15
Show Gist options
  • Save sethlyons/512d8ca71c13c91a75c49598ec8f436b to your computer and use it in GitHub Desktop.
Save sethlyons/512d8ca71c13c91a75c49598ec8f436b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# 1. collect existing hostgroups with `ipa hostgroup-find > <file>
# 2. copy <file> to new IPA server (with new REALM)
# 3. run `import_hostgroups.sh <file>` on new IPA server after running
# `ipa-client-install --uninstall` to remove the existing IPA
# config, and then running `ipa-client-install <new IPA info>` to
# enroll the client in the new IPA server.
progname=${0##*/}
file=$1
if ! [[ -f $file ]]; then
echo "$progname: $file does not exist" >&2
exit 1
fi
if [[ $2 == "-d" ]]; then
debug="echo"
fi
RE_NAME="Host-group: (.*)"
RE_DESC="Description: (.*)"
RE_MEMBER_GROUPS="Member host-groups: (.*)"
RE_MEMBERS="Member hosts: (.*)"
# create groups if they don't exist
name=""
desc=""
while read line; do
if [[ $line =~ $RE_NAME ]]; then
if [[ -n $name ]]; then
if ! ipa hostgroup-show $name >/dev/null 2>&1; then
$debug ipa hostgroup-add $name --desc="$desc"
fi
fi
name=${BASH_REMATCH[1]}
fi
if [[ $line =~ $RE_DESC ]]; then
desc=${BASH_REMATCH[1]}
fi
done < $file
if ! ipa hostgroup-show $name >/dev/null 2>&1; then
$debug ipa hostgroup-add $name --desc="$desc"
fi
name=""
# add hosts to groups
while read line; do
if [[ $line =~ $RE_NAME ]]; then
if [[ -n $name ]]; then
$debug ipa hostgroup-add-member $name --hosts="$hosts" --hostgroups="$hostgroups"
fi
name=${BASH_REMATCH[1]}
fi
if [[ $line =~ $RE_MEMBERS ]]; then
hosts=${BASH_REMATCH[1]}
fi
if [[ $line =~ $RE_MEMBER_GROUPS ]]; then
hostgroups=${BASH_REMATCH[1]}
fi
done < $file
$debug ipa hostgroup-add-member $name --hosts="$hosts" --hostgroups="$hostgroups"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment