Skip to content

Latest commit

 

History

History
289 lines (206 loc) · 12.4 KB

AGENTS.md

File metadata and controls

289 lines (206 loc) · 12.4 KB

AGENTS.md - Developer instructions for the ssh Linux System Role

For end users

If you just want to use this role in a playbook, not change its code, this file isn't for you. See README.md for role variables, usage examples, and supported platforms. See GitHub Issues if you want to report a bug or request a feature without writing code yourself.

For developers

Follow these conventions when you change this role. See contribute.html for the full contributor guide.

Commit messages, PR titles, and PR descriptions

PR title MUST follow Conventional Commits format:

  • Format: <required type><optional (scope)><optional !>: <required description>
  • Valid types: see https://github.com/linux-system-roles/auto-maintenance/blob/main/pr_title_lint.py#L23
  • Examples:
    • feat: Add backup functionality
    • fix: Correct OSTree package installation
    • fix!: Remove deprecated variable (breaking change)
    • chore(formatting): Fix indentation in my_module.py
    • refactor(python)!: Use new python library thispy which has breaking api changes

PR description Enhancements/Features MUST follow the template in .github/pull_request_template.md:

  • Must contain "Enhancement:" or "Feature:" section describing what changed
  • Must contain "Reason:" section explaining why the change was needed
  • Must contain "Result:" section describing the outcome or impact
  • Can contain optional "Issue Tracker Tickets (Jira or BZ if any):" section
  • Must contain "Signed-off-by:" section with your name and email address - use git commit -s
  • Can contain optional "Assisted-by:" section with name of the AI coding assistant and models used

Example:

Feature: Introduce the ssh_secure_logging variable

Reason: Currently, all sensitive tasks use hard-coded no_log: true, which makes debugging difficult.

Result: Users can now set ssh_secure_logging: false for debugging while maintaining secure defaults.

Issue Tracker Tickets (Jira or BZ if any): RHEL-12345

Signed-off-by: John Doe john.doe@example.com

Assisted-by: Fish 4.3 using model Swim 6.2

For bug fixes, use this template instead:

  • Must contain "Cause:" section explaining the root cause of the bug
  • Must contain "Consequences:" section explaining the impact of the bug and how it appears to users
  • Must contain "Fix:" section explaining the fix for the bug and how it fixes the bug
  • Must contain "Result:" section describing the outcome or impact of the fix
  • Can contain optional "Issue Tracker Tickets (Jira or BZ if any):" section
  • Must contain "Signed-off-by:" section with your name and email address - use git commit -s
  • Can contain optional "Assisted-by:" section with name of the AI coding assistant and models used

Example:

Cause: The variable rolename_user_name was being checked for a `none` value but was not being checked for string length greater than 0 if a string.

Consequences: The role allowed empty user names to be configured which caused the daemon to report "User not found".

Fix: The variable rolename_user_name is now checked for string length if not `none`, and will report an error in that case if the length is 0.

Result: The role will not allow the user to provide an empty user name and will report an error if the rolename_user_name length is 0.

Signed-off-by: John Doe john.doe@example.com

Assisted-by: Fish 4.3 using model Swim 6.2

For other change types (docs, refactor, and similar), the PR or commit body does not need a fixed template, but it must still be descriptive.

AI agent attribution

If the code you add was substantially generated by AI, mark it:

  • Add an in-line comment next to the generated code, or at the top of the file after the SPDX line (if any):

    # Assisted by <name of agent> using model(s) <name and version of model>[, <name and version of another model>]
    
  • Add the same attribution in the git commit message and PR body:

    Assisted-by: <name of agent> using model(s) <name and version of model>[, <name and version of another model>]
    

Ansible tasks - tasks/**/*.yml

no_log patterns:

  • For sensitive data (credentials, secrets, passwords, keys):

    - name: Task with sensitive data
      ansible.builtin.command: sensitive command
      no_log: "{{ ssh_secure_logging }}"

    Ensure ssh_secure_logging: true is defined in defaults/main.yml.

  • For facts modules (package_facts, service_facts):

    - name: Gather package facts
      ansible.builtin.package_facts:
      no_log: "{{ ansible_verbosity < 3 }}"

    This hides verbose facts output unless you run with -vvv or higher.

  • NEVER use hardcoded no_log: true - always parametrize.

Package installation (OSTree compatibility):

  • ALWAYS use the ansible.builtin.package module (NEVER yum, dnf, or apt directly).
  • ALWAYS include the use: parameter for OSTree compatibility.
  • It does nothing on non-OSTree systems, so it is safe to always include it.

Typical package module usage:

- name: Ensure packages
  ansible.builtin.package:
    name: "{{ __ssh_packages }}"
    state: present
    use: "{{ (__ssh_is_ostree | d(false)) | ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

Explanation:

  • __ssh_is_ostree is true on OSTree/rpm-ostree systems.
  • The role sets this in tasks/set_vars.yml or similar.
  • DO NOT set it again.
  • | d(false) provides a safe default if the variable is undefined.
  • ternary() selects ansible.posix.rhel_rpm_ostree for OSTree, omit otherwise.
  • omit removes the parameter entirely on non-OSTree systems.

Using the shell: module:

  • ALWAYS use the cmd parameter to specify the script to run, never the free-form style.
  • ALMOST ALWAYS use executable: /bin/bash unless you must use another shell.
  • ALWAYS use set -e, set -u, and set -o pipefail (or combined as set -euo pipefail) as the first line of the script.
  • Ansible requires pipefail.
  • ALWAYS use changed_when:.
  • If you need to detect whether the script changed something, have it exit with a specific code or print a value to stdout/stderr, register: the result, then use changed_when: and failed_when: conditionals.

