Skip to main content

Use Jinja2 templates via cli

Project description

Jinja2 tools

Use Jinja2 templates via cli

Install

$ pip install jinja2-tools

Usage

Usage: jinja render [OPTIONS]
Options:
  -d, --data TEXT          PATH to YAML or JSON data file, URL or '-' for
                           stdin.

  -t, --template TEXT      PATH to directory or to any file that uses Jinja,
                           URL or '-' for stdin.

  -v, --verbose
  -tb, --no-trim-blocks    Disable trim blocks.
  -lb, --no-lstrip-blocks  Disable lstrip blocks.
  -o, --output PATH        PATH for output, stdout by default.
  -e, --extra-var TEXT     key value pair separated by '='. 'value' will be
                           treated as JSON or as a string in case of JSON
                           decoding error. This will take precedence over
                           'data'.

  --help                   Show this message and exit.

Whitespace Control

trim_blocks and lstrip_blocks are used by default, to disable them use -tb or -lb respectively.

Examples

  • Use path from the filesystem for data & template:

    ➜ jinja render -d examples/data/data.yaml -t examples/templates/template.yaml
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    
    # All ACLs have been generated
    
  • Use stdin for data & URL for template, also disable trim blocks & lstrip blocks:

    ➜ jinja render -d - -t https://raw.githubusercontent.com/Avielyo10/jinja2-tools/master/examples/templates/template.yaml -lb -tb < examples/data/data.yaml
    (1)
    ip access-list extended al-hq-in
      (2)
        (3)
        remark Allow traffic from hq to local office
        (4)
      (2)
        (3)
        permit 10.0.0.0/22 10.100.0.0/24
        (5)
      (6)
    (7)
    
    # All ACLs have been generated
    
  • Verbose:

    ➜ jinja render -d examples/data/data.yaml -t examples/templates/template.yaml -v     
    ---------- [Data] ----------
    {
      "access_lists": {
        "al-hq-in": [
          {
            "action": "remark",
            "text": "Allow traffic from hq to local office"
          },
          {
            "action": "permit",
            "src": "10.0.0.0/22",
            "dst": "10.100.0.0/24"
          }
        ]
      }
    }
    
    ---------- [Template] ----------
    {% for acl, acl_lines in access_lists.items() %}(1)
    ip access-list extended {{ acl }}
      {% for line in acl_lines %}(2)
        (3){% if line.action == "remark" %}
        remark {{ line.text }}
        (4){% elif line.action == "permit" %}
        permit {{ line.src }} {{ line.dst }}
        (5){% endif %}
      {% endfor %}(6)
    {% endfor %}(7)
    
    # All ACLs have been generated
    
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    
    # All ACLs have been generated
    
  • Pass the data using multiple extra vars:

    ➜ jinja render -t examples/templates/template.yaml \
    -e access_lists='{"al-hq-in": [{"action": "remark", "text": "Allow traffic from hq to local office"}, {"action": "permit", "src": "10.0.0.0/22", "dst": "10.100.0.0/24"}]}' \
    -e message=world \
    -v
    ---------- [ExtraVars] ----------
    {
      "access_lists": {
        "al-hq-in": [
          {
            "action": "remark",
            "text": "Allow traffic from hq to local office"
          },
          {
            "action": "permit",
            "src": "10.0.0.0/22",
            "dst": "10.100.0.0/24"
          }
        ]
      },
      "message": "world"
    } 
    
    ---------- [Template] ----------
    {% for acl, acl_lines in access_lists.items() %}(1)
    ip access-list extended {{ acl }}
      {% for line in acl_lines %}(2)
        (3){% if line.action == "remark" %}
        remark {{ line.text }}
        (4){% elif line.action == "permit" %}
        permit {{ line.src }} {{ line.dst }}
        (5){% endif %}
      {% endfor %}(6)
    {% endfor %}(7)
    hello {{ message }}!
    # All ACLs have been generated 
    
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    hello world!
    # All ACLs have been generated
    
  • Use directory option

    ➜ jinja render -d examples/data/data.yaml -t examples/ -o test/
    ➜ tree test/
    test/
    ├── config
    │   ├── example.ini
    │   └── example.properties
    ├── data
    │   ├── data.json
    │   └── data.yaml
    └── templates
        ├── lookup.yaml
        └── template.yaml
    
    3 directories, 6 files
    ➜ cat test/templates/template.yaml
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    hello jinja!
    # All ACLs have been generated
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2-tools-1.0.6.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

jinja2_tools-1.0.6-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file jinja2-tools-1.0.6.tar.gz.

File metadata

  • Download URL: jinja2-tools-1.0.6.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for jinja2-tools-1.0.6.tar.gz
Algorithm Hash digest
SHA256 fc3ba384349ed492bd82bc9d8241af7b872418f5e794a01046a219a943dc3c22
MD5 17ae6608d831ec3e5c0714f6539d6e20
BLAKE2b-256 3690b1eb051dbe81b86611df46807f1c2d6911cb56bfb6ebe464b28c106e43df

See more details on using hashes here.

File details

Details for the file jinja2_tools-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: jinja2_tools-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for jinja2_tools-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4ad0563e309ffbc7682916d83f713592538daebaf2200aba2fc6ab8883f8651d
MD5 bbacdce37e50dc1734e8fdb6f1aa2554
BLAKE2b-256 600101d857086db1fe60a84d20c3dd9646e793efcb66ddc3890d47665b4f0e03

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page