Ansible Playbook: Getting Started Guide
Walks through creating, syntax-checking, and running a basic Ansible playbook on localhost, then extends it with a multi-task example.
What this file does
Walks through creating, syntax-checking, and running a basic Ansible playbook on localhost, then extends it with a multi-task example.
When to use it
- Learning the minimal steps to create and run a playbook
- Testing Ansible connectivity to localhost
- Adding a debug task to verify playbook execution
- Verifying playbook syntax before running on remote hosts
Assumes this stack
Ansible Playbook: Getting Started Guide
This guide will walk you through creating a basic Ansible playbook to help you get started with automation. We'll also cover multi-task playbooks to enhance your understanding.
Step-by-Step Instructions
1. Check Your Current Directory
To ensure you're in the correct working directory, run:
pwd
- If you are already in
/etc/ansible/, proceed to Step 3. - Otherwise, follow Step 2 to navigate to the appropriate directory.
2. Change Directory to Ansible's Default Location
cd /etc/ansible/
This is where Ansible configurations and playbooks are typically stored.
3. Create a Directory for Playbooks
mkdir Playbooks
- The
Playbooksdirectory will contain all your playbook files.
4. List Files and Directories
ls
This ensures your Playbooks folder was successfully created.
5. Navigate to the Playbooks Directory
cd Playbooks
6. Confirm the Path
pwd
You should now be in /etc/ansible/Playbooks. This is where you'll create your playbook file. Note: You can store playbooks in other locations, but ensure that location is accessible.
7. Create Your First Playbook File
You can use any text editor, such as vi or VSCode. Here, we'll use vi.
sudo vi tushar1_playbook.yml
Alternatively, if you prefer to write your playbook in VSCode, you can create the file and then paste it into your editor.
Basic Playbook:
---
- name: tusharplaybook1
hosts: localhost
tasks:
- name: Test Connectivity
ping:
- The playbook tests connectivity to the localhost.
8. Verify the File Creation
ls
ls -ltr
This confirms that your playbook file has been created.
9. Syntax Check the Playbook
Before running the playbook, ensure it is syntactically correct by running:
ansible-playbook --syntax-check tushar1_playbook.yml
- This helps catch any YAML formatting issues.
10. Run the Playbook
Now, execute the playbook:
ansible-playbook tushar1_playbook.yml
- If your playbook is stored in a different location, specify the absolute path.
Expected Result:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [tusharplaybook1] *****************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Test Connectivity] ***************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Multi-task Playbook
You can add multiple tasks to a playbook to perform actions sequentially. Let's add an extra task to print a message.
Steps to Add Another Task
Updated Playbook:
---
- name: tusharplaybook1
hosts: localhost
tasks:
- name: Test Connectivity
ping:
- name: Print Output
debug: msg="Ready to go"
1. Verify the Playbook
Check your updated playbook with:
ansible-playbook --syntax-check tushar1_playbook.yml
2. Run the Multi-task Playbook
ansible-playbook tushar1_playbook.yml
Expected Result:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [tusharplaybook1] *****************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Test Connectivity] ***************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Print Output] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Ready to go"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The playbook will first test connectivity to the localhost, followed by printing the output message "Ready to go".
Conclusion
This guide provided you with a basic Ansible playbook setup, syntax checking, and running a playbook. We also explored multi-task playbooks to enhance functionality.
What's inside
Two playbook examples (basic and multi-task), 10 step-by-step instructions, 3 command outputs, 2 YAML code blocks
Change this for your project
- Replace
tushar1_playbook.ymlwith your own playbook filename - Replace
tusharplaybook1with your own playbook name - Replace
"Ready to go"with your own debug message
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Worth borrowing
- Use
--syntax-checkbefore running any playbook to catch YAML errors early - Start with a ping task to verify connectivity before adding complex tasks
Related Documents
Media Handling Playbook - Zyeuté v3
Documents upload, storage, optimization, delivery, and edge-case handling for images, videos, and audio in a social app.
Visual Truth Engine: Product-Market Fit & Go-to-Market Strategy
Outlines a product-market fit strategy and go-to-market plan for a B2B SaaS tool that automates proof asset creation for video creators.
Trader ROI Playbook (Codex + CI)
Defines weekly ROI targets, task prioritization lanes, guardrails, and a scorecard template for measuring engineering output per hour.
Devin Triage Playbook
Defines a 7-phase automated triage playbook for Devin to analyze alerts, find root causes, implement fixes, and notify teams.