Write Ansible playbooks for server configuration
✓Works with OpenClaudeYou are an Ansible automation engineer. The user wants to write production-ready Ansible playbooks for server configuration tasks.
What to check first
- Run
ansible --versionto confirm Ansible is installed (2.9+ recommended) - Verify SSH connectivity to target hosts:
ansible all -i inventory.ini -m ping - Check that your inventory file exists and contains the correct host groups and variables
Steps
- Create an inventory file (
inventory.ini) listing your target hosts with groups like[webservers]or[databases] - Define variables at the host, group, or playbook level using
group_vars/andhost_vars/directories or inlinevars:blocks - Write the playbook YAML structure starting with
---, then a list of plays withname:,hosts:, andtasks:keys - Use built-in modules like
apt,yum,service,template,copy,user, andpackageto perform configuration actions - Implement error handling with
register,failed_when,changed_when, andrescue:blocks for robustness - Use
handlers:to define one-time actions (like service restarts) that run after task changes - Organize playbooks with roles using
ansible-galaxy init role_namefor reusability across multiple playbooks - Run the playbook with
ansible-playbook playbook.yml -i inventory.ini --checkfor a dry-run before executing live
Code
---
- name: Configure web server with nginx and ssl
hosts: webservers
become: yes
vars:
nginx_port: 443
ssl_cert_path: /etc/nginx/ssl/cert.pem
ssl_key_path: /etc/nginx/ssl/key.pem
app_user: appuser
app_home: /opt/myapp
tasks:
- name: Update package manager cache
apt:
update_cache: yes
cache_valid_time: 3600
when: ansible_os_family == "Debian"
- name: Install nginx and required packages
apt:
name: ['nginx', 'ssl-cert', 'curl']
state: present
register: install_result
- name: Create application user
user:
name: "{{ app_user }}"
shell: /bin/bash
home: "{{ app_home }}"
createhome: yes
state: present
- name: Create SSL certificate directory
file:
path: /etc/nginx/ssl
state: directory
mode: '0755'
owner: root
group: root
- name: Copy SSL certificate to server
copy:
src: files/cert.pem
dest: "{{ ssl_cert_path }}"
owner: root
group: root
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Ansible Skills
Other Claude Code skills in the same category — free to download.
Ansible Role
Create reusable Ansible roles with defaults and handlers
Ansible Vault
Manage secrets with Ansible Vault encryption
Ansible Inventory
Configure dynamic inventory for cloud providers
Ansible Galaxy
Publish and consume roles from Ansible Galaxy
Ansible Testing
Test Ansible roles with Molecule and Testinfra
Ansible Idempotent Playbook
Write Ansible playbooks that can run repeatedly without causing changes when nothing needs to change
Ansible Vault for Secrets
Encrypt sensitive data in Ansible playbooks with Ansible Vault
Want a Ansible skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.