Зачем Ansible
Ansible позволяет автоматически настраивать серверы по описанию (infrastructure as code). Не нужны агенты — работает через SSH.
💡 Главное преимущество Ansible — идемпотентность: запустите плейбук 10 раз — результат всегда одинаковый.
Установка Ansible
# Ubuntu/Debian
apt install ansible -y
# Проверка
ansible --version
Файл инвентаря (inventory)
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
Пример плейбука: установка Nginx
---
- name: Install and configure Nginx
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Start and enable nginx
systemd:
name: nginx
state: started
enabled: yes
- name: Deploy config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: reload nginx
handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded
Запуск плейбука
# Тест (dry-run)
ansible-playbook -i inventory.ini setup.yml --check
# Реальный запуск
ansible-playbook -i inventory.ini setup.yml
⚠️ Всегда запускайте с
--check перед применением к production-серверам.