From ae4c3b85c0c8b41b6e825ca4ef7769b6a9db44b8 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 4 Dec 2022 22:50:01 +0100 Subject: [PATCH] First commit --- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++ files/swayidle.service | 11 ++++++++ meta/main.yml | 8 ++++++ tasks/main.yml | 43 ++++++++++++++++++++++++++++++ templates/config.j2 | 17 ++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 README.md create mode 100644 files/swayidle.service create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/config.j2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa35754 --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +Swayidle ansible role +--------------------- + +Install and configure [swayidle](https://github.com/swaywm/swayidle) + +## Variables + +This role only need one variable: `swayidle_config`. Here is an example : + +```yaml +swayidle_config: + timeouts: + - seconds: 300 + command: swaylock -f + - seconds: 600 + command: swaymsg "output * dpms off" + resume: 'swaymsg "output * dpms on"' + events: + before-sleep: swaylock -f + lock: swaylock -f +``` + +This variable contains two others: `timeouts` and `events`. + +`timeouts` is an array of: + + * `seconds`: numbers of second to wait before trigger the event + * `command`: command to execute when the event trigger + * `resume`: command to execute when resume + +`events` contains keys:values about others events. Key contains event name and +value command to execute. + +Please refer to man page for details: `man 1 swayidle`. + +## Licence + +This script is released under le [MIT licence][l_mit] + +Copyright © 2022 Yorick Barbanneau + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +[l_mit]:https://opensource.org/licenses/mit-license.php diff --git a/files/swayidle.service b/files/swayidle.service new file mode 100644 index 0000000..5579a87 --- /dev/null +++ b/files/swayidle.service @@ -0,0 +1,11 @@ +[Unit] +Description=Swayidle service +Documentation=https://github.com/swaywm/swayidle +BindsTo=sway-session.target + +[Service] +Type=simple +ExecStart=/usr/bin/swayidle -w + +[Install] +WantedBy=sway-session.target diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..76f2dde --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,8 @@ +--- +galaxy_info: + role_name: ansible_swayidle + author: ephase + description: install and configure Swayidle + issue_tracker_url: https://git.epha.se/ephase/ansible-swayidle + license: MIT + min_ansible_version: '1.4' diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..87e5e0a --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,43 @@ +--- +- name: Install Swayidle package + ansible.builtin.package: + name: swayidle + state: present + become: true + +- name: Create Swayidle config dirs + ansible.builtin.file: + path: '{{ ansible_user_dir ~ "/.config/swayidle" }}' + state: directory + mode: 0750 + owner: '{{ ansible_user_uid }}' + group: '{{ ansible_user_gid }}' + +- name: Render templates for Swayidle config file + ansible.builtin.template: + src: config.j2 + dest: '{{ ansible_user_dir}}/.config/swayidle/config' + owner: '{{ ansible_user_uid }}' + group: '{{ ansible_user_gid }}' + lstrip_blocks: yes + trim_blocks: yes + mode: 0640 + +- name: copy Swayidle systemd service file + ansible.builtin.copy: + src: 'swayidle.service' + dest: '{{ ansible_user_dir }}/.config/systemd/user/' + mode: 0640 + register: service_file + +- block: + - name: Reload Systemd daemon + ansible.builtin.systemd: + scope: user + daemon-reload: true + - name: Activate Swayidle service + ansible.builtin.systemd: + name: 'swayidle.service' + scope: user + state: started + when: service_file is changed and not ansible_check_mode diff --git a/templates/config.j2 b/templates/config.j2 new file mode 100644 index 0000000..52602d7 --- /dev/null +++ b/templates/config.j2 @@ -0,0 +1,17 @@ +# {{ ansible_managed }} +{% if swayidle_config is defined %} +{% if swayidle_config['timeouts'] is defined %} +{% for timeout in swayidle_config['timeouts'] %} +timeout {{ timeout['seconds'] }} '{{ timeout['command'] }}' +{%- if timeout['resume'] is defined %} +{{ ' resume' }} '{{ timeout['resume'] }}' +{% endif %} + +{% endfor %} +{% endif %} +{% if swayidle_config['events'] is defined %} +{% for event, command in swayidle_config['events'].items() %} +{{ event }} '{{ command }}' +{% endfor %} +{% endif %} +{% endif %}