# Roll out a Linux baseline with Ansible

Source: https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance
Language: en
Published: 2026-10-11
Updated: 2026-11-25

Packages, services, SSH drop-ins, handlers, check mode and technical acceptance as a reusable Linux baseline role.

## Why a Linux baseline matters

A Linux baseline is not full hardening and does not replace CIS, OpenSCAP or a security profile. It is the repeatable base configuration every managed server should receive: baseline packages, services, time synchronization, SSH basics, login notices and a documented acceptance run.

This article builds on the previous Ansible structure. Inventory, group\_vars, host\_vars and Vault stay the same; the new piece is a concrete role that can be adapted for SLES, Fedora, Rocky, Alma, Debian or Ubuntu.

## Target design of the role

- Cross-distribution tasks stay generic.
- Package lists, service names and exceptions live in variables.
- Configuration files are validated before they become active.
- Handlers restart or reload services only when something changed.
- Check mode and diff mode show the planned changes before applying them.

## Role layout

```text
roles/linux_baseline/
  defaults/main.yaml
  tasks/main.yaml
  handlers/main.yaml
  templates/issue.j2
  templates/sshd_config.d/forgeone-baseline.conf.j2
```

## Defaults: start deliberately small

Defaults should be safe but not overloaded. Everything that differs by distribution or customer can later be overridden in group\_vars or host\_vars.

```yaml
# roles/linux_baseline/defaults/main.yaml
linux_baseline_packages:
  - curl
  - vim
  - chrony

linux_baseline_services:
  - chronyd

linux_baseline_manage_issue: true
linux_baseline_manage_sshd_dropin: true
linux_baseline_ssh_password_authentication: false
linux_baseline_ssh_permit_root_login: false
```

## Map distributions explicitly

A common mistake is hardcoding package and service names directly in tasks. Variables per platform family keep the role readable and reusable across environments.

```yaml
# inventories/lab/group_vars/suse.yaml
linux_baseline_services:
  - chronyd

# inventories/lab/group_vars/debian.yaml
linux_baseline_services:
  - chrony
```

## Tasks: packages, services and templates

```yaml
# roles/linux_baseline/tasks/main.yaml
- name: Install baseline packages
  ansible.builtin.package:
    name: "{{ linux_baseline_packages }}"
    state: present

- name: Ensure baseline services are enabled and started
  ansible.builtin.service:
    name: "{{ item }}"
    enabled: true
    state: started
  loop: "{{ linux_baseline_services }}"

- name: Render login banner
  ansible.builtin.template:
    src: issue.j2
    dest: /etc/issue
    owner: root
    group: root
    mode: '0644'
  when: linux_baseline_manage_issue

- name: Install sshd baseline drop-in
  ansible.builtin.template:
    src: sshd_config.d/forgeone-baseline.conf.j2
    dest: /etc/ssh/sshd_config.d/99-forgeone-baseline.conf
    owner: root
    group: root
    mode: '0644'
    validate: /usr/sbin/sshd -t -f %s
  notify: Reload sshd
  when: linux_baseline_manage_sshd_dropin
```

## Template: prefer an SSH drop-in

Where possible, the role should avoid replacing the full sshd\_config. A drop-in reduces the risk of overwriting local policy. Validation with sshd -t remains important before a handler reloads the service.

```jinja2
# roles/linux_baseline/templates/sshd_config.d/forgeone-baseline.conf.j2
PasswordAuthentication {{ 'yes' if linux_baseline_ssh_password_authentication else 'no' }}
PermitRootLogin {{ 'yes' if linux_baseline_ssh_permit_root_login else 'no' }}
```

## Handlers: react only when something changed

```yaml
# roles/linux_baseline/handlers/main.yaml
- name: Reload sshd
  ansible.builtin.service:
    name: sshd
    state: reloaded
```

## Baseline playbook

```yaml
# playbooks/linux-baseline.yaml
- name: Apply Linux baseline
  hosts: linux
  become: true
  gather_facts: true
  roles:
    - role: linux_baseline
```

## Acceptance run

Before applying the baseline, run it dry first. Then run it for real and follow with another check run to reveal unexpected drift.

```bash
ansible-inventory -i inventories/lab/hosts.yaml --graph
ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml --syntax-check
ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml --check --diff
ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml
ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml --check --diff
```

## Acceptance table

```markdown
| Area | Check | Result |
| --- | --- | --- |
| Inventory | target group linux resolves correctly | PASS |
| Packages | baseline packages are installed or unchanged | PASS |
| Services | defined services are enabled and started | PASS |
| SSH | drop-in is validated and service reloads only on change | PASS |
| Check mode | planned state is visible before changes | PASS |
| Idempotence | second run shows no unexpected changes | PASS |
```

## Limits of the baseline

This role is an operational baseline. It does not replace full security hardening, vulnerability handling or compliance acceptance. CIS, OpenSCAP and customer-specific hardening should become separate roles, profiles and acceptance records.

## What comes next

The next useful step is controlled patch and reboot management with Ansible: prepare updates, limit target groups, detect reboot requirements, respect maintenance windows and verify services after reboot.

## Sources and further documentation

The examples use official Ansible modules and follow the documentation for package, service, template, handlers, check mode and diff mode.

## Hero image

[![Ansible logo.](https://forgeone.solutions/media/Ansible_Logo.webp)](https://forgeone.solutions/media/Ansible_Logo.webp)

## Author

Mathias Rumbold

## Category

- [Ansible & Automation](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?category=ansible-automation)
- [Enterprise Linux](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?category=enterprise-linux)

## Tags

- [Ansible](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?tags=ansible)
- [Ansible Roles](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?tags=ansible-roles)
- [Automation](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?tags=automation)
- [Linux](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?tags=linux)
- [Linux Management](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?tags=linux-management)
- [Enterprise Linux](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance?tags=enterprise-linux)

## Ansible automation step by step

This series deliberately stays generic: Ansible fundamentals, repository structure, roles, Vault, Linux baselines, updates, quality checks, CI and AWX. Domain topics such as IdM, FreeIPA, Proxmox or SUSE Multi-Linux Manager are covered in their own specialist series.

1. [Install Ansible and create your first playbooks](https://forgeone.solutions/en/tech/install-ansible-and-create-your-first-playbooks)
2. [Ansible inventories, roles and Vault in practice](https://forgeone.solutions/en/tech/ansible-inventories-roles-vault-manage-linux-systems-reproducibly)
3. [Roll out a Linux baseline with Ansible (current part)](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance)
4. [Orchestrate updates, reboots and maintenance windows with Ansible](https://forgeone.solutions/en/tech/ansible-orchestrate-updates-reboots-maintenance-windows)
5. [Test, lint and validate Ansible roles in CI](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci)
6. [Version, release and change Ansible repositories traceably](https://forgeone.solutions/en/tech/ansible-version-release-change-repositories-traceably)
7. [Position AWX for repeatable execution, credentials and job templates](https://forgeone.solutions/en/tech/awx-ansible-jobs-credentials-job-templates)

## Further content

- [Ansible](https://forgeone.solutions/en/products/ansible)
