- Generate SSH key pair:
ssh-keygen -t ed25519 -C "ansible"- Enter path:
/home/${USER}/.ssh/ansible - You'll see
ansible(Private Key) andansible.pub(Public Key)
- Enter path:
redhat/ubi9 latest a169546264dd 12 days ago 210MB
- This is the free version of RHEL which doesn't require authentication to download
- Go to project directory
- Build the image:
docker build -t rhel-server:v1.0 . - Run 3 containers mapping port 22 to different host ports:
docker run -d -p 2222:22 --name rhel-server-1 rhel-server:v1.0 docker run -d -p 2223:22 --name rhel-server-2 rhel-server:v1.0 docker run -d -p 2224:22 --name rhel-server-3 rhel-server:v1.0
- Check running containers:
docker ps
- Use your private key to connect to each server:
ssh -i ~/.ssh/ansible root@172.17.0.1 -p 2222 ssh -i ~/.ssh/ansible root@172.17.0.1 -p 2223 ssh -i ~/.ssh/ansible root@172.17.0.1 -p 2224
If Ansible is not installed, install it with:
sudo apt install ansible # For UbuntuThe inventory file contains server_name, IP address, and port.
Run this command to ping the servers:
ansible all -i inventory -m ping -u root --private-key ~/.ssh/ansible-m= module (the ping module is run)- You'll receive a pong from the Python interpreter, meaning that it works
Set up the config file ansible.cfg, then just run:
ansible all -m pingansible all --list-hosts
ansible all -m gather_facts # This gives us information about the hosts which we can use to write scripts like ansible_distribution
ansible all -m gather_facts --limit hostname # This is for checking values and cross-verifying with playbook to debug issues ansible all -m dnf -a "update_cache=true"
ansible all -m dnf -a "update_cache=true" --become --ask-become-pass
# Install Package
ansible all -m dnf -a "name=docker"
ansible all -m shell -a "dnf search podman"In the server you can go to this path:
/var/log/
dnf.log contains history of commands run. You can tail it and see the log entries.
ansible-playbook install_apache.yml
ansible-playbook book.yml --become --ask-become-passIn the playbook you write scripts in YAML format:
- hosts: all
become: true
tasks:
- name: Update Repository Index
dnf:
update_cache: yes
- name: Install Package
dnf:
name: httpd
state: present
- name: Check Installation
shell: command_here
register: result
- name: Display Result
debug:
msg: "{{ result.stdout }}"Use different state values:
present- Install package (default behavior)absent- Remove packagelatest- Install/update to latest version
Playbooks may fail for Ubuntu as they're written for dnf and Ubuntu needs apt.
Use conditional statements based on distribution:
- name: Install Apache on RedHat
dnf:
name: httpd
state: present
when: ansible_distribution == "RedHat"
- name: Install Apache on Ubuntu
apt:
name: apache2
state: present
when: ansible_distribution == "Ubuntu"Use the package module for cross-platform package management:
# Works on both RedHat and Ubuntu automatically
- name: Install Git (universal)
package:
name: git
state: present
- name: Install Python (universal)
package:
name: python3
state: present
# For packages with different names per distro
- name: Install Apache (universal)
package:
name: "{{ 'httpd' if ansible_distribution == 'RedHat' else 'apache2' }}"
state: present
# Multiple distributions with same package manager
- name: Install Nginx on RHEL-based systems
dnf:
name: nginx
state: present
when: ansible_distribution in ["RedHat", "CentOS", "Rocky", "AlmaLinux"]ansible all -m gather_facts --limit rhel1 | grep distributionThis command gets the specific distribution from the host.