0

我正在尝试编写一个角色来配置一个 keepalived 集群。我希望根据目标框的 IP 将唯一信息传递到模板中。

在这种情况下:服务器 A 是 192.168.1.140,服务器 B 是 192.182.1.141,VIP 是 192.168.1.142

字典看起来像这样:

---
  192.168.1.140:
    peer: 192.168.1.141
    priority: 110
    vip: 192.168.1.142

  192.1.168.1.141
    peer:192.168.1.140
    priority: 100
    vip: 192.168.1.142

我希望任务看起来像这样:

---
- name: keepalived template
  template:
    src: keepalived.j2
    dest: /etc/keepalived/keepalived.conf
    owner: root
    group: root
    mode: 0644
  with_dict: '{{ ansible_default_ipv4.address }}'

模板看起来像这样:

}
vrrp_instance VI_1 {
interface    eth0
priority    {{ item.value.priority }}
...
unicast_scr {{ ansible_default_ipv4.address }}
unicast_peer {
     {{ item.value.peer }}
}
virtual_ipaddresses {
     {{ item.value.vip }} }
}

任何见解将不胜感激约翰

4

1 回答 1

0

将您的同行详细信息分组到一些常用字典下:

---
peer_configs:
  192.168.1.140:
    peer: 192.168.1.141
    priority: 110
    vip: 192.168.1.142
  192.1.168.1.141
    peer:192.168.1.140
    priority: 100
    vip: 192.168.1.142

with_...通常用于循环,如我所见,您不需要任何循环,因此请使用:

- name: keepalived template
  template:
    src: keepalived.j2
    dest: /etc/keepalived/keepalived.conf
    owner: root
    group: root
    mode: 0644
  vars:
    peer_config: '{{ peer_configs[ansible_default_ipv4.address] }}'

和模板:

vrrp_instance VI_1 {
interface    eth0
priority    {{ peer_config.priority }}
...
unicast_scr {{ ansible_default_ipv4.address }}
unicast_peer {
     {{ peer_config.peer }}
}
virtual_ipaddresses {
     {{ peer_config.vip }} }
}
于 2017-06-27T14:35:55.903 回答