我有一个脚本,该脚本在本地保管库系统中为每个主机执行凭据查找,然后为其运行一个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循环。
有人指出我正确的方向吗?
我没有任何[[“ ansibles]或”“ 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=';'
和这样的read
:
SECRET=$(/opt/vault/bin/get-admin-credential --tag=$host) IFS=';' read host login domain pwd <<< "$SECRET"