ansible 模板为列表添加价值 -
ansible template add value to list -
基于以下 ansible 剧本值..
target: "actual.domain.com"
aliases:
- "alias1.domain.com"
- "alias2.domain.com"
我正在尝试设置一个 ansible 模板来生成 nginx server_name
在这种情况下应该是:
server_name: "actual.domain.com alias1.domain.com alias2.domain.com"
所以,我尝试了以下 jinja2 脚本 ...
{% 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'
我哪里错了?
感谢您的帮助和 HNY!
为什么不简单地连接 target
和 aliases
,然后在结果上连接 运行 join(' ')
?
{% set servername = [ item.target ] %}
{% set aliases = item.aliases if item.aliases is defined else [] %}
{% set ignored = servername.extend(aliases) %}
servername: {{ servername | join(' ') }}
看来你把这件事变得比必要的复杂得多了。为什么不是这样的?
$ 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
文件内容
$ 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 剧本值..
target: "actual.domain.com"
aliases:
- "alias1.domain.com"
- "alias2.domain.com"
我正在尝试设置一个 ansible 模板来生成 nginx server_name 在这种情况下应该是:
server_name: "actual.domain.com alias1.domain.com alias2.domain.com"
所以,我尝试了以下 jinja2 脚本 ...
{% 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'
我哪里错了?
感谢您的帮助和 HNY!
为什么不简单地连接 target
和 aliases
,然后在结果上连接 运行 join(' ')
?
{% set servername = [ item.target ] %}
{% set aliases = item.aliases if item.aliases is defined else [] %}
{% set ignored = servername.extend(aliases) %}
servername: {{ servername | join(' ') }}
看来你把这件事变得比必要的复杂得多了。为什么不是这样的?
$ 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
文件内容
$ 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