[英]undefined variable error in ansible when using jinja2 templates
我正在练习 ansible。 我已经完成了基本操作,现在我正在构建一个 jinja2 模板并使用它。 当我需要为组构建报告并将它们上传到各自的 dns 服务器时,有一个练习。 美国组中所有服务器的报告将上传到 dns_server_america,亚洲也是如此。
dns_server_america ansible_host=172.20.1.100 ansible_ssh_pass=Passw0rd ansible_user=root
dns_server_asia ansible_host=172.20.1.101 ansible_ssh_pass=Passw0rd ansible_user=root
[america]
web0001 ansible_hostname=web0001.company.com ansible_host=10.1.1.101
web0002 ansible_hostname=web0002.company.com ansible_host=10.1.1.102
[asia]
web2001 ansible_hostname=web2001.company.com ansible_host=10.1.1.201
web2002 ansible_hostname=web2002.company.com ansible_host=10.1.1.202
这是 YAML。
- name: Generate dns hosts files on americas servers
hosts: dns_server_america
tasks:
- template: src=templates/hosts.j2 dest=/tmp/hosts.txt
vars:
GROUP_NAME: america
- name: Generate dns hosts files on asia servers
hosts: dns_server_asia
tasks:
- template: src=templates/hosts.j2 dest=/tmp/hosts.txt
vars:
GROUP_NAME: asia
这是 jinja2 模板。
{% for host in groups[GROUP_NAME] %}
{{ host }} {{ hostvars[host]['ansible_host'] }}
{% endfor %}
为什么我们不在 jinja2 模板中引用[host]和[GROUP_NAME] 。 Ansible 说,当变量放在方括号中时,它们应该用引号括起来。 当我用引号括起来时,我收到一条错误消息“未定义的变量”,当我删除引号时,我能够成功运行剧本。 请告知,我可能遗漏了一些东西,或者我理解变量的理论可能是错误的。
问: “为什么我们不在 jinja2 模板中引用 [host] 和 [GROUP_NAME]?”
A: host和GROUP_NAME都是变量。 索引中需要变量的值。 如果变量的名称被引用“host”或“GROUP_NAME” ,则使用变量的名称而不是变量的值。
直接和间接
例如,模板
shell> cat test.txt.j2
{{ dict[index] }}
{{ dict['index'] }}
和剧本
shell> cat playbook.yml
- hosts: localhost
vars:
dict:
index: value of attribute index
attr1: value of attribute attr1
tasks:
- template:
src: test.txt.j2
dest: test.txt
vars:
index: attr1
给
shell> cat test.txt
value of attribute attr1
value of attribute index
这不仅限于模板。 它通常是有效的。 例如
- debug:
msg:
- "{{ dict[index] }}"
- "{{ dict['index'] }}"
vars:
index: attr1
给
msg:
- value of attribute attr1
- value of attribute index
点缀
可以使用“dotted”引用。 例如
- debug:
var: dict.index
给
dict.index: value of attribute index
“dotted”引用可用于嵌套字典。 例如使用嵌套字典
dict:
index:
var1: value of attribute index
attr1:
var1: value of attribute attr1
两个版本都按预期工作
- debug:
msg:
- "{{ dict.index.var1 }}"
- "{{ dict['index'].var1 }}"
vars:
index: attr1
给
msg:
- value of attribute index
- value of attribute index
模板中的虚线引用
但是使用模板时有区别。 当引用放入括号[]
时,所有后续引用也必须放入括号中。 否则,模板将失败。 例如
shell> cat test.txt.j2
{{ dict.index.var1 }} # OK
{{ dict.index['var1'] }} # OK
{{ dict['index']['var1'] }} # OK
{{ dict['index'].var1 }} # WRONG: has no attribute var1
将失败
fatal: [localhost]: FAILED! => changed=false
msg: 'AnsibleUndefinedVariable:
''ansible.parsing.yaml.objects.AnsibleUnicode object''
has no attribute ''var1'''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.