Zerg12
Zerg12

Reputation: 317

How to use variables in Ansible template module

I am trying to use a variable in keepalived.conf.j2 template file that I push to the remote machine. Basically I am trying to insert the remote machine dynamic IP address for eth1 interface into keepalived.conf.j2.

Here is the task:

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

Here is the content of the jinja2 conf file:

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

What is the best way to implement this, so every time I push to the remote machine it will have its eth1 interface in the conf file?

Upvotes: 0

Views: 1303

Answers (1)

Zerg12
Zerg12

Reputation: 317

Ok, It seems I have figured it out. Your playbook has to have gather_facts: on and in j2 template you need to have the following line:

{{ ansible_eth1.ipv4.address }}

Upvotes: 1

Related Questions