ansible渲染template时出错unicode object' has no attribute 'ansible_
本文介绍了ansible渲染template时出错unicode object' has no attribute 'ansible_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
最近在看《奔跑吧,ansible》,在第四章fact与变量遇到一个问题。
直接使用debug能获取到变量ansible_ens33.ipv4.address,但是按照书中的用法,在template中调用时,有如下报错,意思是,没有这个属性,还是说书中的用法有问题?{{ host_dr.ansible_ens33.ipv4.address }}
hosts
[nat_director]
192.168.101.221
[nat_realserver]
rs1 ansible_ssh_host=192.168.101.222
rs2 ansible_ssh_host=192.168.101.223
playbook.yml
- name: Configure Nat Direcorty
hosts: nat_director nat_realserver
vars:
tasks:
- name: template ipvsadm conf
template:
src: templates/ipvsadm.j2
dest: /root/haha
when: group_names[0] == "nat_director"
- debug: var=groups.nat_director
# - debug: var=ansible_ens33.ipv4.address
templates/ipvsadm.j2
{% for host_dr in groups.nat_director %}
-A -t {{ host_dr.ansible_ens33.ipv4.address }}:80 -s rr
{% for host_rs in groups.nat_realserver %}
-a -t {{ host_dr.ansible_ens33.ipv4.address }}:80 -r {{ host_rs.ansible_ens33.ipv4.address }}:80 -m -w 1
{% endfor %}
{% endfor %}
解决方案
经过验证,书上的例子确实有点问题,应该使用hostvars进行加工
{% for host_dr in groups.nat_director %}
-A -t {{ hostvars[host_dr].ansible_ens33.ipv4.address }}:80 -s rr
{% for host_rs in groups.nat_realserver %}
-a -t {{ hostvars[host_dr].ansible_ens33.ipv4.address }}:80 -r {{ hostvars[host_rs].ansible_ens33.ipv4.address }}:80 -m -w 1
{% endfor %}
{% endfor %}
这篇关于ansible渲染template时出错unicode object' has no attribute 'ansible_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文