• 自动化运维ansible(role)


    一、role的介绍
    1、Roles称为角色,本质上是为简化playbook配置文件而产生的一种特殊的方法。
    2、简单来说,roles就是将原本在一个yaml中的文件进行规则化分散,封装到不同的目录下,从而简化playbook的yaml配置文件大小。从其实现方法上来看,类似于软件开发上的代码封装。
    3、其格式下的目录结构是特定的,必须包含tasks、variables、handlers、templates、files目录;使用时只需要遵循其文件结构配置自己的定制化操作。
    二、目录说明

    defaults/main.yml:设置默认变量的地方;默认变量的优先级在所有的变量中是最低的,用于定义一些需要被覆盖的变量;
    files:Ansible中unarchive、copy等模块会自动来这里找文件,从而我们不必写绝对路径,只需写文件名
    handlers/main.yml:存放tasks中的notify指定的内容
    meta/main.yml:定义role依赖关系的文件
    tasks/main.yml:存放playbook的目录,其中main.yml是主入口文件,在main.yml中导入其他yml文件,要采用import_tasks关键字,include将要弃用了
    templates:存放模板文件;template模块会将模板文件中的变量替换为实际值,然后覆盖到客户机指定路径上
    vars/main.yml:定义role中需要使用到的变量
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三、nginx安装示例

    [root@192 role]# tree nginx/
    nginx/
    ├── files
    │   └── inex.html
    ├── handles
    │   └── main.yml
    ├── install-nginx.yml
    ├── tasks
    │   ├── conf.yml
    │   ├── data.yml
    │   ├── install.yml
    │   ├── main.yml
    │   ├── service.yml
    │   └── user.yml
    ├── templates
    │   └── nginx.conf.j2
    └── vars
        └── main.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    [root@192 role]# cat nginx/files/inex.html
    <h1>welcome to beijing!</h1>
    
    • 1
    • 2
    [root@192 role]# cat nginx/tasks/conf.yml
    - name: config file
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify: restart #触发notify和handlers两个角色
    
    • 1
    • 2
    • 3
    • 4
    [root@192 role]# cat nginx/tasks/data.yml
    - name: install package
      yum: name=nginx
    
    • 1
    • 2
    • 3
    [root@192 role]# cat nginx/tasks/install.yml
    - name: yum 安装nginx
      yum: name=nginx
    
    • 1
    • 2
    • 3
    [root@192 role]# cat nginx/tasks/main.yml
    - include: install.yml
    - include: conf.yml
    - include: service.yml
    - include: data.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    [root@192 role]# cat nginx/tasks/service.yml
    - name: service
      service: name=nginx state=started
    
    • 1
    • 2
    • 3
    [root@192 role]# cat nginx/tasks/user.yml
    - name: 创建用户
      vars_file:
        - /home/admin/ansible/roles/nginx/vars/main.yml
      user: name={{ username }} system=yes group={{ groupname }}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    [root@192 role]# cat nginx/templates/nginx.conf.j2
    user {{username}};
    worker_processes 2;
    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 1024;
    }
    
    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/nginx/access.log  main;
    
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    server {
    listen       8080;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html;
    
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    
    error_page 404 /404.html;
    location = /404.html {
    }
    
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
    }
    
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    [root@192 role]# cat nginx/vars/main.yml
    username: daemon
    
    • 1
    • 2
    [root@192 role]# cat nginx/install-nginx.yml
    - hosts: harbor
      remote_user: root
    
      roles:
        - role: nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    当nginx启动成功后增加handles中文件

    [root@192 role]# cat nginx/handles/main.yml
    - name: restart
      service: name=nginx state=restarted
    
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    大数据必学Java基础(九十七):事务及回滚点
    学成在线第一天-项目介绍、项目的搭建、开发流程以及相关面试题
    2小时开发《点球射门游戏》,动画演示思路(上),代码已开源
    ElementUI Message 消息提示,多个显示被覆盖的问题
    输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。c++
    【异常的打卡记录】python实现-附ChatGPT解析
    Day38——进程的创建方法,join方法,进程对象
    论文阅读——Align before Fuse
    初级算法_数组 --- 只出现一次的数字
    Mysql 按照每小时,每天,每月,每年,不存在数据也显示
  • 原文地址:https://blog.csdn.net/weixin_45432833/article/details/133977897