0

Need to loop through the source files one by one for all hosts.

- hosts: epson*
  become: yes

  tasks:
  - name: replace id
    vars:
      id: abc
    template:
      src: epson1.j2
      dest: /home/epson.config
HOSTS FILE
[epson1]
1.1.1.1
[epson2]
1.1.1.1
[epson3]
1.1.1.1
and many more
epson1.j2
create element edge0 {
   state="ENABLED"
   id="{{ id }}"}

epson2.j2
create element edge1 {
   state="ENABLED"
   id="{{ id }}"}

I have many template files like epson1.j2, epson2.j2 and so on. Right now i am able to do template variable replace for 1 host and for 1 file. How can I do for all files for all hosts.

like - host:epson1, src: epson1.j2, dest: /home/epson.config
host:epson2, src: epson2.j2, dest: /home/epson.config
host:epson3, src: epson3.j2, dest: /home/epson.config

need looping inside src for every hosts
suraj shetty
  • 136
  • 1
  • 6
  • What *src* should use the host *1.1.1.1* ? – Vladimir Botka Oct 31 '19 at 16:47
  • so my terrafrom creates 3 ec2 instances(3 hosts) and along with that 3 more j2 template files for each ec2 instance. now i have to go inside each host and do templating but for different file. like host1-template1.j2...host2-template2.j2....all templates are placed in same folder in local server – suraj shetty Oct 31 '19 at 17:59

1 Answers1

0

You should be able to accomplish this simply by using the inventory_hostname magic variable.

- hosts: epson*
  become: yes

  tasks:
  - name: replace id
    vars:
      id: abc
    template:
      src: {{ inventory_hostname }}.j2
      dest: /home/epson.config

The Play will run once for each host and the correct .j2 will be used.

poltj18
  • 286
  • 2
  • 6