Ansible playbooks and configurations for automated system management.
Ansible is an automation tool for:
- Configuration Management: Keep systems configured consistently
- Application Deployment: Deploy applications and updates
- Task Automation: Automate repetitive tasks
- Orchestration: Coordinate complex multi-system tasks
Benefits:
- Agentless (uses SSH)
- Simple YAML syntax
- Idempotent operations
- Large module library
Run Ansible in a Docker container
- No need to install Ansible on host
- Consistent environment
- Easy version management
Collection of ready-to-use Ansible playbooks:
Update and upgrade all packages on Debian/Ubuntu systems
Automatically update Git repositories
Install standard software on new VMs
- Common utilities
- Development tools
- System packages
apt update
apt install ansible -y
# Verify installation
ansible --versioncd /path/to/Proxmox-stuff/ansible
docker-compose up -d# Create inventory of your servers
nano inventory.ini[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
[proxmox]
pve1.local ansible_host=192.168.1.100
pve2.local ansible_host=192.168.1.101
[all:vars]
ansible_user=root
ansible_ssh_private_key_file=~/.ssh/id_rsaansible all -i inventory.ini -m pingUpdate all systems:
cd /path/to/Proxmox-stuff/ansible
# Run update playbook
ansible-playbook -i inventory.ini playbooks/apt-update-upgrade.yml
# Limit to specific hosts
ansible-playbook -i inventory.ini playbooks/apt-update-upgrade.yml --limit webservers
# Dry run (check mode)
ansible-playbook -i inventory.ini playbooks/apt-update-upgrade.yml --checkInstall programs on new VMs:
ansible-playbook -i inventory.ini playbooks/install-new-vm-programsKeep repositories up to date:
ansible-playbook -i inventory.ini playbooks/github-repo-update---
# playbook-name.yml
- name: Description of what this playbook does
hosts: all # or specific group
become: yes # Run as sudo
tasks:
- name: Task description
module_name:
parameter: value---
- name: Install Docker on Ubuntu
hosts: all
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Install Docker
apt:
name: docker-ce
state: present
- name: Ensure Docker is started
service:
name: docker
state: started
enabled: yes---
- name: Harden SSH Configuration
hosts: all
become: yes
tasks:
- name: Disable root login
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
state: present
- name: Disable password authentication
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PasswordAuthentication'
line: 'PasswordAuthentication no'
state: present
- name: Restart SSH service
service:
name: ssh
state: restarted# APT (Debian/Ubuntu)
- name: Install package
apt:
name: nginx
state: present
# YUM/DNF (RedHat/CentOS)
- name: Install package
yum:
name: nginx
state: present# Copy file
- name: Copy configuration
copy:
src: /local/path/config.conf
dest: /remote/path/config.conf
mode: '0644'
# Create directory
- name: Create directory
file:
path: /var/www/html
state: directory
mode: '0755'
# Download file
- name: Download file
get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz- name: Ensure service is running
service:
name: nginx
state: started
enabled: yes
- name: Restart service
service:
name: apache2
state: restarted- name: Create user
user:
name: deploy
groups: sudo
shell: /bin/bash
state: present
- name: Add SSH key
authorized_key:
user: deploy
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"- name: Run command
command: /usr/bin/some-command
- name: Run shell command
shell: echo "Hello" > /tmp/file.txt
- name: Run script
script: /path/to/local/script.sh- Use version control for playbooks
- Test in staging before production
- Use --check mode for dry runs
- Keep playbooks idempotent (can run multiple times)
- Use roles for complex configurations
- Document playbooks with comments
- Use variables for flexibility
- Use vault for sensitive data
---
- name: Use variables
hosts: all
vars:
app_name: myapp
app_port: 8080
tasks:
- name: Install {{ app_name }}
apt:
name: "{{ app_name }}"# Create encrypted file
ansible-vault create secrets.yml
# Edit encrypted file
ansible-vault edit secrets.yml
# Run playbook with vault
ansible-playbook playbook.yml --ask-vault-passOrganize playbooks into reusable roles:
roles/
webserver/
tasks/
main.yml
handlers/
main.yml
templates/
files/
vars/
main.yml
# Use template
- name: Deploy config from template
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conftasks:
- name: Copy nginx config
copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
notify: restart nginx
handlers:
- name: restart nginx
service:
name: nginx
state: restartedtasks:
- name: Install packages
apt:
name: nginx
tags: install
# Run only tagged tasks
ansible-playbook playbook.yml --tags install# Run playbook daily at 2 AM
crontab -e
0 2 * * * ansible-playbook -i /path/to/inventory.ini /path/to/playbook.yml# Test connection
ansible all -i inventory.ini -m ping -vvv
# Check SSH config
ansible all -i inventory.ini -m setup --limit hostname
# Use specific SSH key
ansible-playbook playbook.yml --private-key ~/.ssh/custom_key# "Host unreachable"
# Check: SSH access, firewall, IP address
# "Permission denied"
# Check: SSH keys, ansible_user, become settings
# "Module not found"
# Update Ansible: apt install --upgrade ansible# Verbose output
ansible-playbook playbook.yml -v
ansible-playbook playbook.yml -vv
ansible-playbook playbook.yml -vvv # Very verbose
# Step-by-step
ansible-playbook playbook.yml --step
# Start at specific task
ansible-playbook playbook.yml --start-at-task="Task name"# List all hosts
ansible all -i inventory.ini --list-hosts
# Gather facts
ansible all -i inventory.ini -m setup
# Run ad-hoc command
ansible all -i inventory.ini -a "uptime"
# Check playbook syntax
ansible-playbook playbook.yml --syntax-check
# List tasks
ansible-playbook playbook.yml --list-tasks
# List tags
ansible-playbook playbook.yml --list-tags- Ansible Documentation
- Ansible Galaxy - Pre-made roles
- Ansible Examples