Ansible concat vars to string [英] Ansible concat vars to string
问题描述
我花了一天的大部分时间试图解决这个问题,但到目前为止都失败了.我正在构建一些剧本来自动化 Splunk 中的功能,并尝试从库存组 E.G. 转换主机列表.
I've spent most of the day trying to solve this problem and have thus far failed. I am building some playbooks to automate functions in Splunk, and am attempting to convert a list of hosts from an inventory group E.G.
[search_head]
1.2.3.4
5.6.7.8
我对剧集调试输出的预期(期望)结果应该是:https://1.2.3.4:8089, https://5.6.7.8:8089
My expected (desired) result from the debug output of the play should be:
https://1.2.3.4:8089, https://5.6.7.8:8089
我正在尝试通过对正在运行的主机运行以下剧本来完成此操作:
I am attempting to complete this by running the following playbook against a running host:
---
- 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 }}:8089") %}
{%- endif %}
{%- if not loop.last %}, {% endif -%}
{%- endfor %}
然而,当前播放返回 search_head_uri: ", "
这告诉我循环正在运行,但 {{ host }}
没有解析.
However, the current play returns search_head_uri: ", "
which tells me that the loop is running, but {{ host }}
is not resolving.
推荐答案
打开 Jinja2 表达式或语句后,您应该使用 Jinja2 语法.您不能嵌套它们(即您不能在 {% %}
内使用 {{ }}
).
Once you open a Jinja2 expression or a statement you should use Jinja2 syntax. You cannot nest them (i.e. you can't use {{ }}
inside {% %}
).
{%- if search_head_uri.append("https://" + host + ":8089") %}
这篇关于Ansible concat vars to string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!