将数组作为参数传递给ansible中的脚本模块

时间:2017-12-05 09:29:00

标签: scripting ansible

我正在使用脚本模块在某些主机上运行脚本。我有一个数组ansible变量,我想将脚本作为空格分隔的参数传递。直接传递数组变量作为参数没有帮助。建议?

1 个答案:

答案 0 :(得分:0)

您可以使用jinja filter加入您的列表,并将其作为变量传递,如下所示:

ansible -m script -a "myscript.sh {{ test_list|join(' ') }}" localhost -e "{"test_list": [1,2,3]}"

如果myscript.sh是:

#!/bin/bash
echo Args are: ${@}, 1st: $1 2nd: $2, 3d: $3

输出将是:

localhost | SUCCESS => {
    "changed": true,
    "failed": false,
    "rc": 0,
    "stderr": "",
    "stdout": "Args are: 1 2 3, 1st: 1 2nd: 2, 3d: 3\n",
    "stdout_lines": [
        "Args are: 1 2 3, 1st: 1 2nd: 2, 3d: 3"
    ]
}
相关问题