章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • ansible系列文章

    • ansible入门
    • anisble批量安装node_exporter
    • ansible-playbook中的变量
    • Ansible性能优化——提升ansible执行效率
    • ansible之roles简单使用
      • ansible中template简单使用
      • playbook详解
      • ansible-playbook编排使用tips
      • ansible优秀案例
      • 如何优雅向chatgpt提问ansible相关问题
    • Kubernetes笔记

    • elk

    • jenkins

    • GitLabCI_CD

    • 专题
    • ansible系列文章
    章工运维
    2022-12-09
    目录

    ansible之roles简单使用

    # ansible之roles简单使用 (opens new window)

    目录

    • 一、roles简介 (opens new window)
    • 二、简单的roles示例 (opens new window)
    • 三、roles示例二 (opens new window)

    # 一、roles简介

    将多种不同的tasks的文件集中存储在某个目录下,则该目录就是角色,角色一般存放在/etc/ansible/roles/目录下,可通过ansible的配置文件来调整默认的角色目录,/etc/ansible/roles/目录下有很多子目录,其中每一个子目录对应一个角色,每个角色也有自己的目录结构,如图:

    20200322142808

    每个角色的定义,以特定的层级目录结构进行组织。比如:

    • files:存放有copy或script等模块调用的文件;
    • templates:存放template模块查找所需要的模板文件的目录;
    • tasks:任务存放的目录;
    • handlers:存放相关触发执行器的目录;
    • vars:变量存放的目录;
    • meta:用于存放此角色元数据;
    • default:默认变量存放的目录,文件中定义了此角色使用的默认变量;

    上述目录中,tasks、handlers、vars、meta、default至少应该包含一个main.yaml文件,该目录下也可由其他.yaml文件,但是需要在main.yml文件中用include指令将其他.yml文件包含起来。

    # 二、简单的roles示例

    $ mkdir roles   
    #创建一个目录,名称为roles。官方推荐在/etc/ansible/roles/这个目录,不过在哪里都是可以的
    $ mkdir roles/nginx
    $ cd roles/nginx/
    $ mkdir tasks templates
    $ cd tasks/
    $ cat user.yaml 
    - name: create user
      user: name=nginx uid=80 group=nginx system=yes shell=/sbin/nologin
    
    $ cat group.yaml 
    - name: create group
      group: name=nginx gid=80
    $ cat yum.yaml 
    - name: install package
      yum: name=nginx
    
    $ cat templ.yaml 
    - name: copy conf
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf 
    
    $ cat stservice.yaml 
    - name: start service
      service: name=nginx state=started enabled=yes
    
    $ cat main.yaml 
    - include: group.yaml
    - include: user.yaml
    - include: yum.yaml
    - include: templ.yaml
    - include: stservice.yaml
    #如果需要调用别的角色的yaml文件,也可以这样写,比如:- include:roles/httpd/tasks/copyfile.yaml
    注意:copyfiile.yaml文件中的源路径必须是绝对路径
    
    $ ls
    nginx.conf.j2
    #该文件就是nginx的配置文件改名了而已
    $ cd ../../../
    $ cat nginx.yaml 
    - hosts: webservers
      remote_user: root
      roles:
        - role: nginx    
    #定义了多个角色,也可在接着写,每行调用一个角色
    #也可以定义tags标签,比如:- { roles: httpd ,tags:['web','httpd']}或- { roles: httpd ,tags:'web'  如果需要加when语句,在此处添加 }都可以
    $ ls
    nginx.yaml  roles
    #确保调用nginx的yaml文件是和roles是在同一目录下的
    $ tree
    .
    ├── nginx.yaml
    └── roles
        └── nginx
            ├── tasks
            │   ├── group.yaml
            │   ├── main.yaml
            │   ├── reservice.yaml
            │   ├── stservice.yaml
            │   ├── templ.yaml
            │   ├── user.yaml
            │   └── yum.yaml
            └── templates
                └── nginx.conf.j2
    #确保目录的层次效果是这样的
    $ ansible-playbook nginx.yaml 
    #如果定义了标签,就可对标签进行操作(比如:web或httpd)
    $ ansible webservers -m shell -a 'ss -lntp | grep nginx'
    #确认被控端的主机nginx已经启动
    
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68

    一个简单的nginx的roles已经编写完成了!

    # 三、roles示例二

    $ mkdir app
    $ cd app/
    $ mkdir tasks templates vars handlers files
    $ cd tasks/
    $ cat group.yaml 
    - name: create group
      group: name=apache system=yes
    $ cat user.yaml 
    - name: create user
      user: name=apache group=apache system=yes shell=/sbin/nologin
    $ cat yum.yaml 
    - name: install package
      yum: name=httpd
    $ cat templ.yaml 
    - name: copy file
      template: src=httpd.conf.j2 dest=/etc/httpd/httpd.conf
      notify: restart service
    
    $ cat copyfile.yaml 
    - name: copy config
      copy: src=vhosts.conf dest=/etc/httpd/conf.d/ owner=apache
    $ cat start.yaml 
    - name: start service
      service: name=httpd state=started
    $ cat main.yaml 
    - include: group.yaml
    - include: user.yaml
    - include: yum.yaml
    - include: templ.yaml
    - include: copyfile.yaml
    - include: start.yaml
    $ ls files/vhosts.conf 
    files/vhosts.conf
    #空文件用于测试
    $ cat handlers/main.yaml 
    - name: restart service
      service: name=httpd state=restarted
    $ cat templates/httpd.conf.j2 
    #httpd的配置文件
    $ cat vars/main.yaml 
    username: apache
    groupname: apache
    $ tree
    .
    ├── files
    │   └── vhosts.conf
    ├── handlers
    │   └── main.yaml
    ├── tasks
    │   ├── copyfile.yaml
    │   ├── group.yaml
    │   ├── main.yaml
    │   ├── start.yaml
    │   ├── templ.yaml
    │   ├── user.yaml
    │   └── yum.yaml
    ├── templates
    │   └── httpd.conf.j2
    └── vars
        └── main.yaml
    #目录结构
    $ cat httpd.yaml 
    ---
    - hosts: webservers
      remote_user: root
      roles:
        - httpd
    $ ls
    httpd.yaml  nginx.yaml  roles
    #确保与roles目录在同一目录下
    $ ansible-playbook httpd.yaml 
    $ ansible all -m shell -a 'ps -ef | grep apache'
    //根据配置文件中修改的内容自行进行修改
    
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73

    *************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************

    原文链接:https://www.cnblogs.com/lvzhenjiang/p/14199463.html

    微信 支付宝
    #ansible
    上次更新: 2024/10/22, 18:10:01

    ← Ansible性能优化——提升ansible执行效率 ansible中template简单使用→

    最近更新
    01
    shell脚本模块集合
    05-13
    02
    生活小技巧(认知版)
    04-29
    03
    生活小技巧(防骗版)
    04-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式