Ansible Basics
Configuration management with Ansible playbooks.
Basic Commands
ansible all -m ping # test connectivity
ansible-playbook playbook.yml # run playbook
ansible-playbook playbook.yml --check # dry run
ansible-playbook playbook.yml --syntax-check # check syntax
Inventory File
# hosts.ini
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
[all:vars]
ansible_user=ubuntu
Basic Playbook
---
- name: Configure web servers
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
Common Modules
# Copy file
- copy:
src: /local/file.txt
dest: /remote/file.txt
# Execute command
- command: ls -la
# Install package
- apt:
name: nginx
state: present
Variables
# Define variables
vars:
http_port: 80
app_name: myapp
# Use variables
port: {{ http_port }}