# Install Ansible on Debian/Ubuntu
sudo apt update
sudo apt install ansible
# Install Ansible on CentOS/RHEL
sudo yum install ansible
# Check Ansible version
ansible --version
# Ping test to check connectivity
ansible all -m ping
# Ad-hoc command example (installing a package)
ansible webserver -m yum -a "name=httpd state=latest"
---
- name: Configure Web Servers
hosts: webservers
become: yes
tasks:
- name: Install Apache
yum:
name: httpd
state: present
- name: Start Apache Service
service:
name: httpd
state: started
---
- name: Setup Monitoring
hosts: monitoring
become: yes
tasks:
- name: Install SNMP Service
yum:
name: net-snmp
state: present
- name: Configure SNMP Community
template:
src: snmpd.conf.j2
dest: /etc/snmp/snmpd.conf
notify:
- restart snmpd
handlers:
- name: restart snmpd
service:
name: snmpd
state: restarted
yum: Manages packages on RPM-based systems. apt: Manages packages on Debian-based systems. file: Manages files and directories. copy: Copies files to remote locations. service: Manages services on the system. template: Manages file templates.
[webservers]
webserver1 ansible_host=192.168.1.101
webserver2 ansible_host=192.168.1.102
[monitoring]
monitor1 ansible_host=192.168.1.201
# Variable example
---
web_servers:
- hostname: webserver1
ip: 192.168.1.101
# Template example
---
- name: Configure Apache
template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
Use -vvv for verbose output to debug. Ensure correct YAML syntax in playbooks (ansible-lint for linting). Utilize roles for modular and reusable playbook organization. Leverage Ansible Galaxy for community-contributed roles.
For comprehensive Ansible documentation and tutorials, visit Ansible Documentation.