Chapter 04 · Installing Ansible
Installing Ansible
Installing on the control node
On Debian/Ubuntu (examples):
sudo apt update
sudo apt install -y ansible openssh-client git
Check:
ansible --version
SSH configuration
- Generate a key:
ssh-keygen -t ed25519 - Copy it to the nodes:
ssh-copy-id patrick@node1 - Test:
ssh patrick@node1
Common pitfall: DNS/hosts, ports, firewall, username, and “I have forgotten which VM I am on”.
Running basic commands
# Ad hoc: uptime on all hosts
ansible -i inventory.ini all -a "uptime"
# With sudo
ansible -i inventory.ini web -b -a "systemctl status nginx --no-pager"
Managing modules (files, users, packages, services…)
Files
- ansible.builtin.copy
- ansible.builtin.template
- ansible.builtin.file
System
- ansible.builtin.user
- ansible.builtin.apt / yum / dnf
- ansible.builtin.service
Practical exercise: system baseline
Goal: apply a common baseline to node1 et node2.
- Create a playbook
baseline.ymlwhich: - updates the APT cache
- installs
vim,gkrellm,htop - creates a user
ops(Bash shell) - deploys a file
/etc/motd(welcome message) - Run with
--check, then without it. - Check the managed nodes.
NOTE: To generate a password hash for the user, use the openssl command, which is already installed on the deb-ctrl VM.
openssl passwd -6
The command will prompt you to enter a password twice. You can then copy and paste the generated hash into the password parameter of the ansible.builtin.user module..
Minimal example
- name: System baseline
hosts: web
become: true
vars:
baseline_packages:
- vim
- gkrellm
- htop
ops_user: ops
motd_message: |
Welcome to {{ inventory_hostname }}️
Provisioned by Ansible.
(If everything breaks, the MOTD is not to blame.)
tasks:
- name: Update the APT cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
- name: Install the base packages
ansible.builtin.apt:
name: "{{ baseline_packages }}"
state: present
- name: Create the ops user
ansible.builtin.user:
name: "{{ ops_user }}"
shell: /bin/bash
state: present
create_home: true
- name: Deploy /etc/motd
ansible.builtin.copy:
dest: /etc/motd
content: "{{ motd_message }}\n"
owner: root
group: root
mode: "0644"
QCM
1) The “control node” is:
- A. The machine from which Ansible is run
- B. Always a cloud server
- C. A Kubernetes node
2) On Linux, the connection is most commonly made via:
- A. SSH
- B. SMTP
- C. Telnet (in 2026, good luck with that)
3) Which module is suitable for managing a service?
- A. ansible.builtin.service
- B. ansible.builtin.git
- C. ansible.builtin.debug
Answer key
1) A · 2) A · 3) A