我似乎无法在shell脚本中运行ansible命令。 每当我运行ansible或ansible-playbook命令时,它都会失败并显示以下错误:
./check_best_host_for_vm_creation.sh: line 9: syntax error near unexpected token `ansible-playbook'
我确定ansible-playbook命令正确无误,因为我可以从脚本外部成功执行该命令。
完整的脚本是:
#!/bin/bash
hostname_selected=''
for host in 10.28.187.153 10.28.143.10 do
ansible-playbook /etc/ansible/gather_vcenter_facts.yml --extra-vars "esxi_hostname=$host"
host_memory=`cat /etc/ansible/files/tmp_host_memory`
if [ "$host_memory" -eq 4000]; then
ansible-playbook /etc/ansible/create_vms_on_host.yml --extra-vars "esxi_hostname='$host'"
$hostname_selected=$host
break
fi
done
if ["$hostname_selected = '']; then
echo "No host available with free memory"
else
echo "Script done and the VM is created on host $hostname_selected "
fi
~
文件名以及路径均正确。
答案 0 :(得分:1)
存在一些缩进,间距和语法错误。我纠正了这一点。请尝试让我知道是否现在可以使用。
#!/bin/bash
hostname_selected=''
for host in '10.28.187.153' '10.28.143.10'
do
ansible-playbook /etc/ansible/gather_vcenter_facts.yml --extra-vars "esxi_hostname=$host"
host_memory=$( cat /etc/ansible/files/tmp_host_memory )
if [ "$host_memory" -eq 4000 ]
then
ansible-playbook /etc/ansible/create_vms_on_host.yml --extra-vars "esxi_hostname='$host'"
hostname_selected=$host
break
fi
done
if [ "$hostname_selected" = '' ]
then
echo "No host available with free memory"
else
echo "Script done and the VM is created on host $hostname_selected"
fi
致谢!