1、基础操作模块
command模块
功能:执行简单命令(不支持管道符、重定向)。
示例:
ansible websrvs -m command -a "ls /tmp"
shell模块
功能:支持复杂Shell命令(如管道符、条件判断)。
示例:
ansible all -m shell -a "grep 'error' /var/log/messages | wc -l"
copy模块
功能:复制本地文件到远程主机,支持权限和属主设置。
示例:
ansible node1 -m copy -a "src=/etc/hosts dest=/etc/hosts mode=644 owner=root"
file模块
功能:管理文件或目录(创建、删除、权限、软链接)。
示例:
ansible web -m file -a "path=/data state=directory mode=755" # 创建目录
ansible db -m file -a "src=/data/logs dest=/var/log state=link" # 创建软链接
2、 系统管理模块
yum模块
功能:安装/卸载RPM包(自动处理依赖)。
示例:
ansible all -m yum -a "name=nginx state=latest" # 安装最新版nginx
ansible node1 -m yum -a "name=mysql-server state=absent" # 卸载MySQL
user模块
功能:管理用户账号(创建、删除、设置密码)。
示例:
ansible db -m user -a "name=appuser uid=1001 group=appgroup" # 创建用户
cron模块
功能:管理定时任务。
示例:
ansible all -m cron -a "minute='*/5' job='/usr/bin/backup.sh' name='Daily Backup'"
3、信息获取与测试模块
setup模块
功能:收集主机信息(如内存、网卡、磁盘)。
示例:
ansible node1 -m setup -a "filter=ansible_eth*" # 查看网卡信息
ping模块
功能:测试主机连通性(返回pong表示成功)。
示例:
ansible all -m ping
模块特点总结
幂等性:大部分模块(如yum、copy)支持幂等性,重复执行无副作用。
参数查询:使用ansible-doc -s <模块名>查看模块详细参数。
注:示例中需替换websrvs、node1等主机组名为实际环境中的目标主机组。
---
- hosts: all
become: yes
vars:
nginx_install_method: "yum" # 可选值:yum(包安装)或 compile(编译安装)
nginx_version: "1.24.0" # 编译安装时指定版本
nginx_install_dir: "/usr/local/nginx" # 编译安装路径
nginx_conf_path: "/etc/nginx/nginx.conf" # 配置文件路径(yum安装默认路径)
tasks:
- name: 创建临时目录
ansible.builtin.file:
path: /tmp/nginx
state: directory
mode: 0755
when: nginx_install_method == "compile" # 仅编译安装需要:ml-citation{ref="4,5" data="citationList"}
- block: # YUM安装流程:ml-citation{ref="2,3" data="citationList"}
- name: 安装Nginx(YUM方式)
ansible.builtin.yum:
name: nginx
state: latest
when: nginx_install_method == "yum"
- name: 复制自定义配置文件
ansible.builtin.copy:
src: files/nginx.conf
dest: "{{ nginx_conf_path }}"
backup: yes
mode: 0644
notify: restart nginx
when: nginx_install_method == "yum"
- block: # 编译安装流程:ml-citation{ref="4,5" data="citationList"}
- name: 安装编译依赖
ansible.builtin.yum:
name:
- gcc
- pcre-devel
- zlib-devel
- openssl-devel
- make
state: present
- name: 下载Nginx源码包
ansible.builtin.get_url:
url: "http://nginx.org/download/nginx-{{ nginx_version }}.tar.gz"
dest: /tmp/nginx/nginx.tar.gz
- name: 解压源码包
ansible.builtin.unarchive:
src: /tmp/nginx/nginx.tar.gz
dest: /tmp/nginx
remote_src: yes
- name: 编译安装Nginx
ansible.builtin.shell:
cmd: |
cd /tmp/nginx/nginx-{{ nginx_version }}
./configure --prefix={{ nginx_install_dir }}
make && make install
args:
creates: "{{ nginx_install_dir }}/sbin/nginx" # 幂等性检查:ml-citation{ref="4" data="citationList"}
- name: 创建Systemd服务文件
ansible.builtin.template:
src: templates/nginx.service.j2
dest: /etc/systemd/system/nginx.service
mode: 0644
notify: reload systemd
when: nginx_install_method == "compile"
handlers:
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
when: nginx_install_method == "yum"
- name: reload systemd
ansible.builtin.systemd:
daemon_reload: yes
- name: enable and start nginx(编译安装)
ansible.builtin.service:
name: nginx
state: started
enabled: yes
when: nginx_install_method == "compile"
使用说明
变量控制:
修改 nginx_install_method 为 yum 或 compile 切换安装方式。
编译安装需指定 nginx_version 和 nginx_install_dir。
文件准备:
YUM安装:需提前准备 files/nginx.conf 配置文件模板。
编译安装:需准备 templates/nginx.service.j2 Systemd服务模板。
执行命令:
ansible-playbook nginx_install.yml -e "nginx_install_method=yum" # YUM安装
ansible-playbook nginx_install.yml -e "nginx_install_method=compile" # 编译安装
初始化主机名
替换yum源或apt源
安装时间同步服务器,指向国内的服务器
关闭防火墙,清理相关规则
安装基本软件 vim 等
修改时区为Asia/Shanghai
初始化一个mage用户,拥有sudo权限 ALL=(ALL) NOPASSWD: ALL
---
- hosts: all
become: yes
vars:
system_type: "rocky" # 可选项:rocky / ubuntu
time_server: "ntp.aliyun.com"
base_packages:
rocky: [vim, chrony, wget]
ubuntu: [vim, ntp, wget]
tasks:
# 1. 初始化主机名
- name: 设置主机名
ansible.builtin.hostname:
name: "{{ inventory_hostname }}" # 使用Inventory定义的主机名:ml-citation{ref="7" data="citationList"}
when: inventory_hostname is defined
# 2. 替换软件源
- name: 替换Rocky YUM源
ansible.builtin.copy:
src: "files/rocky.repo"
dest: "/etc/yum.repos.d/rocky.repo"
mode: 0644
when: system_type == "rocky":ml-citation{ref="5" data="citationList"}
- name: 替换Ubuntu APT源
ansible.builtin.copy:
src: "files/ubuntu-sources.list"
dest: "/etc/apt/sources.list"
mode: 0644
when: system_type == "ubuntu":ml-citation{ref="5" data="citationList"}
# 3. 安装时间同步服务
- name: 安装时间同步工具
ansible.builtin.package:
name: "{{ (system_type == 'rocky') | ternary('chrony', 'ntp') }}"
state: present:ml-citation{ref="6" data="citationList"}
- name: 配置时间服务器
ansible.builtin.lineinfile:
path: "/etc/chrony.conf"
regexp: "^pool"
line: "pool {{ time_server }} iburst"
state: present
when: system_type == "rocky":ml-citation{ref="6" data="citationList"}
- name: 启动时间同步服务
ansible.builtin.service:
name: "{{ (system_type == 'rocky') | ternary('chronyd', 'ntp') }}"
enabled: yes
state: restarted:ml-citation{ref="6" data="citationList"}
# 4. 关闭防火墙
- name: 关闭Rocky防火墙
ansible.builtin.service:
name: firewalld
state: stopped
enabled: no
when: system_type == "rocky":ml-citation{ref="5" data="citationList"}
- name: 关闭Ubuntu防火墙
ansible.builtin.service:
name: ufw
state: stopped
enabled: no
when: system_type == "ubuntu":ml-citation{ref="5" data="citationList"}
# 5. 安装基础软件
- name: 安装系统基础软件包
ansible.builtin.package:
name: "{{ base_packages[system_type] }}"
state: present:ml-citation{ref="4,5" data="citationList"}
# 6. 修改时区
- name: 设置时区为上海
ansible.builtin.timezone:
name: Asia/Shanghai:ml-citation{ref="5" data="citationList"}
# 7. 初始化mage用户
- name: 创建mage用户
ansible.builtin.user:
name: mage
shell: /bin/bash
password: "{{ 'mage123' | password_hash('sha512') }}":ml-citation{ref="5" data="citationList"}
- name: 配置sudo免密权限
ansible.builtin.lineinfile:
path: /etc/sudoers
line: "mage ALL=(ALL) NOPASSWD: ALL"
validate: visudo -cf %s:ml-citation{ref="5" data="citationList"}
# 8. Rocky关闭SELinux(仅限Rocky系统)
- name: 永久关闭SELinux
ansible.builtin.selinux:
state: disabled
when: system_type == "rocky":ml-citation{ref="3,5" data="citationList"}
- name: 临时关闭SELinux
ansible.builtin.command: setenforce 0
when: system_type == "rocky"
ignore_errors: yes:ml-citation{ref="3" data="citationList"}
使用说明
1、文件准备:
创建 files/rocky.repo(Rocky Linux YUM源配置)
和 files/ubuntu-sources.list(Ubuntu APT源配置)。
2、变量控制:
# Rocky系统初始化
ansible-playbook init.yml -e "system_type=rocky"
# Ubuntu系统初始化
ansible-playbook init.yml -e "system_type=ubuntu"
3、关键逻辑说明:
SELinux管理:仅针对Rocky系统执行关闭操作,包含永久配置和临时关闭。
软件源替换:通过不同系统类型分发对应的源文件,需提前准备模板。
时区设置:使用Ansible内置 timezone 模块确保兼容性。
用户权限:通过 lineinfile 安全修改sudoers文件,避免直接编辑风险
一、服务端部署流程
1、安装依赖与软件
(1)Linux系统:
# Ubuntu/Debian
sudo apt update && sudo apt install open easy-rsa # :ml-citation{ref="2,6" data="citationList"}
# Rocky/CentOS
sudo yum install epel-release && sudo yum install open easy-rsa # :ml-citation{ref="2,8" data="citationList"}
(2)Windows服务器:从官网下载安装包并运行安装程序,生成证书需使用easy-rsa工具。
2、生成证书与密钥
cd /usr/share/easy-rsa
sudo ./easyrsa init-pki # 初始化PKI目录
sudo ./easyrsa build-ca nopass # 生成无密码CA证书:ml-citation{ref="1,2" data="citationList"}
sudo ./easyrsa build-server-full server nopass # 生成服务器证书
sudo ./easyrsa gen-dh # 生成Diffie-Hellman参数:ml-citation{ref="2" data="citationList"}
3、配置服务端文件
创建配置文件/etc/open/server.conf,示例内容:
proto udp
port 1194
dev tun
ca /etc/open/ca.crt
cert /etc/open/server.crt
key /etc/open/server.key
dh /etc/open/dh.pem
server 10.8.0.0 255.255.255.0 # 客户端IP分配网段:ml-citation{ref="2,3" data="citationList"}
push "route 192.168.0.0 255.255.255.0" # 推送内网路由规则:ml-citation{ref="5,7" data="citationList"}
keepalive 10 120
persist-key
persist-tun
4、启动服务
sudo systemctl enable open@server --now # Linux:ml-citation{ref="2" data="citationList"}
Windows需通过服务管理器启动。
二、客户端配置与连接
1、安装客户端程序
Windows:下载官方安装包,安装时允许安装虚拟网卡驱动。
Linux:使用包管理器安装open。
2、获取配置文件
从服务端导出客户端证书(client.crt、client.key)和ca.crt,并编写client.o配置文件。
3、连接VPN
(1)Windows:右键任务栏OpenVPN图标导入.o文件,输入账号密码连接。
(2)Linux:
sudo open --config client.o # :ml-citation{ref="2,6" data="citationList"}
三、基本使用与维护
1、内网访问
客户端连接后可通过分配的IP访问服务端推送的内网资源(如192.168.0.0/24网段)。
2、路由配置
服务端需在防火墙添加规则放行VPN端口(默认UDP 1194),并配置NAT转发。
3、日志与排错
查看日志:
journalctl -u open@server # Linux:ml-citation{ref="2" data="citationList"}