Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jinja2 relative templates #1244

Closed
stijnopheide opened this issue Oct 6, 2012 · 3 comments
Closed

jinja2 relative templates #1244

stijnopheide opened this issue Oct 6, 2012 · 3 comments
Milestone

Comments

@stijnopheide
Copy link
Contributor

When using inheritance in jinja2 templates, it would be more intuitive to be able to reference depending templates (include, extends) relative to the base template.

example

the base template is a file at: your_repo/templates/some_template.j2

<VirtualHost {% if apps[appname]["ip"] is defined %}{{ ip }}{% else %}*{% endif %}:{% if ssl == "enabled" %}443{% else %}80{% endif %}>
    ServerName {{ servername }}

    ## What shall we share through this vhost?

    {%- block what %}{% endblock %}

{% if serveralias is defined %}
    ServerAlias {{ serveralias }}
{% endif %}
    ErrorLog ${APACHE_LOG_DIR}/{{ appname }}-error.log
    CustomLog ${APACHE_LOG_DIR}/{{ appname }}-access.log combined
    LogLevel warn

{% if ssl == "enabled" %}
    ## Stuff for certificates
    SSLEngine on
    ## ...
{% endif %}
</VirtualHost>

the concrete template is a file at: your_repo/templates/concrete_template.j2

{% extends "some_template.j2" %}
{% block what %}
    DocumentRoot "/home/{{ user }}/applications/{{ appname }}"
    <Directory "/home/{{ user }}/applications/{{ appname }}">
        allow from all
        Options -Indexes
    </Directory>
{% endblock %}

Currently you need to reference the templates relative to the playbook location. I can only get it to work if the playbook is in the 'your_repo' dir, but not if you have to reference it from e.g. your_repo/playbooks/my_playbook.yaml

It is possible to get this to work by changing template_from_file in utils.py:

def template_from_file(basedir, path, vars):
    ''' run a file through the templating engine '''
    loader=jinja2.FileSystemLoader([basedir,os.path.dirname(os.path.join(basedir, path))])
    environment = jinja2.Environment(loader=loader, trim_blocks=True)
    environment.filters['to_json'] = json.dumps
   ...

I don't know however if this has any impact on other parts of ansible.

@mpdehaan
Copy link
Contributor

mpdehaan commented Oct 6, 2012

I agree and suspect it would not cause any problems, want to send me a pull request?

Sent from my iPad

On Oct 6, 2012, at 8:09 AM, Stijn Opheide notifications@github.com wrote:

When using inheritance in jinja2 templates, it would be more intuitive to be able to reference depending templates (include, extends) relative to the base template.

example

the base template is a file at: your_repo/templates/some_template.j2

<VirtualHost {% if apps[appname]["ip"] is defined %}{{ ip }}{% else %}*{% endif %}:{% if ssl == "enabled" %}443{% else %}80{% endif %}>
ServerName {{ servername }}

## What shall we share through this vhost?

{%- block what %}{% endblock %}

{% if serveralias is defined %}
ServerAlias {{ serveralias }}
{% endif %}
ErrorLog ${APACHE_LOG_DIR}/{{ appname }}-error.log
CustomLog ${APACHE_LOG_DIR}/{{ appname }}-access.log combined
LogLevel warn

{% if ssl == "enabled" %}
## Stuff for certificates
SSLEngine on
## ...
{% endif %}

the concrete template is a file at: your_repo/templates/concrete_template.j2

{% extends "some_template.j2" %}
{% block what %}
DocumentRoot "/home/{{ user }}/applications/{{ appname }}"
<Directory "/home/{{ user }}/applications/{{ appname }}">
allow from all
Options -Indexes

{% endblock %}
Currently you need to reference the templates relative to the playbook location. I can only get it to work if the playbook is in the 'your_repo' dir, but not if you have to reference it from e.g. your_repo/playbooks/my_playbook.yaml

It is possible to get this to work by changing template_from_file in utils.py:

def template_from_file(basedir, path, vars):
''' run a file through the templating engine '''
loader=jinja2.FileSystemLoader([basedir,os.path.dirname(os.path.join(basedir, path))])
environment = jinja2.Environment(loader=loader, trim_blocks=True)
environment.filters['to_json'] = json.dumps
...
I don't know however if this has any impact on other parts of ansible.


Reply to this email directly or view it on GitHub.

@stijnopheide
Copy link
Contributor Author

sure

@mpdehaan
Copy link
Contributor

mpdehaan commented Oct 8, 2012

closing this ticket as it will be covered by #1254 ... thanks!

@mpdehaan mpdehaan closed this as completed Oct 8, 2012
@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants