JINJA2 模板 -- keepalived

本文介绍了如何利用Ansible的角色功能和Jinja2模板语言来部署Keepalived。通过示例展示了如何在模板中处理变量和条件,配置包括VRRP_script、vrrp_instance等关键部分,实现高可用路由设置。
摘要由CSDN通过智能技术生成

JINJA2 模板 – keepalived

Ansible 部署 keepalived的时候,想要使用JIANJA2模板来实现。

建议直接使用角色来部署:

ansible-galaxy search mrlesmithjr.keepalived

以下是角色中模板的内容:

`{{ ansible_managed|comment }}
{% if keepalived_config|bool %}
{% if keepalived_router_info is defined %}
{% for item in keepalived_router_info %}
{% if item.check_script is defined %}
{% for chk in item.check_script %}
vrrp_script {{ chk.name }} {
script “{{ chk.script }}”
interval {{ chk.interval }}
weight {{ chk.weight }}
}
{% endfor %}
{% endif %}

vrrp_instance {{ item.name }} {
interface {{ item.vip_int }}

{% if inventory_hostname == item.master_node %}
state MASTER
priority {{ item.router_pri_master }}
{% elif inventory_hostname != item.master_node %}
state BACKUP
priority {{ item.router_pri_backup }}
{% endif %}

virtual_router_id {{ item.router_id }}
virtual_ipaddress {
{% for ip in item.vip_addresses %}
{{ ip }}
{% endfor %}
}

{% if item.check_script is defined %}
{% for chk in item.check_script %}
track_script {
{{ chk.name }}
}
{% endfor %}
{% endif %}

{%- if item.nopreempt is defined and item.nopreempt %}
nopreempt
{% endif -%}

{% if item.use_unicast is defined and item.use_unicast %}
unicast_src_ip {{ unicast_src_ip }}
{% for peer in unicast_peers %}
unicast_peer {
{{ peer }}
{% endfor -%}
}
{% endif %}

{% if item.auth_pass is defined %}
authentication {
auth_type PASS
auth_pass {{ item.auth_pass }}
}
{% endif %}

}
{% endfor %}
{% endif %}
`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值