我花了大部分時間試圖解決這個問題,迄今爲止失敗了。我正在構建一些手冊以使Splunk中的功能自動化,並試圖從清單組E.G.中轉換主機列表。Ansible concat vars字符串
[search_head]
1.2.3.4
5.6.7.8
(編輯)我的預期(期望)從該劇的調試輸出結果應該是: https://1-2-3-4-ansible_nodename:8089, https://5.6.7.8-ansible_nodename:8089
,我試圖通過對正在運行的主機上運行以下劇本完成此:
---
- name: Build search head list to initialize the captain
hosts: search_head
remote_user: ansible
vars:
inventory_file: ./inventory-ec2-single-site
search_head_uri: "{{ lookup('template', './bootstrap-sh-deployer.j2') }}"
pre_tasks:
- include_vars:
dir: 'group_vars'
extensions:
- yml
- yaml
tasks:
- name: dump array
debug:
msg: "{{ search_head_uri }}"`
隨着模板bootstrap-sh-deployer.j2
(編輯):
{%- set search_head_uri = [] %}
{% for host in groups['search_head'] %}
{%- if search_head_uri.append("https://" + [host][ansible_nodename] + ":8089") %}
{%- endif %}
{%- if not loop.last %}, {% endif -%}
{%- endfor %}
(編輯)。然而,與當前播放的錯誤: "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: {{ lookup('template', './bootstrap-sh-deployer.j2') }}: 'list object' has no attribute u'ip-10-10-0-55'\n\nThe error appears to have been in '/Users/christophergarrett/dev/splunk-ansible/deploy_ec2_testing.yml': line 16, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: dump array\n ^here\n"
編輯: 帖子編輯,以反映嘗試收集主機的事實。最初我曾發佈過嘗試從庫存文件中獲取主機的IP,這是我以前的工作。
太棒了,它停止了錯誤。現在我試圖從'inventory'文件中的每個主機獲取'ansible_nodename'。 (%) {% - set search_head_uri = [true] +「:8089」)%} {%endif - %} {% - if not loop.last%},{%endif - %} {% - endfor%}' – AlmostGosu
錯誤: ' 1.2.3.4]:失敗! => { 「failed」:true, 「msg」:「字段'args'有一個無效的值,其中包含一個未定義的變量,錯誤爲:{{lookup('template','。 /bootstrap-sh-deployer.j2')}}:'list object'沒有屬性u'ip-1-2-3-4'\ n \ n錯誤似乎出現在'/ Users/christophergarrett/dev/splunk-ansible/deploy_ec2_testing.yml':第16行第9列,但可能在文件中的其他位置,具體取決於確切的語法問題。\ n \ n違規行顯示爲:\ n \ n任務:\ n - 名稱:dump array \ n^here \ n「 }' – AlmostGosu
如果您有其他問題,請發佈其他問題。以前,想想爲什麼你把方括號中的「host」括起來。 – techraf