# Center text in j2 template

**URL:** <https://forum.ansible.com/t/center-text-in-j2-template/6004>\
**Category:** Get Help\
**Created:** [May 26, 2024, 11:20am UTC](https://forum.ansible.com/t/center-text-in-j2-template/6004 "2024-05-26T11:20:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BiloxiGeek](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/biloxigeek/32/3568_2.png) [@BiloxiGeek](https://forum.ansible.com/u/BiloxiGeek)\
**Post date:** [May 26, 2024, 11:20am UTC](https://forum.ansible.com/t/center-text-in-j2-template/6004/1 "2024-05-26T11:20:25Z")

</div>

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:

```auto
*****************************
 ***<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.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [May 26, 2024, 1:25pm UTC](https://forum.ansible.com/t/center-text-in-j2-template/6004/2 "2024-05-26T13:25:03Z")

</div>

I’d suggest [using awk](https://superuser.com/a/915899) 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.

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [May 26, 2024, 1:58pm UTC](https://forum.ansible.com/t/center-text-in-j2-template/6004/3 "2024-05-26T13:58:16Z")

</div>

Jinja has an awesome `center` filter 😃 : [Template Designer Documentation — Jinja Documentation (3.1.x)](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.center)

```yaml
- 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)"

```

```bash
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   

```

---

<div class="post-metadata">

**Author:** ![BiloxiGeek](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/biloxigeek/32/3568_2.png) [@BiloxiGeek](https://forum.ansible.com/u/BiloxiGeek)\
**Post date:** [May 26, 2024, 2:13pm UTC](https://forum.ansible.com/t/center-text-in-j2-template/6004/4 "2024-05-26T14:13:33Z")

</div>

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

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

```

Results in:

```auto
********************************************************************
 **********
 *****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.
