ansible-roles
ansible-doc yum找例子求帮助
ROLE角色扮演
简介:ansible中,playbooke的目录组织结构。将代码或文件进行模块化,成为role的文件目录组织结构,易读,代码可重用,参次清晰。
目标:通过role远程部署nginx并配置。
Nginx角色名
Files普通文件
Handlers触发器程序
Tasks主任务
Templates金甲板(有变量的文件)
Vars自定义变量
1.目录结构
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 123243 > roles/nginx/files/index.html
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
tree roles/
[root@localhost /]# tree roles/
roles/
├── nginx
│ ├── files
│ │ └── index.html
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
│ └── main.yaml
└── site.yaml
6 directories, 6 files
2.编写任务
vim /roles/nginx/tasks/main.yaml
- name: install epel-release packge
yum:
Name:epel-release
State:latest - name: install nginx packge
yum: name=nginx state=latest - name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html - name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx - name: make sure nginx service running
service: name=nginx state=started enabled=yes
3.准备配置文件
[root@localhost ~]# vim roles/nginx/templates/nginx.conf.j2
For more information on configuration, see:
* Official English Documentation: http://nginx.org/en/docs/
* Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes {{ansible_processor_cores}};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections {{worker_connections}};
}
ansible host1 -m setup -a 'filter=ansible_processor_cores'
ansible all -m setup -a 'filter=worker_connections'
4编写变量:
vim roles/nginx/vars/main.yaml
worker_connections: 10233
vim roles/nginx/handlers/main.yaml
- hosts: host4
roles:- nginx
~
tree roles/
vim roles/site.yaml
[root@localhost ansible]# pwd/etc/ansible/vim ansible.cfg
[defaults]
inventory=/etc/ansible/hosts
roles_path=/etc/ansible/roles
collections_path=/etc/ansible/collections
remote_user= root
- nginx
[privilege_escalation]
become=true
become_ask_pass=False
become_method=sudo
become_user=root
5编写处理程序
[root@localhost ansible]# vim roles/nginx/handlers/main.yaml
- name: restart nginx
service:
name: nginx
state: restarted
6编写剧本
[root@localhost ansible]# vim /etc/ansible/yum_repo.yaml
-
name: Add base
hosts: all
tasks:- yum_repository:
name: baseos9
description: baseos9
baseurl: https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/
gpgcheck: false
enabled: true
- yum_repository:
-
name: Add app
hosts: all
tasks:- yum_repository:
name: app9
description: app9
baseurl: https://mirrors.aliyun.com/centos-stream/9-stream/AppStream/
gpgcheck: no
enabled: yes
- yum_repository:
[root@localhost ansible]# cat yum_repo.yaml
-
name: Add base
hosts: all
tasks:- yum_repository:
name: baseos9
description: baseos9
baseurl: https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/
gpgcheck: false
enabled: true
- yum_repository:
-
name: Add app
hosts: all
tasks:- yum_repository:
name: app9
description: app9
baseurl: https://mirrors.aliyun.com/centos-stream/9-stream/AppStream/
gpgcheck: no
enabled: yes
- yum_repository:
[root@localhost ansible]# cat site.yaml
- hosts: host4
roles:- nginx
7实施(建议切换到剧本所在目录)
[root@localhost etc]# tree ansible/
ansible/
├── ansible.cfg
├── ansible.cfg.bak
├── hosts
├── roles
│ └── nginx
│ ├── files
│ │ └── index.html
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
│ └── main.yaml
├── site.yaml
└── yum_repo.yaml
Cd /etc/ansible
检查法1
[root@localhost ansible]#ansible-playbook -C yum_repo.yaml
检查法2
[root@localhost ansible]# ansible-playbook site.yaml --syntax-check
[root@localhost ansible]#ansible-playbook yum_repo.yaml
[root@localhost ansible]#ansible-playbook -C site.yaml
[root@localhost ansible]#ansible-playbook site.yaml
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具