欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

ansible-play中role的基本用法

程序员文章站 2022-06-11 08:29:55
#role应用 #roles跟调用角色的剧本文件应该与roles同级关系,即放在ansible目录下 #makir /root/ansible/roles/{nginx,http,ftp,mysql,redis} palybook.yml roles/ project/ tasks/ 定义task,... ......
#role应用
#roles跟调用角色的剧本文件应该与roles同级关系,即放在ansible目录下
#makir /root/ansible/roles/{nginx,http,ftp,mysql,redis}
palybook.yml
roles/
    project/
        tasks/   定义task,role的基本元素,至少包含一个main.yml文件
        files/   存放由copy或script模块等调用的文件
        vars/     定义变量文件
        templates/  template模块查找所需要末班文件的目录
        handlers/
        default/  设定默认变量
#以nginx为例
思路:
1.group:创建用户组nginx
2.user:创建用户nginx
3.yum:安装nginx
4.template:配置文件更新nginx.conf
5.service:启动nginx
####################################################################
cd /root/ansible/roles/nginx
mkdir tasks templates
cd task

touch group.yml
- name: create group nginx
    group: name=nginx gid=80

touch user.yml
-name: create user nginx
    user: name=nginx uid=80 group=nginx system=yes shell=/sbi/nologin
 
touch install.yml
- name: install package
    yum: name=nginx
   
touch start.yml
- name: start service
    service: name=nginx state=started enabled=yes
    
touch restart.yml
- name: restart service
    service: name=nginx state=restarted
    
touch templ.yml
- name: copy conf
    template: src=nginx.conf.j2 dest=/etc/nginx/conf/nginx.conf
    
touch main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: templ.yml
- include: start.yml
      
cd ../templates  && ll
nginx.conf.j2

cd /root/ansible
touch nginx_role.yml
- hosts: websrvs
  remote_user: root
  roles:
    - role: nginx

执行命令:ansible-playbook nginx_role.yml