Installing Nerd Fonts is easy in Arch Linux: they are in the official repositories. That’s not true for Ubuntu and Fedora. Of course, installing them in these distributions is also not complicated. Still, it is a manual procedure: download the font archive, extract it in a specific directory, and regenerate the font cache.
That sounds like an automation task for Ansible!
I created an Ansible role to install my favorite Nerd Fonts in Ubuntu and Fedora: https://github.com/LorenzoBettini/my_nerd_fonts_role.
You can find the instructions for using such a role in your Ansible playbooks.
Here, I briefly describe the main parts.
The file “tasks/download-font.yml” defines the tasks for downloading and extracting the zip archive after creating the destination directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
--- - name: Check existing directory for {{ font }} stat: path: '/usr/local/share/fonts/{{ font }}' register: font_dir - name: Create directory for {{ font }} become: true ansible.builtin.file: path: '/usr/local/share/fonts/{{ font }}' mode: 0775 state: directory - name: Download and extract {{ font }} become: true ansible.builtin.unarchive: src: "https://github.com/ryanoasis/nerd-fonts/releases/latest/download/{{ font }}.tar.xz" dest: "/usr/local/share/fonts/{{ font }}" remote_src: true when: not font_dir.stat.exists |
The “tasks/main.yml” uses the above tasks, iterating through all the specified fonts to install. It also ensures that some packages are installed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
--- # tasks file for lorenzobettini.my_nerd_fonts_role - name: Ensure ca-certificates is present become: true ansible.builtin.package: state: present name: - ca-certificates - name: Ensure fc-cache is present become: true ansible.builtin.package: state: present name: - fontconfig - name: Override xz package name for Debian. ansible.builtin.set_fact: xz_package: xz-utils when: ansible_os_family == 'Debian' # unzip is useful for extracting zips - name: Install unzip and other compression utilities become: true ansible.builtin.package: state: present name: - unzip - "{{ xz_package }}" - name: Download and extract fonts to fonts directory. include_tasks: "download-font.yml" loop: "{{ fonts_to_install }}" loop_control: loop_var: font - name: Update font cache ansible.builtin.shell: fc-cache -f changed_when: false |
The variable “fonts_to_install” contains the list of font names to install based on the ZIP archives to download from the Nerd Fonts release site.
The file “vars/main.yml” contains my list of fonts to install:
1 2 3 4 5 6 7 8 9 |
--- # vars file for lorenzobettini.my_nerd_fonts_role xz_package: xz fonts_to_install: - JetBrainsMono - Meslo - FiraMono - Hack - CascadiaMono |
That’s all: I use this role in my playbooks, and it installs all the Nerd Fonts I like best.
Enjoy your Nerd Fonts! 🙂