内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档介绍Ansi
风哥提示:
ble Roles角色管理的创建和使用方法。
Part01-Role结构
1.1 创建Role目录结构
[root@ansible ~]# mkdir -p /fglinux/ansible/roles/nginx/{tasks,handlers,templates,files,vars,defaults,meta}
# 查看目录结构
[root@ansible ~]# tree /fglinux/ansible/roles/nginx/
/fglinux/ansible/roles/nginx/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
│ └── nginx.conf.j2
└── vars
└── main.yml
# 创建默认变量
[root@ansible ~]# cat > /fglinux/ansible/roles/nginx/defaults/main.yml << 'EOF'
nginx_user: nginx
nginx_group: nginx
nginx_port: 80
nginx_worker_processes: auto
nginx_worker_connections: 1024
nginx_keepalive_timeout: 65
nginx_gzip: "on"
nginx_server_name: localhost
nginx_root: /usr/share/nginx/html
nginx_index: index.html index.htm
EOF
# 创建变量文件
[root@ansible ~]# cat > /fglinux/ansible/roles/nginx/vars/main.yml << 'EOF'
nginx_packages:
- nginx
- nginx-all-modules
nginx_config_path: /etc/nginx/nginx.conf
nginx_conf_dir: /etc/nginx/conf.d
nginx_log_dir: /var/log/nginx
EOF
# 创建任务文件
[root@ansible ~]# cat > /fglinux/ansible/roles/nginx/tasks/main.yml << 'EOF'
- name: 安装Nginx软件包
dnf:
name: "{{ nginx_packages }}"
state: present
notify: Enable Nginx
- name: 创建Nginx用户组
group:
name: "{{ nginx_group }}"
state: present
- name: 创建Nginx用户
user:
name: "{{ nginx_user }}"
group: "{{ nginx_group }}"
shell: /sbin/nologin
create_home: no
- name: 创建日志目录
file:
path: "{{ nginx_log_dir }}"
state: directory
owner: "{{ nginx_user }}"
group: "{{ nginx_group }}"
mode: '0755'
- name: 配置Nginx主配置文件
template:
src: nginx.conf.j2
dest: "{{ nginx_config_path }}"
owner: root
group: root
mode: '0644'
backup: yes
notify: Reload Nginx
- name: 创建站点配置目录
file:
path: "{{ nginx_conf_dir }}"
state: directory
owner: root
group: root
mode: '0755'
- name: 创建默认站点根目录
file:
path: "{{ nginx_root }}"
state: directory
owner: "{{ nginx_user }}"
group: "{{ nginx_group }}"
mode: '0755'
- name: 创建默认首页
copy:
content: |
Welcome to FGEDU Nginx Server
dest: “{{ nginx_root }}/index.html”
owner: “{{ nginx_user }}”
group: “{{ nginx_group }}”
mode: ‘0644’
– name: 启动Nginx服务
service:
name: nginx
state: started
enabled: yes
EOF
# 创建handlers
[root@ansible ~]# cat > /fglinux/ansible/roles/nginx/handlers/main.yml << 'EOF'
- name: Reload Nginx
service:
name: nginx
state: reloaded
- name: Restart Nginx
service:
name: nginx
state: restarted
- name: Enable Nginx
service:
name: nginx
enabled: yes
EOF
# 创建配置模板
[root@ansible ~]# cat > /fglinux/ansible/roles/nginx/templates/nginx.conf.j2 << 'EOF'
user {{ nginx_user }};
worker_processes {{ nginx_worker_processes }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections {{ nginx_worker_connections }};
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/ngin更多学习教程公众号风哥教程itpux_comx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout {{ nginx_keepalive_timeout }};
types_hash_max_size 4096;
gzip {{ nginx_gzip }};
gzip_types text/plain text/css application/json application/javascript;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen {{ nginx_port }};
listen [::]:{{ nginx_port }};
server_name {{ nginx_server_name }};
root {{ nginx_root }};
index {{ nginx_index | join(' ') }};
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
}
EOF
# 创建meta信息
[root@ansible ~]# cat > /fglinux/ansible/roles/nginx/meta/main.yml << 'EOF'
galaxy_info:
author: fgedu
description: Nginx Web Server Role
company: FGEDU
license: GPL-2.0
min_ansible_version: 2.9
platforms:
- name: EL
versions:
- 8
- 9
galaxy_tags:
- nginx
- web
- server
dependencies: []
EOF
Part02-使用Role
2.1 调用Role
[root@ansible ~]# cat > /fglinux/ansible/site.yml << 'EOF' --- - name: 部署Nginx Web服务器 hosts: webservers become: yes roles: - role: nginx vars: nginx_port: 80 nginx_worker_connections: 2048 nginx_server_name: "{{ inventory_hostname }}" EOF # 执行Playbook [root@ansible ~]# ansible-playbook /fglinux/ansible/site.yml PLAY [部署Nginx Web服务器] *************************************************** TASK [Gathering Facts] ****************************************************** ok: [web1.fgedu.net.cn] ok: [web2.fgedu.net.cn] TASK [nginx : 安装Nginx软件包] *********************************************** changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 创建Nginx用户组] *********************************************** changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 创建Nginx用户] ************************************************* changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 创建日志目录] ************************************************** changed: [web1.fgedu.net.cn] changed: [web2.学习交流加群风哥微信: itpux-comfgedu.net.cn] TASK [nginx : 配置Nginx主配置文件] ******************************************** changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 创建站点配置目录] *********************************************** changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 创建默认站点根目录] ********************************************* changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 创建默认首页] ************************************************** changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] TASK [nginx : 启动Nginx服务] ************************************************* changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.更多视频教程www.fgedu.net.cncn] RUNNING HANDLER [nginx : Enable Nginx] *************************************** ok: [web1.fgedu.net.cn] ok: [web2.fgedu.net.cn] RUNNING HANDLER [nginx : Reload Nginx] *************************************** changed: [web1.fgedu.net.cn] changed: [web2.fgedu.net.cn] PLAY RECAP ****************************************************************** web1.fgedu.net.cn : ok=12 changed=10 unreachable=0 failed=0 web2.fgedu.net.cn : ok=12 changed=10 unreachable=0 failed=0 # 验证Nginx状态 [root@ansible ~]# ansible webservers -m shell -a "systemctl status nginx | head -10" web1.fgedu.net.cn | CHANGED | rc=0 >>
● nginx.service – The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Fri 2026-04-04 02:00:00 CST; 1min ago
Process: 12345 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 12346 (nginx)
Tasks: 3 (limit: 11232)
Memory: 5.0M
CGroup: /system.slice/nginx.service
├─12346 “nginx: master process /usr/sbin/nginx”
- 按功能模块划分Role
- 使用defaults定义默认变量
- 使用handlers处理服务变更
- 添加meta信息便于分享
- 使用Ansible Galaxy管理Roles
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
