Skip to content

Commit

Permalink
feat(): Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wickdChromosome committed May 19, 2024
1 parent e28a385 commit 5132988
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 72 deletions.
5 changes: 1 addition & 4 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Enter code owners here:
# https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners
#
# Example:
# * <your username>
* @wickdchromosome
62 changes: 32 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@

# Ansible Role - potos\_template
# XDG autostart role

Role to use as template for new roles of Potos Linux Clients.
A role for Potos, used to configure scripts that should run for users as they log in.

[![Test](https://github.com/projectpotos/ansible-role-potos_template/actions/workflows/test.yml/badge.svg)](https://github.com/projectpotos/ansible-role-potos_template/actions/workflows/test.yml)
A key feature is to only run the script once. So the first time the user logs in, the script runs, then disables itself.

## Example Playbook
# A picture is worth a thousand words
![Role chart](role.png)

As this role is tested via Molecule one can use [that
playbook](./molecule/default/converge.yml) as a starting point:
This role is supposed to be imported into the specs repo, and configured in the specs repo's vars/ directory.

```yaml
---
## Quickstart

- name: Converge
hosts: all
gather_facts: yes
tasks:
- name: run role
ansible.builtin.include_role:
name: 'ansible-role-potos_template'
If the role is only imported into the specs repo, but not configured with vars, it will deploy a script which sets the volume of the default sound sink to ~80% using pulseaudio.
To get started asap, import the role in the specs repo(files/templates/requirements.yml.j2):
```
- name: xdgautostart
src: git+https://github.com/wickdChromosome/ansible-role-potos_xdgautostart.git
version: 'master'
```

## Role Variables
The default variables are defined in [defaults/main.yml](./defaults/main.yml):
Now do an ansible-pull. In var/log/potos/ansible.log, you should be able to see the pulseaudio script being deployed.

```yaml
---
In `/usr/local/bin`, you should be able to see `pulseaudio-example.sh-wrapper.sh` and `pulseaudio-example.sh`.

# Example variable
potos_template_example_var: 'hello world!'
In `/etc/xdg/autostart` you will find pulseaudio-example.sh-wrapper.sh.desktop.

# List of items used for action xyz
potos_template_example_list: []
After logout and login, if runonlyonce is set(by default it is for this example), you will find `~/.config/autostart/pulseaudio-example.sh-wrapper.sh.desktop` as well, where autorun is disabled for the user you are logged in under for this script.

```
## If the script is already on the remote host
Existing script automation(maybe deployed by another role under /usr/local/bin) is also supported.
However, in that case the potos_xdgautostart list should refer to the script as $deployed_script + .j2

Another option is to use `ansible-doc` to read the argument specification:
For example, if the script is under `/usr/local/bin/example.sh`, the script should be present in the list as `script: "example.sh.j2"`, even
if there is no existing .j2 template for the script.

```sh
ansible-doc --type role -r . main ansible-role-potos_template
## Vars
Here, after importing the role in the specs repo:
```
- potos_xdgautostart: # List of scripts to autostart
- script: "test_1.sh.j2" # Name of script to run on user login(stored in files/potos_xdgautostart for specs repo)
runonlyonce: yes # For each user, run the script once and only once on first login
my_var_to_template: "1245" # If test_1.sh.j2 has $my_var_to_template, it can be templated as {{ item.my_var_to_template }} in test_1.sh.j2
- script: "test_2.sh.j2"
runonlyonce: no # For each user, run the script on every login
```

## Requirements

N/A
The script specified to be deployed must exist either under templates/ as specified, or on the remote host already deployed.

## License

Expand Down
11 changes: 6 additions & 5 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---

# Example variable
potos_template_example_var: 'hello world!'

# List of items used for action xyz
potos_template_example_list: []
# Int between 0 and 65536(0-100%), for setting
# PulseAudio default volume
potos_xdg_autostart:
- script: "pulseaudio_example.sh.j2"
runonlyonce: true
potos_pulseaudio_default_volume: 50000
6 changes: 6 additions & 0 deletions files/disable_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Disable this script
script_name=$(basename "$0")
mkdir -p "$HOME/.config/autostart/"
cp "/etc/xdg/autostart/$script_name.desktop" "$HOME/.config/autostart/$script_name.desktop"
sed -i 's/X-GNOME-Autostart-enabled=true/X-GNOME-Autostart-enabled=false/g' "$HOME/.config/autostart/$script_name.desktop"
chmod +x "$HOME/.config/autostart/$script_name.sh.desktop"
16 changes: 5 additions & 11 deletions meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

argument_specs:
main:
short_description: Role to use as template for new roles of Potos Linux Clients.
short_description: Role to use to run scripts automatically on user login, either once or recurringly
options:
potos_template_example_var:
potos_xdg_autostart:
description:
- Example variable
type: str
required: no
default: 'hello world!'
potos_template_example_list:
description:
- List of items used for action xyz
- List of scripts for deployment
type: list
elements: str
required: no
elements: dict
required: false
default: []
4 changes: 2 additions & 2 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

galaxy_info:
author: Project Potos
description: ole to use as template for new roles of Potos Linux Clients.
description: Role to use as a way to auto-execute on scripts on login
company: Project Potos
role_name: potos_wallpaper
role_name: potos_xdgautostart
namespace: projectpotos
license: GPL-3.0-only
min_ansible_version: '2.12'
Expand Down
14 changes: 11 additions & 3 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

- name: Converge
hosts: all
gather_facts: yes
gather_facts: true
vars:
potos_xdg_autostart:
- script: "test_1.sh.j2"
runonlyonce: true
- script: "test_2.sh.j2"
runonlyonce: false

tasks:
- name: run role

- name: Run role
ansible.builtin.include_role:
name: 'ansible-role-potos_template'
name: 'ansible-role-potos_xdgautostart'
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

touch $HOME/"$0"_worked.txt

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

touch $HOME/"$0"_worked.txt

7 changes: 5 additions & 2 deletions molecule/default/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

- name: Prepare
hosts: all
gather_facts: yes
gather_facts: true

tasks: []
tasks:
- name: Make directory for gnome autostart script(w/o installing gnome)
changed_when: false
ansible.builtin.command: 'mkdir -p /etc/xdg/autostart' # noqa: command-instead-of-module
62 changes: 57 additions & 5 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,65 @@

- name: Verify
hosts: all
gather_facts: no

gather_facts: false
tasks:
- name: check if template has been applied
# test templates
- name: Check if template for script desktop file has been applied
ansible.builtin.stat:
path: "/etc/xdg/autostart/test_1.sh_wrapper.sh.desktop"
register: out
failed_when:
- not out.stat.exists

- name: Check if template for script has been applied
ansible.builtin.stat:
path: "/potos.example"
path: "/usr/local/bin/test_1.sh"
register: out
changed_when: false
failed_when:
- not out.stat.exists

- name: Check if template for wrapper script has been applied
ansible.builtin.stat:
path: "/usr/local/bin/test_1.sh_wrapper.sh"
register: out
failed_when:
- not out.stat.exists

# test that the wrapper doesn't have disable lines when it shouldn't(if the script is set to run on every login)
- name: Check that the test case where runonlyonce is true does have the disable script
ansible.builtin.lineinfile:
path: "/usr/local/bin/test_1.sh_wrapper.sh"
line: "# BEGIN ANSIBLE MANAGED BLOCK"
state: present
register: out
failed_when:
- out.changed

- name: Check that the test case where runonlyonce is false doesnt have the disable script
ansible.builtin.lineinfile:
path: "/usr/local/bin/test_2.sh_wrapper.sh"
line: "BEGIN ANSIBLE MANAGED BLOCK"
state: absent
register: out
failed_when:
- out.changed

# test that the desktop file points to the correct wrapper script
- name: Test that the desktop file points to the correct wrapper script
ansible.builtin.lineinfile:
path: "/etc/xdg/autostart/test_2.sh_wrapper.sh.desktop"
line: "Exec=/usr/local/bin/test_2.sh_wrapper.sh"
state: present
register: out
failed_when:
- out.changed

# test that the wrapper script points to the correct path for the main script it wraps
- name: Test that the wrapper script points to the correct path for the main script it wraps
ansible.builtin.lineinfile:
path: "/etc/xdg/autostart/test_2.sh_wrapper"
line: "/usr/local/bin/test_2.sh"
state: present
register: out
failed_when:
- out.changed
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ansible>=2.12
# Required for testing

## ansible/yaml lint
ansible-lint~=24.5.0
ansible-lint~=24.2.2
yamllint~=1.35.1

## molecule tests
Expand Down
Binary file added role.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 47 additions & 6 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
---

- name: Example task to deploy a template
# Loop over all script names, make an autostart desktopfile for each
- name: Copy over the desktop file to the new dir
ansible.builtin.template:
src: 'potos.example.j2'
dest: '/potos.example'
owner: root
group: root
mode: '0755'
src: "potos_xdgautostart/etc/xdg/autostart/desktopfile.j2"

Check warning on line 6 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: /etc/xdg/autostart/{{ item.script | replace('.j2','') }}_wrapper.sh.desktop -> /etc/xdg/autostart/{{ item.script | replace('.j2', '') }}_wrapper.sh.desktop

Check warning on line 6 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: /etc/xdg/autostart/{{ item.script | replace('.j2','') }}_wrapper.sh.desktop -> /etc/xdg/autostart/{{ item.script | replace('.j2', '') }}_wrapper.sh.desktop
dest: "/etc/xdg/autostart/{{ item.script | replace('.j2','') }}_wrapper.sh.desktop"
force: true
mode: '0777'
loop: "{{ potos_xdg_autostart }}"

# Copy over script file to global autostart dir
- name: Copy over the script file to the new dir
ansible.builtin.template:
src: "potos_xdgautostart/usr/local/bin/{{ item.script }}"

Check warning on line 15 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }} -> /usr/local/bin/{{ item.script | replace('.j2', '') }}

