This lab sets up Ansible and applies a basic playbook to multiple hosts. By the end you will have the discipline for production Ansible use.
Tasks
Task 1: Install Ansible
sudo apt install ansible
ansible --version
Task 2: Build inventory
mkdir -p ~/ansible-lab
cd ~/ansible-lab
cat > inventory.ini <<EOF
[webservers]
localhost ansible_connection=local
[all:vars]
ansible_python_interpreter=/usr/bin/python3
EOF
Task 3: Test connectivity
ansible -i inventory.ini all -m ping
Task 4: Write a playbook
# site.yml
- name: Configure all hosts
hosts: all
become: yes
tasks:
- name: Ensure vim is installed
apt:
name: vim
state: present
update_cache: yes
- name: Ensure /etc/motd has the right content
copy:
content: |
Welcome to {{ ansible_hostname }}
Managed by Ansible.
dest: /etc/motd
owner: root
group: root
mode: '0644'
Task 5: Apply
ansible-playbook -i inventory.ini site.yml
Task 6: Verify idempotency
ansible-playbook -i inventory.ini site.yml
Output should show changed=0.
Task 7: Check mode
ansible-playbook -i inventory.ini site.yml --check
Task 8: Document
ANSIBLE LAB
===========
Inventory: ~/ansible-lab/inventory.ini
Playbook: ~/ansible-lab/site.yml
Hosts managed: localhost (1)
Tasks applied:
- vim installed
- /etc/motd created
Idempotency: verified (re-run = changed=0)