Write and Run Your First Playbook
Beginner10 minTrending
Create a basic Ansible playbook to install packages, configure services, and deploy changes across multiple hosts.
Prerequisites
- -Ansible installed
- -SSH access to target hosts
- -Inventory file configured
Steps
1
Create a playbook file
Creates a YAML playbook that installs and starts nginx on all hosts in the webservers group.
$ cat > site.yml << 'EOF'
---
- name: Configure web servers
hosts: webservers
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: present
update_cache: true
- name: Start nginx service
service:
name: nginx
state: started
enabled: true
EOF
Always use 'become: true' when tasks require root privileges.
2
Run the playbook in check mode
Performs a dry run showing what changes would be made without actually applying them.
$ ansible-playbook site.yml --check --diff
Always run --check first on production systems to preview changes safely.
3
Run the playbook
Executes the playbook against all hosts defined in the webservers group.
$ ansible-playbook site.yml
4
Run with verbose output
Shows detailed task output. Use -vv or -vvv for even more debugging information.
$ ansible-playbook site.yml -v
5
Limit to specific hosts
Runs the playbook only against a single host for targeted deployment.
$ ansible-playbook site.yml --limit web01.example.com
Full Script
FAQ
Discussion
Loading comments...