From bdcc2ce7cf4d0562c815e5baa9188e984290df4b Mon Sep 17 00:00:00 2001 From: hLinx <327159425@qq.com> Date: Mon, 28 Nov 2022 17:33:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E7=BB=93?= =?UTF-8?q?=E5=B0=BE=E7=9A=84=E4=BD=BF=E7=94=A8=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E6=96=87=E6=A1=88=E4=B8=8D=E5=90=88=E9=80=82=E3=80=82?= =?UTF-8?q?=E8=80=8C=E4=B8=94=E4=B8=BA=E4=BB=80=E4=B9=88=E7=AA=81=E7=84=B6?= =?UTF-8?q?=E8=A6=81=E5=A4=84=E7=90=86json=EF=BC=9F=20#1323?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../variable-use-guide/magic-variable.en.md | 25 +++++++++--------- .../variable-use-guide/magic-variable.md | 26 ++++++++++--------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.en.md b/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.en.md index a2ded5c6e8..1ff8fc1b86 100644 --- a/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.en.md +++ b/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.en.md @@ -19,7 +19,7 @@ Magic Variable is a special variable that built-in specific to the JOB platform, ```bash # job_import {{host_type_var_name}} - + echo ${host_type_var_name} ``` @@ -36,10 +36,10 @@ Magic Variable is a special variable that built-in specific to the JOB platform, ```bash # job_import {{JOB_LAST_ALL}} # Get all host list of last step - + # job_import {{JOB_LAST_SUCCESS}} # Get success host list of last step - + # job_import {{JOB_LAST_FAIL}} # Get failure host list of last step ``` @@ -47,42 +47,43 @@ Magic Variable is a special variable that built-in specific to the JOB platform, The result output format is "Cloud Area + : + inner IP addr", multiple IPs are separated by commas(,) - To get value of other host's namespace variable - + ```bash # job_import {{JOB_NAMESPACE_ALL}} # Get value of all namespace variable echo ${JOB_NAMESPACE_ALL} - + # job_import {{JOB_NAMESPACE_varname}} # Get value of specific namespace variable echo ${JOB_NAMESPACE_varname} ``` Result (Example): - + ```bash ### echo ${JOB_NAMESPACE_ALL} (If there have two namespace variables: ns_var1 and ns_var2): {"ns_var1":{"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"},"ns_var2":{"0:20.20.20.1":"aaaa","0:20.20.20.2":"bbbb","0:20.20.20.3":"cccc","0:20.20.20.4":"dddd"}} - + ### echo ${JOB_NAMESPACE_ns_var1}: {"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"} ``` +# Suggestions - Two ways to processing JSON string with Shell (For reference only, it's recommended to be on-line after debug): 1. Using sed & awk : ```bash #!/bin/bash - + str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}' - + function json_parse { local _json=$1 local _key=$2 temp=`echo $_json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $_key` echo ${temp##*|} } - + json_parse $str ip1 ## Value of ip1 in $str variable, it will be "value1" json_parse $str ns1 ## Value of ns1, it will be {"ip1":"value1","ip2":"value2"} ``` @@ -90,9 +91,9 @@ Magic Variable is a special variable that built-in specific to the JOB platform, 2. Using Python's json module : ```bash #!/bin/bash - + str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}' - + ### To get value of ip2 in $str variable, it will be "value2" echo ${str} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["ns1"]["ip2"]' ``` \ No newline at end of file diff --git a/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.md b/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.md index 9cc8c1711d..43642e418a 100644 --- a/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.md +++ b/src/frontend/src/views/task-manage/common/variable-use-guide/magic-variable.md @@ -18,7 +18,7 @@ JOB平台执行引擎提供的特有的变量能力 ```bash # job_import {{主机列表的全局变量名}} - + echo ${主机列表的全局变量名} ``` @@ -35,10 +35,10 @@ JOB平台执行引擎提供的特有的变量能力 ```bash # job_import {{JOB_LAST_ALL}} # 获取上一个步骤的所有执行主机IP列表 - + # job_import {{JOB_LAST_SUCCESS}} # 获取上一个步骤执行成功的主机IP列表 - + # job_import {{JOB_LAST_FAIL}} # 获取上一个步骤执行失败的主机IP列表 ``` @@ -46,41 +46,43 @@ JOB平台执行引擎提供的特有的变量能力 输出的格式同上: `云区域ID + 冒号 + 内网IP`,多个IP地址以逗号分隔 - 获取其他主机的命名空间变量值 - + ```bash # job_import {{JOB_NAMESPACE_ALL}} # 获取所有命名空间变量的汇聚值 echo ${JOB_NAMESPACE_ALL} - + # job_import {{JOB_NAMESPACE_命名空间变量名}} # 获取某个命名空间变量的汇聚值 echo ${JOB_NAMESPACE_命名空间变量名} ``` 输出结果(示例): - + ```bash ### echo ${JOB_NAMESPACE_ALL} 的输出(假定有 ns_var1 和 ns_var2 两个命名空间类型全局变量): {"ns_var1":{"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"},"ns_var2":{"0:20.20.20.1":"aaaa","0:20.20.20.2":"bbbb","0:20.20.20.3":"cccc","0:20.20.20.4":"dddd"}} - + ### echo ${JOB_NAMESPACE_命名空间变量名} 的输出: {"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"} ``` + +# 处理建议 - Shell 脚本处理 JSON字符串的两种方式(仅供参考,正式投入使用建议调试确认后再上线): 方式一:(纯 sed + awk 实现) ```bash #!/bin/bash - + str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}' - + function json_parse { local _json=$1 local _key=$2 temp=`echo $_json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $_key` echo ${temp##*|} } - + json_parse $str ip1 ## 获取 str 的 json字符串中 ip1 的值,即:value1 json_parse $str ns1 ## 获取 str 的 json字符串中 ns1 的值,即:{"ip1":"value1","ip2":"value2"} ``` @@ -88,9 +90,9 @@ JOB平台执行引擎提供的特有的变量能力 方式二:(借助 Python 的 json 模块实现) ```bash #!/bin/bash - + str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}' - + ### 获取 str 的 json字符串中 ip2 的值,即:value2 echo ${str} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["ns1"]["ip2"]' ``` \ No newline at end of file