Current stable version: Ansible-core 2.20.2 (released January 29, 2026). Here are some useful Ansible snippets covering various tasks and functionalities.
---
- name: Sample Playbook
hosts: webservers
become: yes
tasks:
- name: Install Apache
package:
name: apache2 # Use 'httpd' for RHEL/CentOS
state: present
---
- name: Install package
hosts: all
vars:
package_name: git
tasks:
- name: Install Git
package:
name: "{{ package_name }}"
state: present
---
- name: Install multiple packages
hosts: all
vars:
packages:
- git
- curl
- vim
tasks:
- name: Install packages
package:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
---
- name: Create users
hosts: all
vars:
users:
alice: "Alice Admin"
bob: "Bob Operator"
tasks:
- name: Create users
user:
name: "{{ item.key }}"
comment: "{{ item.value }}"
state: present
loop: "{{ users | dict2items }}"
---
- name: Conditionally install packages
hosts: all
tasks:
- name: Install Git if not installed
package:
name: git
state: present
when: ansible_distribution == 'Ubuntu'
- name: Install Git on RHEL-based systems
package:
name: git
state: present
when: ansible_os_family == 'RedHat'
---
- name: Handle service restart
hosts: all
tasks:
- name: Install Apache
package:
name: apache2 # Use 'httpd' for RHEL/CentOS
state: present
notify: Restart Apache
- name: Configure Apache
template:
src: apache.conf.j2
dest: /etc/apache2/apache2.conf # Use /etc/httpd/conf/httpd.conf for RHEL/CentOS
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2 # Use 'httpd' for RHEL/CentOS
state: restarted
---
- name: Register variable example
hosts: all
tasks:
- name: Check if Apache is running
service_facts:
- name: Print Apache status
debug:
msg: "Apache is {{ ansible_facts.services['apache2'].state }}" # Use 'httpd' for RHEL/CentOS
when: "'apache2' in ansible_facts.services" # Use 'httpd' for RHEL/CentOS
---
- name: Configure web server
hosts: webservers
tasks:
- name: Template a configuration file
template:
src: apache.conf.j2
dest: /etc/apache2/apache2.conf
owner: root
group: root
mode: '0644'
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
---
- name: Install Python packages
hosts: localhost
tasks:
- name: Install pip
package:
name: python3-pip
state: present
- name: Install packages from requirements.txt
pip:
requirements: /path/to/requirements.txt
virtualenv: /path/to/venv
---
- name: Tagged tasks example
hosts: all
tasks:
- name: Install Git
package:
name: git
state: present
tags:
- git
- packages
- name: Install Curl
package:
name: curl
state: present
tags:
- curl
- packages
---
- name: Run command example
hosts: all
tasks:
- name: Check disk usage
command: df -h
register: disk_usage
- name: Show disk usage
debug:
var: disk_usage.stdout_lines
---
- name: Example of block, rescue, and always
hosts: all
tasks:
- name: Attempt to start the service
block:
- name: Start Apache
service:
name: apache2
state: started
- name: Verify service is running
uri:
url: "http://localhost"
method: GET
register: health_check
retries: 3
delay: 5
until: health_check.status == 200
rescue:
- name: Handle the failure
debug:
msg: "Failed to start the service: {{ ansible_failed_result.msg | default('Unknown error') }}"
- name: Send notification about failure
debug:
msg: "Service start failed on {{ inventory_hostname }}"
always:
- name: Ensure the service status is logged
debug:
msg: "Service start attempted on {{ inventory_hostname }}"
---
- name: Using filters
hosts: all
vars:
my_list: [1, 2, 3, 4, 5]
my_string: "hello world"
tasks:
- name: Demonstrate filter usage
debug:
msg: |
Uppercase: {{ my_string | upper }}
Length: {{ my_string | length }}
Even numbers: {{ my_list | selectattr('mod', '==', 0) | list }}
Sum: {{ my_list | sum }}
---
- name: File operations
hosts: all
tasks:
- name: Create a directory
file:
path: /opt/myapp
state: directory
mode: '0755'
owner: myuser
group: mygroup
- name: Copy a file
copy:
src: /local/path/file.conf
dest: /remote/path/file.conf
owner: myuser
group: mygroup
mode: '0644'
- name: Modify a line in a file
lineinfile:
path: /etc/myapp/config.conf
regexp: '^listen_port='
line: 'listen_port={{ app_port }}'
backup: yes
---
- name: Using vault
hosts: all
vars_files:
- secrets.yml # This file is encrypted with ansible-vault
tasks:
- name: Use encrypted variable
mysql_db:
name: "{{ db_name }}"
state: present
login_user: "{{ db_admin_user }}"
login_password: "{{ db_admin_password }}" # This is stored encrypted
---
- name: Complex loop example
hosts: all
vars:
web_servers:
- name: web1
ip: 192.168.1.10
ports: [80, 443]
- name: web2
ip: 192.168.1.11
ports: [80, 443, 8080]
tasks:
- name: Open firewall ports for each server
ufw:
rule: allow
port: "{{ item.ports_item }}"
proto: tcp
loop: "{{ web_servers | subelements('ports') }}"
loop_control:
loop_var: item
label: "{{ item.name }}:{{ item.ports_item }}"
---
- name: Using includes and imports
hosts: all
tasks:
# Static import (evaluated at parse time)
- import_tasks: install_packages.yml
# Dynamic include (evaluated at runtime)
- include_tasks: configure_services.yml
# Include tasks conditionally
- include_tasks: database_setup.yml
when: install_database | default(false)
---
- name: Working with facts
hosts: all
tasks:
- name: Gather facts
setup:
gather_subset: min # Gather minimal facts for faster execution
- name: Print system information
debug:
msg: |
Hostname: {{ ansible_hostname }}
Distribution: {{ ansible_distribution }} {{ ansible_distribution_version }}
Architecture: {{ ansible_architecture }}
Memory: {{ (ansible_memtotal_mb / 1024) | int }} GB
CPUs: {{ ansible_processor_vcpus }}
Any questions?
Feel free to contact us. Find all contact information on our contact page.