Check warning on line 15 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }} -> /usr/local/bin/{{ item.script | replace('.j2', '') }}
dest: "/usr/local/bin/{{ item.script | replace('.j2','') }}"
force: true
mode: '0777'
loop: "{{ potos_xdg_autostart }}"
failed_when: false

# Check to make sure target script file is on the remote host,
# either because it was copied over or it was already there
- name: Make sure target script file is present
ansible.builtin.stat:
path: "/usr/local/bin/{{ item.script | replace('.j2','') }}"

Check warning on line 26 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }} -> /usr/local/bin/{{ item.script | replace('.j2', '') }}

Check warning on line 26 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }} -> /usr/local/bin/{{ item.script | replace('.j2', '') }}
register: file_stat
failed_when: not file_stat.stat.exists
loop: "{{ potos_xdg_autostart }}"

# Create the script wrapper file to global autostart dir
- name: Copy over the script file wrapper to the new dir
ansible.builtin.lineinfile:
path: "/usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh"

Check warning on line 34 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }} -> /usr/local/bin/{{ item.script | replace('.j2', '') }}

Check warning on line 34 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh -> /usr/local/bin/{{ item.script | replace('.j2', '') }}_wrapper.sh

Check warning on line 34 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: ^/usr/local/bin/{{ item.script | replace('.j2','') }}$ -> ^/usr/local/bin/{{ item.script | replace('.j2', '') }}$

