在服务器规模化的时代,Ansible就像一位"自动化指挥官"️,让你轻松管理成百上千台Linux主机!本文将带你全面掌握Ansible的核心用法,从基础架构到高级Playbook,从主机管理到应用部署。无论你是要管理几台服务器还是整个数据中心,这些Ansible技巧都能让你的运维效率提升10倍!准备好你的控制节点,让我们一起进入自动化运维的世界吧~
# Ubuntu/Debian
sudo apt update
sudo apt install ansible
# RHEL/CentOS
sudo yum install epel-release
sudo yum install ansible
# 验证安装
ansible --version
# /etc/ansible/hosts 或项目目录中的hosts文件
[web_servers]
web1.example.com ansible_user=admin
web2.example.com ansible_port=2222
[db_servers]
db1.example.com
db2.example.com
[cluster:children]
web_servers
db_servers
[all:vars]
ansible_python_interpreter=/usr/bin/python3
# AWS EC2示例
ansible-inventory -i aws_ec2.yml --graph
# 主机变量
# inventory文件中定义
web1.example.com ansible_user=admin custom_var=value
# 组变量
# group_vars/web_servers.yml
---
http_port: 80
https_port: 443
ansible [pattern] -m [module] -a "[module options]" [options]
# 检查所有主机连通性
ansible all -m ping
# 收集主机信息
ansible all -m setup
# 在多台主机上执行命令
ansible web_servers -m shell -a "uptime"
# 管理服务
ansible web_servers -m service -a "name=nginx state=restarted"
# 文件分发
ansible db_servers -m copy -a "src=/local/path dest=/remote/path"
# 软件包管理
ansible all -m apt -a "name=nginx state=latest" --become
# site.yml 示例
---
- name: Configure Web Servers
hosts: web_servers
become: yes
vars:
http_port: 80
max_clients: 200
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: latest
- name: Copy Nginx config
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
# 用户管理
- name: Add user
user:
name: webadmin
groups: www-data
shell: /bin/bash
password: "{{ 'password' | password_hash('sha512') }}"
# 文件权限
- name: Set file permissions
file:
path: /var/www/html
owner: www-data
group: www-data
mode: '0755'
state: directory
# 定时任务
- name: Add cron job
cron:
name: "Backup database"
minute: "0"
hour: "2"
job: "/opt/scripts/backup.sh"
# 条件执行
- name: Install EPEL on CentOS
yum:
name: epel-release
state: present
when: ansible_distribution == "CentOS"
# 循环示例
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- mysql-server
- php-fpm
roles/
nginx/
tasks/
main.yml
handlers/
main.yml
templates/
nginx.conf.j2
vars/
main.yml
defaults/
main.yml
files/
custom.conf
ansible-galaxy init roles/nginx
- hosts: web_servers
roles:
- nginx
- { role: mysql, db_password: 'secret' }
-e
)vars:
)roles/xxx/defaults
)tasks:
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- mysql-server
tags: packages
# 运行指定标签的任务
ansible-playbook site.yml --tags "packages"
# 创建加密文件
ansible-vault create secret.yml
# 编辑加密文件
ansible-vault edit secret.yml
# 运行使用加密数据的Playbook
ansible-playbook site.yml --ask-vault-pass
# users.yml
- name: Manage Users
hosts: all
become: yes
vars_files:
- secrets/users_pass.yml # 加密的密码文件
tasks:
- name: Create users
user:
name: "{{ item.name }}"
groups: "{{ item.groups | default('users') }}"
password: "{{ item.password | default(users_default_pass) }}"
shell: "{{ item.shell | default('/bin/bash') }}"
loop: "{{ users }}"
# cluster_sync.yml
- name: Sync Cluster Configuration
hosts: cluster_nodes
become: yes
tasks:
- name: Copy configuration files
synchronize:
src: /local/configs/
dest: /etc/app/
delete: yes
recursive: yes
# system_update.yml
- name: Update All Systems
hosts: all
become: yes
tasks:
- name: Update apt cache (Debian)
apt:
update_cache: yes
when: ansible_os_family == "Debian"
- name: Upgrade all packages (Debian)
apt:
upgrade: dist
when: ansible_os_family == "Debian"
- name: Update yum packages (RHEL)
yum:
name: '*'
state: latest
when: ansible_os_family == "RedHat"
# 控制并行进程数
ansible-playbook site.yml -f 10 # 使用10个并行进程
# ansible.cfg
[defaults]
forks = 20
host_key_checking = False
pipelining = True
- name: Long running task
command: /opt/scripts/long_task.sh
async: 3600 # 超时时间(秒)
poll: 0 # 不等待完成
通过本文的系统学习,我们已经掌握了Ansible批量管理的完整技能:
自动化黄金法则:
记住:自动化不是可选项,而是必选项! 现在就用Ansible解放你的双手吧!✨
PS:如果你在学习过程中遇到问题,别慌!欢迎在评论区留言,我会尽力帮你解决!