如何在 Ansible 模板模块中使用变量

How to use variables in Ansible template module

我正在尝试使用推送到远程计算机的 keepalived.conf.j2 模板文件中的变量。基本上我试图将 eth1 接口的远程机器动态 IP 地址插入 keepalived.conf.j2

这是任务:

- name: Keepalived config push
  template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf force=yes owner=root mode=664
  tags: Config push

这里是 jinja2 配置文件的内容:

   }
vrrp_instance 50 {
  virtual_router_id 50
  advert_int 1
  priority 101
  state MASTER
  interface eth0
  virtual_ipaddress {
    {{ ansible_eth1:network}} dev eth0

实现这个的最佳方法是什么,所以每次我推送到远程机器时,它都会在 conf 文件中有它的 eth1 接口?

好的,看来我想通了。 您的剧本必须有 gather_facts:在 j2 模板中您需要有以下行:

{{ ansible_eth1.ipv4.address }}