Bash playbook的CURL调用中缺少变量值

Bash playbook的CURL调用中缺少变量值,bash,curl,ansible,Bash,Curl,Ansible,我有一个ansible,它从文件中读入数据,然后调用需要文件中数据的bash脚本。Bash文件有一个CURL调用,使用从playbook传递到脚本的数据。 带着错误。我发现变量似乎没有正确地传递给CURL调用 文件: 剧本 - hosts: localhost vars: contents: "{{ lookup('file', 'container.id') }}" tasks: - debug: msg="the value of

我有一个ansible,它从文件中读入数据,然后调用需要文件中数据的bash脚本。Bash文件有一个CURL调用,使用从playbook传递到脚本的数据。 带着错误。我发现变量似乎没有正确地传递给CURL调用

文件:

剧本

- hosts: localhost 
  vars:
     contents: "{{ lookup('file', 'container.id') }}"
  tasks:
     - debug: msg="the value of container.id is {{ contents }}"
     - set_fact:
          atomID: >-
            {{
              lookup('file', 'container.id')
                | regex_search('com\.boomi\.container\.id=(.*)', '\1')
                | first
            }}
     - debug: msg="the value of container.id is {{atomID}}"     
     - command:  "sh eko.sh {{atomID}}"
       register: command_result
     - debug:
        var: command_result.stdout_lines
Bash脚本

#! /usr/bin/env bash
# set -x

user=user@xyz.com
passsword=2yu3990!£

curl -u "$user:$password" \
    -H "Accept: application/json" -s \
    https://api.boomi.com/api/rest/v1/FQ67wer/SharedServerInformation/${atomID} \
    | python -m json.tool
任务执行结果

TASK [debug] *******************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "command_result.stdout_lines": [
        "{",
        "    \"@type\": \"Error\",",
        "    \"message\": \"ID of object to get must not be blank or null\"",
        "}"
    ]
}

上面的例子中有几个问题。但是真正阻止您获得预期结果的是,您在最终脚本中使用了一个未定义的变量。我怀疑这是因为您混淆了ansible变量和bash环境变量

bash脚本正在尝试直接使用
$atomID
(您可以将其作为
${atomID}
进行保护),而该脚本未在脚本中的任何位置设置或包含shell环境。您正在将该值作为命令中的第一个位置参数传递给脚本,以便将其作为
$1
检索。如果您没有在某个点上赋值
atomID=$1
,您的脚本就无法自行猜测

此外,获取容器id需要进行非常复杂的操作。
container.id
是一个属性文件(即没有节的ini文件),ansible有一个现成的方法来检索这些值

为了测试我的示例playbook,我修改了您的脚本,以便它可以从传递的参数中获取值。我还
chmod u+x
文件,以便直接从命令/shell运行:

#/usr/bin/env bash
#从位置参数1为bash变量atomID赋值
#如果没有参数,则设置为“未定义”
atomID=${1:-未定义}
#简单测试:打印出atomID-替换为实际代码
echo atomID是${atomID}
剧本:

-name:使用ini文件中的参数运行脚本
主机:本地主机
收集事实:错误
变量:
atomID:“{{lookup('ini','com.boomi.container.id type=properties file=container.id')}”
任务:
-名称:如果将“-v”传递给playbook命令,则显示已解析的atomID
调试:
阿托米德
冗长:1
-name:使用给定参数运行脚本
命令:eko.sh{{atomID}
寄存器:运行脚本
-名称:显示脚本结果
调试:
var:run_script.stdout
结果是:

# use -v if you want to see the first debug value
$ ansible-playbook playbook.yml 

PLAY [Run a script with parameter from ini file] ***********************************************************************************************************************************************

TASK [Show parsed atomID] **********************************************************************************************************************************************************************
skipping: [localhost]

TASK [Run our script with the given parameter] *************************************************************************************************************************************************
changed: [localhost]

TASK [Show script result] **********************************************************************************************************************************************************************
ok: [localhost] => {
    "run_script.stdout": "atomID is aa186a63-f285-45e6-9a1e-afda36c7f59f"
}

PLAY RECAP *************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

这是一个完美的解决方案,也有助于通过ini查找使代码更具可读性。Cheers@sakworld如果这修正了你的问题,考虑接受答案(绿色蜱),所以其他人知道有一个工作解决方案。有关这方面的更多信息,请参见感谢您在勾号上告诉我我所做的非常小的更改是
command:eko.sh{{atomID}
as
“sh command:eko.sh{{atomID}”
我在回答中精确地指出,我
chmod u+x
脚本文件可以直接执行。此外,使用
sh
启动以
bash
shebang开头的脚本可能不是一个好主意。啊,对不起。同意你的chmod。
# use -v if you want to see the first debug value
$ ansible-playbook playbook.yml 

PLAY [Run a script with parameter from ini file] ***********************************************************************************************************************************************

TASK [Show parsed atomID] **********************************************************************************************************************************************************************
skipping: [localhost]

TASK [Run our script with the given parameter] *************************************************************************************************************************************************
changed: [localhost]

TASK [Show script result] **********************************************************************************************************************************************************************
ok: [localhost] => {
    "run_script.stdout": "atomID is aa186a63-f285-45e6-9a1e-afda36c7f59f"
}

PLAY RECAP *************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0