call jinja2 template within Ansible role at Playbook level

319 Views Asked by At

I have a folder structure as

Ansible:
 - roles
   - elastic.beats
 - templates
   filebeat-inputs.yml.j2

From my playbook, I am calling it as below:

 - { 
     role: elastic.beats,
     beat: "filebeat", 
     beat_conf: "templates/filebeat-inputs.yml.j2" 
  }

But this does not seem to work. Note: beat_conf: accepts a "map structure of values" and hence any call to the yml.j2 file must be in the same form.

Also, how do I call more than one beat for the role? is it like this? or there is a cleaner way.

  - { 
     role: elastic.beats, 
     beat: "filebeat", 
     beat_conf: "templates/filebeat-inputs.yml.j2" 
    }
  - { 
     role: elastic.beats,
     beat: "metricbeat",
     beat_conf: "templates/metribeat-inputs.yml.j2" 
    }

Thanks

Just to give more context: I am trying to use Ansible elastic.beats role [https://github.com/elastic/ansible-beats] which needs a mandatory parameter beat_conf.

This parameter accepts values in the following format (map):

hosts: localhost
roles:
  - { 
      role: elastic.beats, 
      beat: filebeat
      beat_conf:
        filebeat:
          inputs:
            - type: log
              enabled: true
              paths:
                - /var/log/*.log
            - type: log
              paths:
                - /var/log/mysql.log
              scan_frequency: 10s
            - type: log
              paths:
                - /var/log/apache.log
              scan_frequency: 5s

However, the inputs can be put in a separate file ($root/templates/filebeat-inputs.yml.j2) as:

  - type: log
    enabled: true
    paths:
      - /var/log/*.log
  - type: log
    paths:
      - /var/log/mysql.log
    scan_frequency: 10s
  - type: log
    paths:
      - /var/log/apache.log
    scan_frequency: 5s
          

[Ref: https://www.elastic.co/guide/en/beats/filebeat/7.12/configuration-filebeat-options.html]

How do I call this file ($root/templates/filebeat-inputs.yml.j2) so that the final filebeat.yml file generated on the targets is of the following format:

https://github.com/elastic/beats/blob/master/filebeat/filebeat.yml

0

There are 0 best solutions below