rp346
rp346

Reputation: 7028

Create Ansible local facts from variables

I am trying to create local facts from variables.

My fact is:

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

My task is:

- include_vars: clouderamanager.yml

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

Which doesn't create the local facts, it fails with following error

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}

Upvotes: 0

Views: 551

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56859

Lineinfile does exactly what it says it does: it changes a single line in a file.

If you want to create a local fact that looks like:

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

Then you need to create a file that looks like this:

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

You can do this with the template module if, like in your example, you have variables in it that you want to dynamically build.

In this exact scenario though I'm confused as to what the benefit of this is rather than having a variable set up as you have in your question and using that rather than templating out a local fact file and then re-reading it later.

Upvotes: 1

Related Questions