Check warning on line 34 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }} -> /usr/local/bin/{{ item.script | replace('.j2', '') }}

Check warning on line 34 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh -> /usr/local/bin/{{ item.script | replace('.j2', '') }}_wrapper.sh

Check warning on line 34 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: ^/usr/local/bin/{{ item.script | replace('.j2','') }}$ -> ^/usr/local/bin/{{ item.script | replace('.j2', '') }}$
line: "/usr/local/bin/{{ item.script | replace('.j2','') }}"
regexp: "^/usr/local/bin/{{ item.script | replace('.j2','') }}$"
state: present
backup: false
create: true
mode: '0777'
loop: "{{ potos_xdg_autostart }}"

# Append disable script to each copied over script
- name: Add disable script to wrapper
ansible.builtin.blockinfile:
path: "/usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh"

Check warning on line 46 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu20)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh -> /usr/local/bin/{{ item.script | replace('.j2', '') }}_wrapper.sh

Check warning on line 46 in tasks/main.yml

View workflow job for this annotation

GitHub Actions / test (ubuntu22)

jinja[spacing]

Jinja2 spacing could be improved: /usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh -> /usr/local/bin/{{ item.script | replace('.j2', '') }}_wrapper.sh
block: "{{ lookup('file', 'files/disable_script.sh') }}"
state: present
loop: "{{ potos_xdg_autostart }}"
when: item.runonlyonce
3 changes: 0 additions & 3 deletions templates/potos.example.j2

This file was deleted.

8 changes: 8 additions & 0 deletions templates/potos_xdgautostart/etc/xdg/autostart/desktopfile.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ ansible_managed | comment }}
[Desktop Entry]
Type=Application
Name=Potos autostart script for {{ item.script | replace('.j2','') }}
Comment=Potos autostart script for {{ item.script | replace('.j2','') }}
Exec=/usr/local/bin/{{ item.script | replace('.j2','') }}_wrapper.sh
X-GNOME-AutoRestart=false
X-GNOME-Autostart-enabled=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

{{ ansible_managed | comment }}

# Set default sink volume for user
potos_pulseaudio_defsink=$( pactl info | awk '/Default Sink:/ {print $3}' )
pactl set-sink-mute $potos_pulseaudio_defsink 0 # Make sure its unmuted
pactl set-sink-volume $potos_pulseaudio_defsink "{{ item.potos_pulseaudio_default_volume }}"
3 changes: 3 additions & 0 deletions test_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

MOLECULE_DISTRO="ubuntu22" molecule test

0 comments on commit 5132988

Please sign in to comment.