ansible模板为列表增加价值- [英] ansible template add value to list -

查看:112
本文介绍了ansible模板为列表增加价值-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于以下可读取的剧本值..

based on the following ansible playbook values ..

target:  "actual.domain.com"
aliases:
  - "alias1.domain.com"
  - "alias2.domain.com"

我正在尝试设置一个Ansible模板以生成nginx server_name
,在这种情况下,该名称应为:

I am trying to setup an ansible template to produce the nginx server_name which in this case should be:

server_name: "actual.domain.com alias1.domain.com alias2.domain.com"

因此,我尝试了以下jinja2脚本...

so , I tried the following jinja2 script ...

{% if item.aliases is defined %}
    {% set servername = [ item.target ] %}
    {% for alias in item.aliases.iteritems() %}
       {% if alias|length > 0 %}
           {% servername|join(' '), alias %}  # <= line 30
       {% endif %}
    {% endfor %}
    server_name {{ servername }};
 {% else %}
     server_name {{ item.target }};
 {% endif %}
  ....

但失败了,行号:30,错误:遇到未知标签'servername'

but it's failing , line number: 30, error: Encountered unknown tag 'servername'

我在哪里可能错了?

谢谢寻求帮助和HNY!

thanks for help and HNY !

推荐答案

似乎您使这一过程变得比必要的复杂得多。为什么不这样呢?

It seems you've made this substantially more complicated than necessary. Why not something like this?

$ ansible-playbook -i hosts play.yml

PLAY [localhost] **************************************************************

TASK: [template src='servername.j2' dest=tmp/servername-{{item.target}}] ******
changed: [localhost] => (item={'target': 'actual.domain.com', 'aliases': ['alias1.domain.com', 'alias2.domain.com']})

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0



文件内容



Contents of files

$ tail -n 1000 `find ./ -type f`

==> .//hosts <==
[localhost]
localhost ansible_connection=local


==> .//play.yml <==
- hosts: localhost
  gather_facts: false
  vars:
    servers:
      - target: "actual.domain.com"
        aliases:
          - "alias1.domain.com"
          - "alias2.domain.com"
  tasks:
    - template: src='servername.j2' dest=tmp/servername-{{item.target}}
      with_items: servers


==> .//servername.j2 <==
server_name {{ item.target }} {{ item.aliases|join(" ") }}


==> .//tmp/servername-actual.domain.com <==
server_name actual.domain.com alias1.domain.com alias2.domain.com

这篇关于ansible模板为列表增加价值-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