Ansible concat变量到字符串

Ansible concat变量到字符串,ansible,jinja2,ansible-facts,ansible-template,Ansible,Jinja2,Ansible Facts,Ansible Template,我花了一天的大部分时间试图解决这个问题,但到目前为止都失败了。我正在构建一些剧本来自动化Splunk中的功能,并试图从库存组转换主机列表,例如 [search_head] 1.2.3.4 5.6.7.8 我对该剧调试输出的预期(期望)结果应为: https://1.2.3.4:8089, https://5.6.7.8:8089 我正试图通过对正在运行的主机运行以下playbook来完成此操作: --- - name: Build search head list to initializ

我花了一天的大部分时间试图解决这个问题,但到目前为止都失败了。我正在构建一些剧本来自动化Splunk中的功能,并试图从库存组转换主机列表,例如

[search_head]
1.2.3.4
5.6.7.8
我对该剧调试输出的预期(期望)结果应为:
https://1.2.3.4:8089, https://5.6.7.8:8089

我正试图通过对正在运行的主机运行以下playbook来完成此操作:

---
  - 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\u head\u uri:“,”
这告诉我循环正在运行,但是
{{host}
没有解析。

一旦打开Jinja2表达式或语句,就应该使用Jinja2语法。不能嵌套它们(即不能在
{%}
内部使用
{{{}}


这起作用了——将上面的答案结合起来修复jinja格式,并使用
hostvars
访问
ansible\u节点名

{%- set search_head_uri = [] %}
{% for host in groups['search_head'] %}
    {{ "https://" + hostvars[host]['ansible_nodename'] + ":8089" }}
    {%- if not loop.last %}, {% endif -%}
{%- endfor %}

太棒了,它不再出错了。现在,我正试图从
清单
文件中的每个主机获取
ansible\u节点名
<代码>{%-set search_head_uri=[true]]}{%for hostin group['search_head']%}{%-if search_head_uri.append(“https://”+[host][ansible_nodename]+“:8089”)}{%endif-%}{%-if not loop.last%},{%endif-%}{%-endfor%}错误:致命:[1.2.3.4]:失败!=>{“failed”:true,“msg”:“字段'args'的值无效,似乎包含未定义的变量。错误为:{lookup('template','./bootstrap sh deployer.j2')}:“列表对象”没有属性u'ip-1-2-3-4'\n\n错误似乎出现在“/Users/christorgerrett/dev/splunk ansible/deploy\u ec2\u testing.yml”:第16行第9列,但可能\n位于文件的其他位置,具体取决于语法问题。\n\n有问题的行似乎是:\n\n tasks:\n-name:dump array\n^here\n”}如果您有其他问题,请发布其他问题。在此之前,请考虑将
host
括在方括号中的原因。
{%- set search_head_uri = [] %}
{% for host in groups['search_head'] %}
    {{ "https://" + hostvars[host]['ansible_nodename'] + ":8089" }}
    {%- if not loop.last %}, {% endif -%}
{%- endfor %}