Ok, we are now finally at a stage where we can bring together all the ideas into one page.
Add a new host into the ansible host file /etc/ansible/hosts
[centos10]
newvm01 ansible_host=newvm01.ash.local ansible_user=awk ansible_become=yesCreate the automation user awk
sudo useradd -m awk
sudo passwd awk
echo "awk ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/awkUse ssh-copyip to copy the public key
ssh-copy-id awk@newvm01.ash.localInstall ansible on the new vm (managed node)
sudo dnf install -y ansible-core git
The packages to install are in the roles/baseline/vars/main.yml but we can go one step further and create OS‑specific vars files for packages we wish to install and this is what is {{ ansible_distribution }} which picks up the operating system..
roles/baseline/vars/CentOS.yml
roles/baseline/vars/Ubuntu.yml
roles/baseline/vars/Rocky.yml
roles/baseline/vars/Debian.yml
Define the playbook under
/etc/ansible/playbooks/baseline-init.yml ---
- hosts: all
become: true
roles:
- baselineThis file contains TASKS/VARIABLES that has an existing motd file so we will replace that will jinja.
roles/baseline/templates/motd.j2Welcome to {{ inventory_hostname }}.
Managed by Ansible Baseline Role.
Environment: {{ motd_environment }}
Owner: {{ motd_owner }}
OS: {{ ansible_distribution }} {{ ansible_distribution_version }}
Kernel: {{ ansible_kernel }}
IP: {{ ansible_default_ipv4.address }}Here is where the action happen
roles/baseline/tasks/main.ymlThe main thing is the include_vars: “{{ ansible_distribution }}.yml” which loads the OS correctly
- name: Load OS-specific variables
include_vars: "{{ ansible_distribution }}.yml"
- name: Baseline role is running
debug:
msg: "Baseline tasks executed"
- name: Set system timezone
timezone:
name: "Europe/London"
- name: Update package cache
package:
update_cache: yes
- name: Upgrade all packages to latest
package:
name: "*"
state: latest
- name: Install baseline packages
package:
name: "{{ packages }}"
state: present
- name: Remove unwanted packages
package:
name: "{{ unwanted_packages }}"
state: absent
- name: Deploy MOTD template
template:
src: motd.j2
dest: /etc/motd
owner: root
group: root
mode: '0644'
- name: Ensure SSH service is running
service:
name: "{{ ssh_service }}"
state: started
enabled: trueNow create the playbook under
playbooks/baseline-init.yml---
- hosts: all
become: true
roles:
- baselineDo a dry run just for that group using -l
ansible-playbook playbooks/baseline.yml -l centos10or, do a dry run just for that host using -l
ansible-playbook playbooks/baseline.yml -l newvm01
Run the playbook

Login and check