Third-party collections:

  • Avoid third-party collections (community.general, community.crypto, etc.).
  • Use ansible.builtin modules or the command module instead.

Referencing other system roles:

  • Use the FQCN: fedora.linux_system_roles.ssh.

Idempotency:

  • Tasks must be idempotent - safe to run multiple times without unintended changes.

  • Use proper state parameters (present/absent, started/stopped, etc.).

  • command/shell tasks should use creates/removes or changed_when to avoid false changes.

  • Example:

    - name: Initialize database
      ansible.builtin.command: /usr/bin/initialize-db
      args:
        creates: /var/lib/db/initialized.flag

Check mode support:

  • All tasks should support check mode (--check).
  • Use check_mode: false only when required (for example, fact gathering).
  • Test that the role works with --check --diff.

Test coverage requirement:

  • When you add new tasks to tasks/main.yml or create new task files, add matching coverage in tests/.
  • New functionality MUST include test files (tests/tests_*.yml) that exercise the new code paths.
  • Cover both success and failure or edge cases.
  • If a change adds new tasks without new or updated tests, add test coverage before merging.

Handlers - handlers/**/*.yml

  • Handlers with sensitive data must use no_log: "{{ ssh_secure_logging }}".

Test playbooks - tests/tests_*.yml

CRITICAL: Role invocation pattern:

  • NEVER use ansible.builtin.include_role directly.

  • NEVER use ansible.builtin.import_role directly.

  • NEVER use the roles: keyword.

  • ALMOST ALWAYS use the centrally managed wrapper:

    - name: Run role
      ansible.builtin.include_tasks: tasks/run_role_with_clear_facts.yml
      vars:
        ssh_<parameter>: <value>
  • The only exception is tests_invalid_inputs.yml, where you can use include_role directly to test invalid inputs.

  • It is not required, but it can help in some cases.

Test quality requirements:

  • Cover both success and failure scenarios.

  • Use the assert module to verify expected state after role execution.

  • Include cleanup tasks so tests are rerunnable.

  • Run tests in a block with an always section that runs cleanup.

  • Tag cleanup tasks with tests::cleanup so you can skip cleanup while debugging.

  • Tests must be idempotent - a second run must not fail, and must not change anything.

  • This is difficult to test.

  • Many roles track changes in each task, then use set_fact to set a variable you can assert on - for example, __kernel_settings_changed in the kernel_settings role tests.

  • Example verification:

    - name: Verify service is running
      ansible.builtin.assert:
        that:
          - ansible_facts.services['mssql-server.service'].state == 'running'
        fail_msg: "SQL Server service is not running"

Templates - templates/**/*.j2

Required headers, in this order:

  1. ansible_managed header: {{ ansible_managed | comment }}
  2. Role fingerprint: {{ "system_role:ssh" | comment(prefix="", postfix="") }}

Variable definitions - defaults/**/*.yml

  • All variables MUST be prefixed with ssh_.
  • All variables which have a definite default value must be declared in defaults/main.yml.
  • Ansible does not load other files from defaults/.
  • These are user-facing API variables.
  • For every new variable, document it in README.md (description and usage example) and in meta/argument_specs.yml.
  • If either is missing, add it.
  • If a variable should have no default (remain undefined unless the user sets it), do NOT add it to defaults/main.yml.
  • Declare and document it only in meta/argument_specs.yml (see below).
  • A placeholder or empty default defeats the purpose of leaving it undefined.
  • In role code, use is defined or | default() to guard against undefined variable access.

Variable definitions - vars/**/*.yml

  • Internal variables MUST be prefixed with __ssh_.
  • User-facing variables belong in defaults/main.yml, not here.

Role argument validation - meta/argument_specs.yml

argument_specs.yml is the authoritative documentation and type or shape validation for every role variable. Keep it in sync when you add, rename, or change a variable.

  • Document every user-facing variable here: type, whether it is required, its default (if any), choices, and a clear description.
  • Document nested suboptions (dicts or lists of dicts) the same way, recursively.
  • Variables that should stay undefined by default (no sensible default, and the role or module must see them as unset unless the user sets them) must NOT get a default in defaults/main.yml.
  • Declare them only in argument_specs.yml (without a default: key) so Ansible documents and type-checks them while they stay undefined at runtime.
  • Do not add a dummy or empty default in defaults/main.yml just to define the variable.
  • If a variable can be undefined, only check when: var is defined - for example, variables declared in meta/argument_specs.yml but not in defaults/main.yml.
  • argument_specs.yml validation is limited: it checks types and shapes, but it cannot express cross-field constraints (for example, "a requires b"), cannot fully validate polymorphic or raw-typed fields that accept more than one type, and runs before the role gathers its own facts.
  • When you need validation beyond that, add checks in tasks/assert_role_vars.yml (create the file if needed, and run it near the start of tasks/main.yml).
  • Use tasks/assert_role_vars.yml when:
    • A value depends on another variable (cross-field validation).
    • A field accepts multiple shapes or types that argument_specs.yml cannot tell apart (for example, a raw type that may be a string or a dict).
    • See the network role's assert_connection_vars.yml for an example.
    • The check needs gathered facts or other runtime state, not only the variable value.
    • Keep these checks minimal.
    • Prefer argument_specs.yml for everything it can express; use assert_role_vars.yml only for the rest.

Documentation - README.md

  • Document all new user-facing variables.
  • Include usage examples for new functionality.