我正在编写 Ansible 剧本,在模板中我需要替换一个变量,该变量是字典列表。
任务文件如下:
vars:
locations:
- context: "/rest"
server: "http://locahost:8080;"
- context: "/api"
server: "http://localhost:9090;"
tasks:
- name: testing the template
template:
src: ./conf.j2
dest: /tmp/test.conf
with_items: '{{ locations }}'
我需要替换locations
模板中的。所以模板如下:
{% for location in item %}
location {{ location['context'] }}
proxy_pass {{ location['server'] }}
{% endfor %}
我期待的输出如下:
location /rest
proxy_pass http://localhost:8080
location /api
proxy_pass htpp://localhost:9090
但是我很难让替换正确,任何人都可以帮助指出我在哪里犯了错误。
我得到的错误是
failed: [127.0.0.1] (item={u'context': u'/rest', u'server':
u'http://localhost:9090;'}) => {"failed": true, "item": {"context":
"/rest", "server": "http://localhost:8080;"}, "msg":
"AnsibleUndefinedVariable: 'context' is undefined"}
failed: [127.0.0.1] (item={u'context': u'/api', u'server':
u'http://locahost:8080;'}) => {"failed": true, "item": {"context":
"/api", "server": "http://locahost:9090;"}, "msg":
"AnsibleUndefinedVariable: 'context' is undefined"}