Center text in j2 template

I’m trying to work up a template for a text file and can’t figure out how to get a string centered in a set number of columns.

One thing I’m trying to do is write a new /etc/motd file that will have as part of it a line with 3 ‘*’ characters at the beginning and end of the line that is 60 characters long, with ansible_fact[hostname] centered on the line. The hostnames are all different lengths so I need to rely on some sort of formatting string to center the text. Basically like this:

*****************************
***     <insert text>     ***
*****************************

I can get the desired format in python but haven’t figured out how to embed that in the playbook to be included in the resulting file. I think the best way would be to figure out the proper syntax in a j2 template but haven’t had much luck there either.

This hostname line in motd is just one instance of a longer list of templates I need to write, but it’s a good representation of the overall need.

I’d suggest using awk or another tool for this, if it has to be done using Jinja2 I guess you would need to count the number of characters per line, deduct that from the line length and half it to work out what padding is required.

Jinja has an awesome center filter :smiley: : Template Designer Documentation — Jinja Documentation (3.1.x)

- hosts: localhost
  gather_facts: false
  vars:
    motd_banner: |
      {{ '*' * motd_width }}
      {{ '*' * motd_border_width }}{{ motd_centered }}{{ '*' * motd_border_width }}
      {{ '*' * motd_width }}
    motd_centered: "{{ motd | center(motd_width - (motd_border_width * 2)) }}"
    motd_border_width: 3
    motd_width: 60

  tasks:
    - ansible.builtin.debug:
        var: motd_banner
      vars:
        motd: "This is the message of the day!"

    - ansible.builtin.debug:
        var: motd_banner
      vars:
        motd: "If the number of characters is even (40)"

    - ansible.builtin.debug:
        var: motd_banner
      vars:
        motd: "If the number of characters is odd (39)"
PLAY [localhost] *************************************************************************************************

TASK [ansible.builtin.debug] *************************************************************************************
ok: [localhost] => 
    motd_banner: |-
        ************************************************************
        ***           This is the message of the day!            ***
        ************************************************************

TASK [ansible.builtin.debug] *************************************************************************************
ok: [localhost] => 
    motd_banner: |-
        ************************************************************
        ***       If the number of characters is even (40)       ***
        ************************************************************

TASK [ansible.builtin.debug] *************************************************************************************
ok: [localhost] => 
    motd_banner: |-
        ************************************************************
        ***       If the number of characters is odd (39)        ***
        ************************************************************

PLAY RECAP *******************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
3 Likes

Found the syntax after a bunch of trial and error just now:

********************************************************************
*****                                                          *****
*****{{ "{:^58}".format(ansible_facts.hostname) }}*****
*****                                                          *****
********************************************************************
*****{{ "{:^58}".format('%Y%m%d / %H%ML' | strftime(ansible_date_time.epoch)) }}*****
********************************************************************

Results in:

********************************************************************
*****                                                          *****
*****                         thishost                         *****  
*****                                                          *****
********************************************************************
*****                     20240526 / 0908L                     *****
********************************************************************

I’ve got other info in the actual motd but this gives a good idea of how to format it and what it ends up looking like.

4 Likes