[英]undefined variable error in ansible when using jinja2 templates
I am practising ansible.我正在练习 ansible。 I have come all way through basic and now I am working on building a jinja2 template and using it.
我已经完成了基本操作,现在我正在构建一个 jinja2 模板并使用它。 There is an exercise when I need to build a report for groups and upload upload them to their respective dns server.
当我需要为组构建报告并将它们上传到各自的 dns 服务器时,有一个练习。 Reports for all the server in american group will be uploaded to dns_server_america, similarly for asia.
美国组中所有服务器的报告将上传到 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
This is the YAML.这是 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
This is the jinja2 template.这是 jinja2 模板。
{% for host in groups[GROUP_NAME] %}
{{ host }} {{ hostvars[host]['ansible_host'] }}
{% endfor %}
Why are we not quoting [host] and [GROUP_NAME] in the jinja2 template.为什么我们不在 jinja2 模板中引用[host]和[GROUP_NAME] 。 Ansible says that when variables are put in square brackets, they are supposed to be wrapped within quotes.
Ansible 说,当变量放在方括号中时,它们应该用引号括起来。 When I enclose then with quotes, I get an error message "undefined variable" and when I remove the quotes I am able to run the playbook successfully.
当我用引号括起来时,我收到一条错误消息“未定义的变量”,当我删除引号时,我能够成功运行剧本。 Please advise, I may be missing something or my theory of understanding variables could be wrong.
请告知,我可能遗漏了一些东西,或者我理解变量的理论可能是错误的。
Q: "Why are we not quoting [host] and [GROUP_NAME] in the jinja2 template?"
问: “为什么我们不在 jinja2 模板中引用 [host] 和 [GROUP_NAME]?”
A: Both host and GROUP_NAME are variables. A: host和GROUP_NAME都是变量。 The value of the variables is needed in the index.
索引中需要变量的值。 If the names of the variables are quoted 'host' or 'GROUP_NAME' the names of the variables are used instead of the variable's values.
如果变量的名称被引用“host”或“GROUP_NAME” ,则使用变量的名称而不是变量的值。
Direct and indirect直接和间接
For example, the template例如,模板
shell> cat test.txt.j2
{{ dict[index] }}
{{ dict['index'] }}
and the playbook和剧本
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
give给
shell> cat test.txt
value of attribute attr1
value of attribute index
This is not limited to templates.这不仅限于模板。 It's generally valid.
它通常是有效的。 For example
例如
- debug:
msg:
- "{{ dict[index] }}"
- "{{ dict['index'] }}"
vars:
index: attr1
gives给
msg:
- value of attribute attr1
- value of attribute index
Dotted点缀
It's possible to use the "dotted" reference.可以使用“dotted”引用。 For example
例如
- debug:
var: dict.index
gives给
dict.index: value of attribute index
The "dotted" reference can be used in nested dictionaries. “dotted”引用可用于嵌套字典。 For example with the nested dictionary
例如使用嵌套字典
dict:
index:
var1: value of attribute index
attr1:
var1: value of attribute attr1
both versions work as expected两个版本都按预期工作
- debug:
msg:
- "{{ dict.index.var1 }}"
- "{{ dict['index'].var1 }}"
vars:
index: attr1
gives给
msg:
- value of attribute index
- value of attribute index
Dotted references in template模板中的虚线引用
But there is a difference when a template is used.但是使用模板时有区别。 When a reference is put into the brackets
[]
all subsequent references must be put into the brackets as well.当引用放入括号
[]
时,所有后续引用也必须放入括号中。 Otherwise, the template will fail.否则,模板将失败。 For example
例如
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
will fail将失败
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.