0

我正在尝试从变量创建局部事实。

我的事实是:

datadog_http_checks:
  - name : {{ env }} ResourceManager
    url : http://{{ inventory_hostname }}:
    threshold : 5
    window : 5
    timeout : 10

我的任务是:

- include_vars: clouderamanager.yml

- lineinfile: dest=/etc/ansible/facts.d/datadog_http_checks.fact line={{ datadog_http_checks }} create=yes

这不会创建本地事实,它会失败并出现以下错误

TASK [hadoop : lineinfile] *****************************************************
fatal: [hmn001.dev.abc.cc]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/xyz/.ansible/tmp/ansible-tmp-1460581285.88-252089462921262/lineinfile\", line 2540, in <module>\r\n    main()\r\n  File \"/home/abc/.ansible/tmp/ansible-tmp-1460581285.88-252089462921262/lineinfile\", line 371, in main\r\n    ins_aft, ins_bef, create, backup, backrefs)\r\n  File \"/home/abc/.ansible/tmp/ansible-tmp-1460581285.88-252089462921262/lineinfile\", line 266, in present\r\n    lines.append(line + os.linesep)\r\nTypeError: can only concatenate list (not \"str\") to list\r\n", "msg": "MODULE FAILURE", "parsed": false}
4

1 回答 1

1

Lineinfile完全按照它所说的去做:它改变了文件中的一行。

如果你想创建一个看起来像这样的本地事实:

datadog_http_checks:
  - name : {{ env }} ResourceManager
    url : http://{{ inventory_hostname }}:
    threshold : 5
    window : 5
    timeout : 10

然后您需要创建一个如下所示的文件:

[datadog_http_checks]
name={{ env }} ResourceManager
url=http://{{ inventory_hostname }}:
threshold=5
window=5
timeout=10

如果像在您的示例中那样,您可以使用模板模块执行此操作,并且您想要动态构建其中的变量。

在这个确切的场景中,虽然我很困惑这样做的好处是什么,而不是像你在问题中那样设置一个变量并使用它,而不是模板化一个本地事实文件,然后再重新阅读它。

于 2016-04-13T21:44:29.127 回答