在AWS Systems Manager自动化文档中运行Bash脚本遇类型错误求助
AWS Systems Manager自动化文档执行Bash脚本报错:
is of type String, but expected type is StringMap 我尝试使用AWS Systems Manager的自动化文档执行一段Bash脚本,但持续收到错误提示:is of type String, but expected type is StringMap,并附上了错误截图。我的自动化文档设置截图显示无法定义字符串类型,以下是按AWS要求在首尾添加单引号的脚本代码:
'#### errocodes' '#### 0 hanadb state = OK' '#### 10 hanadb state = STOPPED' '#### 20 hanadb state = WARNING' '#### 30 hanadb state = ERROR' '' '# stop hana db' 'echo "Stopping Hana DB"' 'sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop' '' '# check hana db state' 'echo "Checking if Hana DB is running."' 'HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`' 'sleep 20' 'if [[ "$HANADBSTATUS" =~ "GRAY" ]]' 'then' ' echo "Hana DB is stopped."' ' exit 10' 'else' 'i=1' 'while [[ ! "$HANADBSTATUS" =~ "GRAY" ]] && [[ "$i" -lt 11 ]]' ' do' ' echo "Warning: HANA DB is running. Checking 10 times with 20 second intervalls until script aborts. This is check $i." ' ' sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop' ' sleep 20' ' # check db state again' ' HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`' ' ((i++))' ' if [ "$i" = 10 ]' ' then' ' echo "Error: retried $i-Times. Couldnt stop DB. Exiting Script."' ' echo "Script aborts with Error 0"' ' exit 0' ' fi' ' done' ' if [[ "$HANADBSTATUS" =~ "GRAY" ]]' ' then' ' echo "Hana DB is stopped."' ' exit 10' ' fi' 'fi'
问题原因与解决方法
核心原因
这个错误是由于SSM自动化文档中参数类型不匹配导致的:某个被定义为StringMap类型的参数,你传入了普通的String值;或者你需要用String类型的参数,却被强制设置成了StringMap。
解决步骤
检查参数类型配置
找到报错对应的参数,确认其类型是否为StringMap。如果是,你需要传入键值对格式的内容(例如{"key": "value"}),而非纯字符串。调整脚本传入方式
如果无法修改参数类型(如你所说无法定义字符串类型),可以将脚本内容包装成StringMap格式,比如用固定键存储脚本:{"script_content": "你的完整脚本内容"}然后在自动化文档的执行步骤中引用该键对应的值(例如
{{ script_content }})。修正脚本格式
你当前的脚本每行都被单引号包裹,这会导致Bash执行失败。正确做法是去掉所有行的单引号,同时把转义的"替换为普通双引号,修正后的脚本如下:#### errocodes #### 0 hanadb state = OK #### 10 hanadb state = STOPPED #### 20 hanadb state = WARNING #### 30 hanadb state = ERROR # stop hana db echo "Stopping Hana DB" sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop # check hana db state echo "Checking if Hana DB is running." HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList` sleep 20 if [[ "$HANADBSTATUS" =~ "GRAY" ]] then echo "Hana DB is stopped." exit 10 else i=1 while [[ ! "$HANADBSTATUS" =~ "GRAY" ]] && [[ "$i" -lt 11 ]] do echo "Warning: HANA DB is running. Checking 10 times with 20 second intervals until script aborts. This is check $i." sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop sleep 20 # check db state again HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList` ((i++)) if [ "$i" = 10 ] then echo "Error: retried $i-Times. Couldnt stop DB. Exiting Script." echo "Script aborts with Error 0" exit 0 fi done if [[ "$HANADBSTATUS" =~ "GRAY" ]] then echo "Hana DB is stopped." exit 10 fi fi
内容的提问来源于stack exchange,提问作者Cédric Jäggi
相关产品推荐
相关产品推荐

