diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..bc64645 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,82 @@ +--- +stages: + - verify + - test-default + - test-with-custom-ascii + +image: + name: registry.ednz.fr/forge/ansible-runner + +variables: + ANSIBLE_HOST_KEY_CHECKING: 'false' + ANSIBLE_FORCE_COLOR: 'true' + ANSIBLE_PYTHON_INTERPRETER: /usr/bin/python3 + DOCKER_AUTH_CONFIG: $CI_DOCKER_AUTH_CONFIG + +.stage-test-default: + stage: test-default + +.stage-test-with-custom-ascii: + stage: test-with-custom-ascii + +.variables-ubuntu-2004: + variables: + MOLECULE_TEST_OS: "ubuntu2004" + +.variables-ubuntu-2204: + variables: + MOLECULE_TEST_OS: "ubuntu2204" + +.variables-debian-11: + variables: + MOLECULE_TEST_OS: "debian11" + +.script-molecule-test-default: + script: + - molecule test + +.script-molecule-test-with-custom-ascii: + script: + - molecule test -s with_custom_repo + +ansible-verify: + stage: verify + script: + - yamllint . -c .yamllint + - ansible-lint + +ansible-test-ubuntu-2004-default: + extends: + - .stage-test-default + - .variables-ubuntu-2004 + - .script-molecule-test-default + +ansible-test-ubuntu-2204-default: + extends: + - .stage-test-default + - .variables-ubuntu-2204 + - .script-molecule-test-default + +ansible-test-debian-11-default: + extends: + - .stage-test-default + - .variables-debian-11 + - .script-molecule-test-default + +ansible-test-ubuntu-2004-with-custom-ascii: + extends: + - .stage-test-with-custom-ascii + - .variables-ubuntu-2004 + - .script-molecule-test-with-custom-ascii + +ansible-test-ubuntu-2204-with-custom-ascii: + extends: + - .stage-test-with-custom-ascii + - .variables-ubuntu-2204 + - .script-molecule-test-with-custom-ascii + +ansible-test-debian-11-with-custom-ascii: + extends: + - .stage-test-with-custom-ascii + - .variables-debian-11 + - .script-molecule-test-with-custom-ascii diff --git a/defaults/main.yml b/defaults/main.yml index 4197d24..3aa2cea 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,6 @@ --- # defaults file for update_motd +update_motd_filename: "00-motd-neofetch" update_motd_print_info: - name: "CPU" module: "cpu" diff --git a/files/motd.sh b/files/motd.sh index 77e6ec0..d3bbe7e 100644 --- a/files/motd.sh +++ b/files/motd.sh @@ -1,3 +1,3 @@ -#! /bin/bash +#! /bin/sh neofetch --config /etc/profile.d/motd.cfg \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml index 963038d..1d5bc88 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,3 +1,4 @@ +--- galaxy_info: namespace: 'ednxzu' role_name: 'update_motd' diff --git a/meta/requirements.yml b/meta/requirements.yml new file mode 100644 index 0000000..2b3cea7 --- /dev/null +++ b/meta/requirements.yml @@ -0,0 +1,4 @@ +--- +# meta file for update_motd +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml index e9320f9..ca250b7 100644 --- a/molecule/default/requirements.yml +++ b/molecule/default/requirements.yml @@ -1,3 +1,4 @@ --- # requirements file for molecule -roles: [] +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 0cff669..ee590ba 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -3,8 +3,28 @@ def test_hosts_file(host): """Validate /etc/hosts file.""" - f = host.file("/etc/hosts") + etc_hosts = host.file("/etc/hosts") + assert etc_hosts.exists + assert etc_hosts.user == "root" + assert etc_hosts.group == "root" - assert f.exists - assert f.user == "root" - assert f.group == "root" +def test_motd_file(host): + """Validate motd.cfg file.""" + motd_cfg = host.file("/etc/profile.d/motd.cfg") + dist_os = host.system_info.distribution + assert motd_cfg.exists + assert motd_cfg.user == "root" + assert motd_cfg.group == "root" + assert motd_cfg.mode == 0o644 + assert motd_cfg.contains("print_info()") + assert motd_cfg.contains("ascii_distro=\"" + dist_os + "_small\"") + +def test_motd_file(host): + """Validate 00-motd-neofetch file.""" + motd_neofetch = host.file("/etc/update-motd.d/00-motd-neofetch") + assert motd_neofetch.exists + assert motd_neofetch.user == "root" + assert motd_neofetch.group == "root" + assert motd_neofetch.mode == 0o744 + assert motd_neofetch.contains("#! /bin/sh") + assert motd_neofetch.contains("neofetch --config /etc/profile.d/motd.cfg") \ No newline at end of file diff --git a/molecule/with_custom_ascii/converge.yml b/molecule/with_custom_ascii/converge.yml new file mode 100644 index 0000000..8c00c25 --- /dev/null +++ b/molecule/with_custom_ascii/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.update_motd" + ansible.builtin.include_role: + name: "ednxzu.update_motd" diff --git a/molecule/with_custom_ascii/group_vars/all.yml b/molecule/with_custom_ascii/group_vars/all.yml new file mode 100644 index 0000000..3038409 --- /dev/null +++ b/molecule/with_custom_ascii/group_vars/all.yml @@ -0,0 +1,12 @@ +--- +update_motd_filename: "00-motd-neofetch" +update_motd_print_info: + - name: "CPU" + module: "cpu" + - name: "Memory" + module: "memory" + - name: "Local IP" + module: local_ip + - name: "Disk" + module: "disk" +update_motd_ascii_distro: "{{ ansible_distribution }}_small" diff --git a/molecule/with_custom_ascii/molecule.yml b/molecule/with_custom_ascii/molecule.yml new file mode 100644 index 0000000..7a97a52 --- /dev/null +++ b/molecule/with_custom_ascii/molecule.yml @@ -0,0 +1,41 @@ +--- +dependency: + name: galaxy + options: + requirements-file: ./requirements.yml +driver: + name: docker +platforms: + - name: instance + image: geerlingguy/docker-${MOLECULE_TEST_OS}-ansible + command: "" + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup + cgroupns_mode: host + privileged: true + pre_build_image: true +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp/.ansible +verifier: + name: testinfra +lint: | + yamllint -c .yamllint . + ansible-lint +scenario: + name: with_custom_ascii + test_sequence: + - dependency + - lint + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/with_custom_ascii/requirements.yml b/molecule/with_custom_ascii/requirements.yml new file mode 100644 index 0000000..ca250b7 --- /dev/null +++ b/molecule/with_custom_ascii/requirements.yml @@ -0,0 +1,4 @@ +--- +# requirements file for molecule +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/with_custom_ascii/tests/conftest.py b/molecule/with_custom_ascii/tests/conftest.py new file mode 100644 index 0000000..f7ddb3f --- /dev/null +++ b/molecule/with_custom_ascii/tests/conftest.py @@ -0,0 +1,22 @@ +"""PyTest Fixtures.""" +from __future__ import absolute_import + +import os + +import pytest + + +def pytest_runtest_setup(item): + """Run tests only when under molecule with testinfra installed.""" + try: + import testinfra + except ImportError: + pytest.skip("Test requires testinfra", allow_module_level=True) + if "MOLECULE_INVENTORY_FILE" in os.environ: + pytest.testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ["MOLECULE_INVENTORY_FILE"] + ).get_hosts("all") + else: + pytest.skip( + "Test should run only from inside molecule.", allow_module_level=True + ) diff --git a/molecule/with_custom_ascii/tests/test_default.py b/molecule/with_custom_ascii/tests/test_default.py new file mode 100644 index 0000000..c2e6fb3 --- /dev/null +++ b/molecule/with_custom_ascii/tests/test_default.py @@ -0,0 +1,28 @@ +"""Role testing files using testinfra.""" + + +def test_hosts_file(host): + """Validate /etc/hosts file.""" + etc_hosts = host.file("/etc/hosts") + assert etc_hosts.exists + assert etc_hosts.user == "root" + assert etc_hosts.group == "root" + +def test_motd_file(host): + """Validate motd.cfg file.""" + motd_cfg = host.file("/etc/profile.d/motd.cfg") + assert motd_cfg.exists + assert motd_cfg.user == "root" + assert motd_cfg.group == "root" + assert motd_cfg.mode == 0o644 + assert motd_cfg.contains("print_info()") + +def test_motd_file(host): + """Validate 00-motd-neofetch file.""" + motd_neofetch = host.file("/etc/update-motd.d/00-motd-neofetch") + assert motd_neofetch.exists + assert motd_neofetch.user == "root" + assert motd_neofetch.group == "root" + assert motd_neofetch.mode == 0o744 + assert motd_neofetch.contains("#! /bin/sh") + assert motd_neofetch.contains("neofetch --config /etc/profile.d/motd.cfg") \ No newline at end of file diff --git a/tasks/cleanup.yml b/tasks/cleanup.yml index 1295082..3e32039 100644 --- a/tasks/cleanup.yml +++ b/tasks/cleanup.yml @@ -23,3 +23,9 @@ path: "{{ item.path }}" state: absent loop: "{{ collected_files.files + collected_directories.files }}" + when: (item.path|basename) != update_motd_filename + +- name: "Remove /etc/motd file" + ansible.builtin.file: + path: "/etc/motd" + state: absent diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..ddbe222 --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,17 @@ +--- +# task/configure file for update_motd +- name: "Copy configuration file for neofetch motd" + ansible.builtin.template: + src: "motd.cfg.j2" + dest: "{{ update_motd_config_path }}/motd.cfg" + owner: root + group: root + mode: '0644' + +- name: "Copy motd script file" + ansible.builtin.copy: + src: files/motd.sh + dest: "{{ update_motd_path }}/{{ update_motd_filename }}" + mode: '0744' + owner: root + group: root diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..524ea59 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,10 @@ +--- +# task/install file for update_motd +- name: "Install neoftech" + ansible.builtin.include_role: + name: ednxzu.manage_apt_packages + vars: + manage_apt_packages_list: + - name: neofetch + version: latest + state: present diff --git a/tasks/main.yml b/tasks/main.yml index 2d2a797..6993979 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,8 +1,10 @@ --- # task/main file for update_motd -- name: "Debug" - ansible.builtin.debug: - msg: "{{ ansible_distribution }}" +- name: "Import install.yml" + ansible.builtin.include_tasks: install.yml - name: "Import cleanup.yml" - ansible.builtin.include_tasks: cleanup.yml \ No newline at end of file + ansible.builtin.include_tasks: cleanup.yml + +- name: "Import configure.yml" + ansible.builtin.include_tasks: configure.yml diff --git a/templates/motd.cfg.j2 b/templates/motd.cfg.j2 index 04c922a..433df8d 100644 --- a/templates/motd.cfg.j2 +++ b/templates/motd.cfg.j2 @@ -3,76 +3,75 @@ print_info() { info title info underline -{% for module in alpine_motd_print_info %} +{% for module in update_motd_print_info %} info "{{ module.name }}" {{ module.module }} {% endfor %} - info cols } -title_fqdn="{{ alpine_motd_title_fqdn }}" -kernel_shorthand="{{ alpine_motd_kernel_shorthand }}" -distro_shorthand="{{ alpine_motd_distro_shorthand }}" -os_arch="{{ alpine_motd_os_arch }}" -uptime_shorthand="{{ alpine_motd_uptime_shorthand }}" -memory_percent="{{ alpine_motd_memory_percent }}" -memory_unit="{{ alpine_motd_memory_unit }}" -package_managers="{{ alpine_motd_package_managers }}" -shell_path="{{ alpine_motd_shell_path }}" -shell_version="{{ alpine_motd_shell_version }}" -speed_type="{{ alpine_motd_speed_type }}" -speed_shorthand="{{ alpine_motd_speed_shorthand }}" -cpu_brand="{{ alpine_motd_cpu_brand }}" -cpu_speed="{{ alpine_motd_cpu_speed }}" -cpu_cores="{{ alpine_motd_cpu_cores }}" -cpu_temp="{{ alpine_motd_cpu_temp }}" -gpu_brand="{{ alpine_motd_gpu_brand }}" -gpu_type="{{ alpine_motd_gpu_type }}" -refresh_rate="{{ alpine_motd_refresh_rate }}" -gtk_shorthand="{{ alpine_motd_gtk_shorthand }}" -gtk2="{{ alpine_motd_gtk2 }}" -gtk3="{{ alpine_motd_gtk3 }}" -public_ip_host="{{ alpine_motd_public_ip_host }}" -public_ip_timeout="{{ alpine_motd_public_ip_timeout }}" -local_ip_interface={{ alpine_motd_local_ip_interface }} -de_version="{{ alpine_motd_de_version }}" -disk_show={{ alpine_motd_disk_show }} -disk_subtitle="{{ alpine_motd_disk_subtitle }}" -disk_percent="{{ alpine_motd_disk_percent }}" -music_player="{{ alpine_motd_music_player }}" -song_format="{{ alpine_motd_song_format }}" -song_shorthand="{{ alpine_motd_song_shorthand }}" -mpc_args={{ alpine_motd_mpc_args }} -colors={{ alpine_motd_colors }} -bold="{{ alpine_motd_bold }}" -underline_enabled="{{ alpine_motd_underline_enabled }}" -underline_char="{{ alpine_motd_underline_char }}" -separator="{{ alpine_motd_separator }}" -block_range={{ alpine_motd_block_range }} -color_blocks="{{ alpine_motd_color_blocks }}" -block_width={{ alpine_motd_block_width }} -block_height={{ alpine_motd_block_height }} -col_offset="{{ alpine_motd_col_offset }}" -bar_char_elapsed="{{ alpine_motd_bar_char_elapsed }}" -bar_char_total="{{ alpine_motd_bar_char_total }}" -bar_border="{{ alpine_motd_bar_border }}" -bar_length={{ alpine_motd_bar_length }} -bar_color_elapsed="{{ alpine_motd_bar_color_elapsed }}" -bar_color_total="{{ alpine_motd_bar_color_total }}" -memory_display="{{ alpine_motd_memory_display }}" -battery_display="{{ alpine_motd_battery_display }}" -disk_display="{{ alpine_motd_disk_display }}" -image_backend="{{ alpine_motd_image_backend }}" -image_source="{{ alpine_motd_image_source }}" -ascii_distro="{{ alpine_motd_ascii_distro }}" -ascii_colors={{ alpine_motd_ascii_colors }} -ascii_bold="{{ alpine_motd_ascii_bold }}" -image_loop="{{ alpine_motd_image_loop }}" -thumbnail_dir="{{ alpine_motd_thumbnail_dir }}" -crop_mode="{{ alpine_motd_crop_mode }}" -image_size="{{ alpine_motd_image_size }}" -catimg_size="{{ alpine_motd_catimg_size }}" -gap={{ alpine_motd_gap }} -yoffset={{ alpine_motd_yoffset }} -xoffset={{ alpine_motd_xoffset }} -background_color="{{ alpine_motd_background_color }}" -stdout="{{ alpine_motd_stdout }}" \ No newline at end of file +title_fqdn="{{ update_motd_title_fqdn }}" +kernel_shorthand="{{ update_motd_kernel_shorthand }}" +distro_shorthand="{{ update_motd_distro_shorthand }}" +os_arch="{{ update_motd_os_arch }}" +uptime_shorthand="{{ update_motd_uptime_shorthand }}" +memory_percent="{{ update_motd_memory_percent }}" +memory_unit="{{ update_motd_memory_unit }}" +package_managers="{{ update_motd_package_managers }}" +shell_path="{{ update_motd_shell_path }}" +shell_version="{{ update_motd_shell_version }}" +speed_type="{{ update_motd_speed_type }}" +speed_shorthand="{{ update_motd_speed_shorthand }}" +cpu_brand="{{ update_motd_cpu_brand }}" +cpu_speed="{{ update_motd_cpu_speed }}" +cpu_cores="{{ update_motd_cpu_cores }}" +cpu_temp="{{ update_motd_cpu_temp }}" +gpu_brand="{{ update_motd_gpu_brand }}" +gpu_type="{{ update_motd_gpu_type }}" +refresh_rate="{{ update_motd_refresh_rate }}" +gtk_shorthand="{{ update_motd_gtk_shorthand }}" +gtk2="{{ update_motd_gtk2 }}" +gtk3="{{ update_motd_gtk3 }}" +public_ip_host="{{ update_motd_public_ip_host }}" +public_ip_timeout="{{ update_motd_public_ip_timeout }}" +local_ip_interface={{ update_motd_local_ip_interface }} +de_version="{{ update_motd_de_version }}" +disk_show={{ update_motd_disk_show }} +disk_subtitle="{{ update_motd_disk_subtitle }}" +disk_percent="{{ update_motd_disk_percent }}" +music_player="{{ update_motd_music_player }}" +song_format="{{ update_motd_song_format }}" +song_shorthand="{{ update_motd_song_shorthand }}" +mpc_args={{ update_motd_mpc_args }} +colors={{ update_motd_colors }} +bold="{{ update_motd_bold }}" +underline_enabled="{{ update_motd_underline_enabled }}" +underline_char="{{ update_motd_underline_char }}" +separator="{{ update_motd_separator }}" +block_range={{ update_motd_block_range }} +color_blocks="{{ update_motd_color_blocks }}" +block_width={{ update_motd_block_width }} +block_height={{ update_motd_block_height }} +col_offset="{{ update_motd_col_offset }}" +bar_char_elapsed="{{ update_motd_bar_char_elapsed }}" +bar_char_total="{{ update_motd_bar_char_total }}" +bar_border="{{ update_motd_bar_border }}" +bar_length={{ update_motd_bar_length }} +bar_color_elapsed="{{ update_motd_bar_color_elapsed }}" +bar_color_total="{{ update_motd_bar_color_total }}" +memory_display="{{ update_motd_memory_display }}" +battery_display="{{ update_motd_battery_display }}" +disk_display="{{ update_motd_disk_display }}" +image_backend="{{ update_motd_image_backend }}" +image_source="{{ update_motd_image_source }}" +ascii_distro="{{ update_motd_ascii_distro }}" +ascii_colors={{ update_motd_ascii_colors }} +ascii_bold="{{ update_motd_ascii_bold }}" +image_loop="{{ update_motd_image_loop }}" +thumbnail_dir="{{ update_motd_thumbnail_dir }}" +crop_mode="{{ update_motd_crop_mode }}" +image_size="{{ update_motd_image_size }}" +catimg_size="{{ update_motd_catimg_size }}" +gap={{ update_motd_gap }} +yoffset={{ update_motd_yoffset }} +xoffset={{ update_motd_xoffset }} +background_color="{{ update_motd_background_color }}" +stdout="{{ update_motd_stdout }}" \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index f73d6b4..443489b 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,4 @@ --- # vars file for update_motd -update_motd_path: "/etc/update-motd.d" \ No newline at end of file +update_motd_path: "/etc/update-motd.d" +update_motd_config_path: "/etc/profile.d"