我有这个脚本,它在本地保管库系统中为每个主机执行凭据查找,然后为其运行 ansible-playbook。
#!/bin/bash
for host in `cat ~/.ansible/hosts`
do
SECRET=`/opt/vault/bin/get-admin-credential --tag=$host`
HOST=`echo $SECRET | cut -d ';' -f1`
LOGIN=`echo $SECRET | cut -d ';' -f2`
DOMAIN=`echo $SECRET | cut -d ';' -f3`
PWD=`echo $SECRET | cut -d ';' -f4`
if [ -z "$DOMAIN" ]; then
ansible-playbook -i ~/.ansible/hosts ~/.ansible/windows.yml -e "ansible_host=$HOST ansible_user=$LOGIN ansible_password=$PWD" --limit $host
else
ansible-playbook -i ~/.ansible/hosts ~/.ansible/windows.yml -e "ansible_host=$HOST ansible_user=$LOGIN@$DOMAIN ansible_password=$PWD" --limit $host
fi
done
这按顺序循环每个主机,我已经尝试过 GNU 的东西parallel
但无法做我想做的事,并行运行 5 的 for 循环。
有人指出我正确的方向吗?
我没有“安西布斯” or "vaults",所以这完全未经测试,但可能会让你接近:
doit(){
host="$1"
SECRET=$(/opt/vault/bin/get-admin-credential --tag=$host)
HOST=$(echo $SECRET | cut -d ';' -f1)
LOGIN=$(echo $SECRET | cut -d ';' -f2)
DOMAIN=$(echo $SECRET | cut -d ';' -f3)
PWD=$(echo $SECRET | cut -d ';' -f4)
if [ -z "$DOMAIN" ]; then
ansible-playbook -i ~/.ansible/hosts ~/.ansible/windows.yml -e "ansible_host=$HOST ansible_user=$LOGIN ansible_password=$PWD" --limit $host
else
ansible-playbook -i ~/.ansible/hosts ~/.ansible/windows.yml -e "ansible_host=$HOST ansible_user=$LOGIN@$DOMAIN ansible_password=$PWD" --limit $host
fi
}
# Export doit function to subshells created by GNU Parallel
export -f doit
parallel -a ~/.ansible/hosts doit
从风格上来说,可能还有一些改进。首先,由大写字母组成的 shell 变量被保留,所以你不应该使用HOST
, DOMAIN
另外,您可能可以简化所有难看的剪切和回显,以通过使用IFS=';'
and a read
像这样:
SECRET=$(/opt/vault/bin/get-admin-credential --tag=$host)
IFS=';' read host login domain pwd <<< "$SECRET"
所以,我最好的也是最终的答案是:
doit(){
host="$1"
secret=$(/opt/vault/bin/get-admin-credential --tag=$host)
IFS=';' read host login domain pwd <<< "$secret"
if [ -z "$domain" ]; then
ansible-playbook -i ~/.ansible/hosts ~/.ansible/windows.yml -e "ansible_host=$host ansible_user=$login ansible_password=$pwd" --limit $host
else
ansible-playbook -i ~/.ansible/hosts ~/.ansible/windows.yml -e "ansible_host=$host ansible_user=$login@$domain ansible_password=$pwd" --limit $host
fi
}
# Export doit function to subshells created by GNU Parallel
export -f doit
parallel -a ~/.ansible/hosts doit
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)