从jinja2引用主机文件中的变量

时间:2018-05-30 11:02:26

标签: ansible jinja2

我正在使用jinja2模板进行haproxy配置,并且我在库存文件中声明了变量。如何在模板中引用此变量。我在脚本下面粘贴,但它不起作用。

我的库存: -

[as]
10.0.0.1 asRole=batch
10.0.0.2 asRole=batch
10.0.0.3 asRole=transaction
10.0.0.4 asRole=transaction

[hap]
10.0.0.6 

[hap]是haproxy的ip,我的ansible是基于[as]节点在[hap]节点上设置haproxy配置。

我的Jinja2 tempalte: -

{% for host in groups['as'] %}                                                 
 {% if "transaction" in "{{ asRole }} %}                                
  server {{ hostvars[host]['inventory_hostname'] }} {{ host }}:{{ httpd_port }}check
 {% endif %}                                                                     
{% endfor %}

BR, Tanmoy

1 个答案:

答案 0 :(得分:0)

小心{% for host in groups['as'] %} {% if "transaction" in asRole %} server {{ hostvars[host]['inventory_hostname'] }} {{ host }}:{{ httpd_port }}check {% endif %} {% endfor %} Var:

s=df.groupby(['Customer Name','Deposit_Date'],as_index=False)[['DPD']].agg(['count','sum'])
s.columns = [' '.join(col) for col in s.columns]
s.reset_index(inplace=True)

s['DPD_CumSum']=s.groupby(['Customer Name'])[['DPD sum']].cumsum()
s['DPD_CumCount']=s.groupby(['Customer Name'])[['DPD count']].cumsum()
s['DPD_CumMean']=s['DPD_CumSum']/ s['DPD_CumCount']
s['DPD_PrevMean']=s.groupby(['Customer Name'])['DPD_CumMean'].shift(1)

df=df.merge(s[['Customer Name','Deposit_Date','DPD_PrevMean']],how='left',on=['Customer Name','Deposit_Date'])