2

I'm trying to write my first ansible playbook to setup my Arch Linux workstations and servers. So far everything worked fine but now I've run into a problem I can't really wrap my head around.

I'm trying to have multiple roles that require changes to the mkinitcpio.conf file, often in the same line.

At the moment I have a role mkinitcpio which generates the /etc/mkinitcpio.conf file based on a template.

group_vars/all.yml

encryption
  enabled: true

roles/mkinitcpio/tasks/main.yml

- name: Generate mkinitcpio.conf
  template:
    src: mkinitcpio.conf.j2
    dest: /etc/mkinitcpio.conf
  notify:
    - generate mkinitcpio

roles/mkinitcpio/handlers/main.yml

- name: generate mkinitcpio
  comand: mkinitcpio -p linux

roles/mkinitcpio/templates/mkinitcpio.conf.j2

HOOKS=(base systemd autodetect keyboard sd-vconsole modconf block {% if encryption.enabled == true %}sdencrypt {% endif %}sd-lvm2 filesystems fsck)

Now I have a second role plymouth that also has to update the mkinitcpio.conf, dependend if it will be installed or not. It will have to add sd-plymouth between systemd and autodetect and notify generate mkinitcpio after that.

The solutions I came up with so far are: 1.) Make the plymouth role dependend on the mkinitcpio role. The plymouth role installs plymouth, than modifies the HOOKS line via the lineinfile module. After that, it notifies generate mkinitcpio if needed.

This solution will get very complicated, if more roles will be added, that may update the mkinitcpio.conf file.

2.) Add another conditional to roles/mkinitcpio/templates/mkinitcpio.conf.j2:

HOOKS=(base systemd {% if plymouth == true %}sd-plymouth {% endif %}autodetect keyboard sd-vconsole modconf block {% if encryption.enabled == true %}sdencrypt {% endif %}sd-lvm2 filesystems fsck)

Make the mkinitcpio role dependend on the plymouth role, so that it will install plymouth at first. After that, the mkinitcpio role will update the complete mkinitcpio.conf file and notify generate mkinitcpio if needed.

This solution does not seem right to me, because it has a "reversed dependency".

I hope I described my problem understandable and you can give me some tips about the best way to solve it. Thank you in advance!

Marc
  • 41
  • 2

0 Answers0