From ed405fe273c54937bbdc4cad9f28dcb68aa57330 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 30 Nov 2022 09:40:47 +0100 Subject: [PATCH] First commit --- README.md | 40 +++++++++++++++++++++ tasks/main.yml | 39 ++++++++++++++++++++ templates/51-swaylock_keybindings.config.j2 | 13 +++++++ templates/config.j2 | 12 +++++++ 4 files changed, 104 insertions(+) create mode 100644 README.md create mode 100644 tasks/main.yml create mode 100644 templates/51-swaylock_keybindings.config.j2 create mode 100644 templates/config.j2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..3eca8a7 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +Swaylock Ansible Role +--------------------- + +Install and configure [swaylock](https://github.com/swaywm/swaylock) + +## Variables + +You can define 3 variables for this role, all are optionnals. + +`swaylock_flags` list options which do not need valus like `daemonize`: +```yaml +--- +swaylock_flags: + - ignore-empty-password + - show-failed-attempts + - daemonize +``` + +`swaylock_options` list key-value type options like `image` or all colors +related stuff: + +```yaml +swaylock_options: + image: '~/documents/ressources/wallpapers/psychonauts.jpg' + +``` + +You can check *Swaylock* manual pages (`man swaylock`) for all options. + +The last variable, `swaylock_keybinding` list *Swaylock* related keybinding for +*Sway*. Check my [Sway Ansible role](https://git.epha.se/ephase/ansible-sway) +for managing this variable. + +```yaml +swaylock_keybindings: + - mode: normal + bindings: + - bindsym: '$mod+alt+l' + action: 'exec swaylock' +``` diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..37b0e50 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,39 @@ +--- +- name: Install Swaylock package + ansible.builtin.package: + name: swaylock + state: present + become: true + +- name: Create Swaylock config dirs + ansible.builtin.file: + path: '{{ ansible_user_dir ~ "/.config/swaylock" }}' + state: directory + mode: 0750 + owner: '{{ ansible_user_uid }}' + group: '{{ ansible_user_gid }}' + +- name: Render templates for Swaylock config file + ansible.builtin.template: + src: config.j2 + dest: '{{ ansible_user_dir}}/.config/swaylock/config' + owner: '{{ ansible_user_uid }}' + group: '{{ ansible_user_gid }}' + lstrip_blocks: yes + trim_blocks: yes + mode: 0640 + +- name: Render Swaylock keybinding for sway config file + ansible.builtin.template: + src: '{{ item }}.config.j2' + dest: '{{ ansible_user_dir ~ "/.config/sway/conf.d/" ~ item ~ ".config" }}' + owner: '{{ ansible_user_uid }}' + group: '{{ ansible_user_gid }}' + lstrip_blocks: true + trim_blocks: true + mode: 0640 + validate: >- + sh -c "cat {{ansible_user_dir}}/.config/sway/conf.d/* %s > /tmp/sway; if sway --config /tmp/sway --validate 2>&1 | grep -q sway/config.c; then >&2 echo \"Error validating sway configuration\"; rm /tmp/sway; exit 1; else rm /tmp/sway; exit 0; fi" + loop: + - 51-swaylock_keybindings + any_errors_fatal: true diff --git a/templates/51-swaylock_keybindings.config.j2 b/templates/51-swaylock_keybindings.config.j2 new file mode 100644 index 0000000..7bb7eff --- /dev/null +++ b/templates/51-swaylock_keybindings.config.j2 @@ -0,0 +1,13 @@ +{% for section in swaylock_keybindings %} +{% if section.mode != "normal" %} +mode {{ section.mode }} { +{% endif %} + +{% for bind in section.bindings %} +bindsym {{ bind.bindsym }} {{ bind.action}} +{% endfor %} + +{% if section.mode != "normal" %} +} +{% endif %} +{% endfor %} diff --git a/templates/config.j2 b/templates/config.j2 new file mode 100644 index 0000000..1f64b59 --- /dev/null +++ b/templates/config.j2 @@ -0,0 +1,12 @@ +# {{ ansible_managed }} +{% if swaylock_flags is defined %} +{% for flag in swaylock_flags %} +{{ flag }} +{% endfor %} +{% endif %} +{% if swaylock_options is defined %} + +{% for key, value in swaylock_options.items() %} +{{ key }}={{ value }} +{% endfor %} +{% endif %}