2

I need to generate a single file on the remote host using multiple template files and Jinja's {% block block_name %} in my Ansible role

For example,

main.conf.j2:

value1 = 123
value2 = 456

{% block test %} {% endblock %}

value3 = 789

{% block example %} {% endblock %}

value4 = abcd

test.conf.j2:

{% block test %}
more text here
{% endblock %}

example.conf.j2

{% block example %}
....
example_param = 'example!'
....
{% endblock %}

What's the next step? I must use {% extends 'nginx.conf.j2' %} in test.conf.j2 and example.conf.j2? And if so - how will look my Ansible task? Or even something else?

If I trying something like this:

- name: Copy config
  template:
    src: "{{ item }}"
    dest: "{{ conf_file_path }}"
  with_items:
    - "main.conf.j2"
    - "test.conf.j2"
    - "example.conf.j2"
    - "abcd.conf.j2"

it works only for main.conf.j2 and test.conf.j2, but ignores example.conf.j2 and other templates

Serg
  • 109
  • 2
  • 10

1 Answers1

6

Q: "What's the next step? I must use {% extends 'nginx.conf.j2' %} ... ?"

A: Yes. extends is needed. For example

    - template:
        src: test.j2
        dest: test

with the templates

    shell> cat main.j2
    value1 = 123
    {% block test %}
    value = default value in main.j2
    {% endblock %}
    value3 = 789
    shell> cat test.j2 
    {% extends 'main.j2' %}
    {% block test %}
    value = custom value in test.j2
    {% endblock %}

gives

    shell> cat test 
    value1 = 123
    value = custom value in test.j2
    value3 = 789

Q: "How will look my Ansible task?"

    - name: Copy config
      template:
        src: "{{ item }}"
        dest: "{{ conf_file_path }}"
      with_items:
        - "main.conf.j2"
        - "test.conf.j2"
        - "example.conf.j2"
        - "abcd.conf.j2"

A: The loop will repeatedly overwrite the dest file in each iteration. See template.


FWIW. It's possible to use blockinfile and loop the lookup of the templates. For example

    - template:
        src: main2.j2
        dest: test
    - blockinfile:
        marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item }}"
        path: test
        block: "{{ lookup('template', item) }}"
      loop:
        - test.conf.j2
        - example.conf.j2

with the templates

    shell> cat main2.j2
    value1 = 123
    
    # BEGIN ANSIBLE MANAGED BLOCK test.conf.j2
    value_test = default value in main2.j2
    # END ANSIBLE MANAGED BLOCK test.conf.j2
    
    # BEGIN ANSIBLE MANAGED BLOCK example.conf.j2
    value_example = default value in main2.j2
    # END ANSIBLE MANAGED BLOCK example.conf.j2
    
    value3 = 789
    shell> cat test.conf.j2
    value_test = custom value in test.conf.j2
    shell> cat example.conf.j2
    value_example = custom value in example.conf.j2

give

    shell> cat test 
    value1 = 123
    
    # BEGIN ANSIBLE MANAGED BLOCK test.conf.j2
    value_test = custom value in test.conf.j2
    # END ANSIBLE MANAGED BLOCK test.conf.j2
    
    # BEGIN ANSIBLE MANAGED BLOCK example.conf.j2
    value_example = custom value in example.conf.j2
    # END ANSIBLE MANAGED BLOCK example.conf.j2
    
    value3 = 789
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63