简体   繁体   English

无法在 shell 脚本中运行 ansible 命令

[英]Can't run ansible commands inside a shell script

I can't seem to run ansible commands inside shell scripts.我似乎无法在 shell 脚本中运行 ansible 命令。 Whenever I run ansible or ansible-playbook commands, it fails with the below error:每当我运行 ansible 或 ansible-playbook 命令时,它都会失败并显示以下错误:

./check_best_host_for_vm_creation.sh: line 9: syntax error near unexpected token `ansible-playbook'

I am sure that the ansible-playbook command is correct and there is nothing wrong with it, as I am able to execute it successfully from outside the script.我确信 ansible-playbook 命令是正确的并且它没有任何问题,因为我能够从脚本外部成功执行它。

The full script is:完整的脚本是:

#!/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
~

File names are correct, as well as paths.文件名正确,路径也正确。

There were several indentation, spacing and syntax errors.有几个缩进、间距和语法错误。 I corrected to this.我纠正了这一点。 Please try and let me know if it works now.请尝试让我知道它现在是否有效。

#!/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

Regards!问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM