Ansible Operations¶
Prerequisites¶
- Ansible installed
- SSH key access to target host(s)
- Inventory file configured
Quick Reference¶
| Action | Command |
|---|---|
| Check Syntax | ansible-playbook --syntax-check site.yml |
| Dry Run | ansible-playbook site.yml --check |
| Dry Run + Diff | ansible-playbook site.yml --check --diff |
| Run Playbook | ansible-playbook site.yml |
| Run with Diff | ansible-playbook site.yml --diff |
| Run Tags Only | ansible-playbook site.yml --tags "dns" |
| List Hosts | ansible-inventory --list |
| Ad-hoc Ping | ansible all -m ping |
| Init Role | ansible-galaxy init <name> |
| Encrypt File | ansible-vault encrypt <file> |
Procedure: Preview Changes (Dry Run)¶
When to use: Before applying changes to a live system, preview what Ansible would do without actually making any modifications.
Steps:
-
Run in check mode (dry run — no changes applied):
Tasks will reportansible-playbook site.yml --checkchangedorokbased on what they would do, but nothing is modified on the target. -
Combine with
--diffto see exactly what would change in files:Foransible-playbook site.yml --check --difftemplate,copy, andlineinfiletasks, this shows a unified diff of the file contents before and after. -
Use
--diffwithout--checkduring a real run to see what changed as it happens:ansible-playbook site.yml --diff -
Combine with tags to preview a specific role:
ansible-playbook site.yml --check --diff --tags dns
Limitations:
- Some modules cannot predict their result in check mode (e.g.,
command,shell). These will be skipped. - Tasks that depend on earlier tasks having actually run may behave differently in check mode.
Procedure: Run a Playbook¶
When to use: Applying configuration to managed hosts.
Steps:
-
Syntax check:
ansible-playbook --syntax-check site.yml -
Run the playbook:
ansible-playbook -i inventory.ini site.yml -
Run with sudo/become prompt (if password needed):
ansible-playbook -i inventory.ini site.yml -K
Troubleshooting:
- "Unreachable": Check SSH connectivity and key setup.
- "Permission denied": Ensure the user has sudo rights and you used
-b(become) in the playbook or command.
Procedure: Run with Tags¶
When to use: Running only specific parts of a playbook (e.g., only updating configuration files).
Steps:
-
List available tags:
ansible-playbook site.yml --list-tags -
Run specific tags:
ansible-playbook site.yml --tags "nginx,config" -
Skip tags:
ansible-playbook site.yml --skip-tags "packages"
Troubleshooting:
- "No tasks run": The tags might be misspelled or applied to tasks that didn't need to run (idempotency).
Procedure: Create a Role¶
When to use: Structuring complex configurations into reusable components.
Steps:
-
Initialize role structure:
ansible-galaxy init roles/myrole -
Structure:
tasks/main.yml: Main list of taskshandlers/main.yml: Service restartstemplates/: Jinja2 configuration templatesvars/main.yml: Role-specific variables
Troubleshooting:
- Role not found: Ensure
roles_pathis configured inansible.cfgor the role is in aroles/directory relative to the playbook.
Procedure: Use Variables¶
When to use: Making playbooks flexible (e.g., different ports for dev/prod).
Steps:
-
Define in
group_vars/all.yml:http_port: 80 -
Use in tasks:
- name: Start Apache service: name: "{{ apache_service }}" state: started -
Use in templates (
.j2):Listen {{ http_port }}
Troubleshooting:
- "Undefined variable": Check variable scope and precedence.
Procedure: Use Handlers¶
When to use: Restarting a service only when a configuration file changes.
Steps:
-
Define handler:
handlers: - name: restart apache service: name: httpd state: restarted -
Notify from task:
- name: Copy config template: src: httpd.conf.j2 dest: /etc/httpd/conf/httpd.conf notify: restart apache
Troubleshooting:
- Handler didn't run: The notify task must report "changed". If the file was already correct, the handler won't trigger.
Procedure: Push to GitLab¶
When to use: Saving your Ansible code to version control.
Steps:
-
Check status:
git status -
Add changes:
git add . -
Commit:
git commit -m "Update apache role configuration" -
Push:
git push origin main
Troubleshooting:
- "rejected": Pull changes from remote first (
git pull).
Related Documentation¶
- Technologies: Ansible
- Concepts: Configuration Management, Version Control