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

[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

[root@localhost ansible]# cat yum_repo.yaml

[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

posted on   沐心q  阅读(5)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示