Ansible equivalent for desktop menu file replacement script?

Hi,

For the last decade or so, I’ve been using a bone-headed shell script to install and customize my various Linux desktops. Like this one for Rocky Linux 8 and KDE, for example.

I’m currently rewriting all this stuff for Ansible, since this is the way.

I’ve already converted something like 80 % of my Rocky Linux 8 + KDE setup script into an Ansible playbook and a corresponding collection of roles.

Now I’ve got a challenging problem to solve. My [setup.sh](https://gitlab.com/kikinovak/rocky-8-desktop/-/blob/main/setup.sh) script sports a replace_menus() function that does what it says: rewrite some *.desktop files in /usr/share/applications using a personal collection of menu entry stubs.

Here’s the function:

# Custom menus
ENTRIESDIR="${CWD}/el8/menus"
ENTRIES=$(ls ${ENTRIESDIR})
MENUDIRS="/usr/share/applications \
          /var/lib/flatpak/exports/share/applications"
...
replace_menus() {

  echo "  === Install custom desktop menu ==="
  echo
  sleep ${SLEEP}

  for MENUDIR in ${MENUDIRS}
  do

    for ENTRY in ${ENTRIES}
    do

      if [ -r ${MENUDIR}/${ENTRY} ]
      then

        echo "  Rewriting menu item: ${ENTRY}"
        sed -i '/^#/d' ${MENUDIR}/${ENTRY}
        sed -i '/^\[Desktop Action/Q' ${MENUDIR}/${ENTRY}
        sed -i '/^\[X-Drawing/Q' ${MENUDIR}/${ENTRY}
        sed -i '/^\[X-Property/Q' ${MENUDIR}/${ENTRY}
        sed -i '/^GenericName/d' ${MENUDIR}/${ENTRY}
        sed -i '/^Name/d' ${MENUDIR}/${ENTRY}
        sed -i '/^Comment/d' ${MENUDIR}/${ENTRY}
        sed -i '/^Keywords/d' ${MENUDIR}/${ENTRY}
        sed -i '/^Actions/d' ${MENUDIR}/${ENTRY}
        sed -i '/^Version/d' ${MENUDIR}/${ENTRY}
        sed -i '/^Categories/d' ${MENUDIR}/${ENTRY}
        sed -i '/^NoDisplay/d' ${MENUDIR}/${ENTRY}
        sed -i '/^$/d' ${MENUDIR}/${ENTRY}
        cat ${ENTRIESDIR}/${ENTRY} >> ${MENUDIR}/${ENTRY}
        sleep 0.1

      fi

    done

  done

And here’s the collection of menu stub files with improved translations and/or better categorizations.

Now I wonder how I could possibly translate this into Ansible’s way of doing things. I even wonder if Ansible is the right tool for doing this, or if I’m eventually better off with a simple menu_rewrite.sh shell script.

So I thought: why not ask the Ansible gurus here in this forum ?

I would say using the Ansible jinja2 templates option. You would then use the variables within Ansible to change the variables inside the jinja2 template. Then in the templates directory you put your desktop entries/icons/shortcuts, and use ansible to copy it to the appropriate location on your target systems.

An example of something I did, whilst I realise not a great example since firewalld can create new services anyway, it’ll at least give you an idea for how to format the template file, and how to distribute it:

  vars:
    services:
      - servicename: ssh-10022
        servicedesc: "SSH port 10022"
        serviceproto: tcp
        serviceport: 10022
      - servicename: checkmk
        servicedesc: "ChecMK port 6556"
        serviceproto: tcp
        serviceport: 6556

    - name: Create custom port services
      template:
        src: firewalld-service.j2
        dest: "/etc/firewalld/services/{{ item.servicename }}.xml"
      loop: "{{ services }}"

and the template file:

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>{{ item.servicename }}</short>
  <description>{{ item.servicedesc }}</description>
  <port protocol="{{ item.serviceproto }}" port="{{ item.serviceport }}"/>
</service>
1 Like

Just edited the above giving a working example of how that would work out for you.

1 Like