zoukankan      html  css  js  c++  java
  • ansible之利用角色简化playbook

    角色结构描述

    Ansible角色提供了一种方法,让用户能以通用的方式更加轻松地重复利用Ansible代码。通过打包的方式将任务归档至一起,更加灵活的调用

    Ansible角色具有下列优点:

    • 模块化,通用配置
    • 角色可以分组内容,从而与他人轻松共享代码
    • 可以编写角色来定义系统类型的基本要素:Web服务器、数据库服务器、Git存储库,或满足其他用途
    • 角色使得较大型项目更容易管理
    • 角色可以由不同的管理员并行开发

    角色结构目录

    • role目录结构
    [root@localhost roles]# tree user.example/
    user.example/
    ├── defaults #默认值,可以修改的变量
    │   └── main.yml
    ├── files #调用的静态文件(安装包等)
    ├── handlers
    │   └── main.yml #处理程序定义
    ├── meta
    │   └── main.yml #角色相关信息
    ├── README.md
    ├── tasks
    │   └── main.yml #角色任务定义
    ├── templates #角色引用的模板定义
    ├── tests
    │   ├── inventory
    │   └── test.yml # 角色里test.yml(查看运行是否正常,给用户演示如何使用)
    └── vars
        └── main.yml 定义角色的变量值
    

    定义变量和默认值

    • vars/main.yml定义变量
    • defaults/main.yml 定义刻意需要覆盖的变量

    调用角色

    • 调用角色
    //用到roles模块
    ---
    - hosts: remote.example.com
      roles:
        - role1
        - role2
    
    • 设置角色role2变量
    ---
    - hosts: remote.example.com
      roles:
        - role: role1
        - role: role2
          var1: val1
          var2: val2
    
    • 和上面等效的语法
    ---
    - hosts: remote.example.com
      roles:
        - role: role1
        - { role: role2, var1: val1, var2: val2 }
    

    执行顺序

    • 按照任务列表执行
    - name: Play to illustrate order of execution
      hosts: remote.example.com
      pre_tasks:
        - debug:
          msg: 'pre-task'
          notify: my handler
      roles:
        - role1
      tasks:
        - debug:
          msg: 'first task'
          notify: my handler #notify任务是整个task运行完毕后触发
      post_tasks:
        - debug:
          msg: 'post-task'
          notify: my handler
      handlers:
        - name: my handler
          debug:
            msg: Running my handler
    
    • 动态包含角色,更灵活
    - name: Execute a role as a task
      hosts: remote.example.com
      tasks:
        - name: A normal task
          debug:
            msg: 'first task'
        - name: A task to include role2 here
          include_role: role2 #ansible2.3新增include_role,2.4新增import_role
    

    红帽企业自带系统角色

    自RHEL7.4开始,操作系统随附了多个Ansible角色,作为rhel-system-roles软件包的一部分。在RHEL8中,该软件包可以从AppStream中获取。

    RHEL系统角色

    角色 状态 描述
    rhel-system-roles.kdump 全面支持 配置kdump崩溃恢复服务
    rhel-system-roles.network 全面支持 配置网络接口
    rhel-system-roles.selinux 全面支持 配置和管理SELinux自定义,包括SELinux模式、
    文件和端口上下文、布尔值设置以及SELinux用户
    rhel-system-roles.timesync 全面支持 使用网络时间协议或精确时间协议配置时间同步
    rhel-system-roles.postfix 技术预览 使用Postfix服务将每个主机配置为邮件传输代理
    rhel-system-roles.firewall 开发中 配置主机的防火墙
    rhel-system-roles.tuned 开发中 配置tuned服务,以调优系统性能

    目的

    在多个版本之间标准化红帽企业Linux子系统的配置。使用系统角色来配置版本6.10及以上的任何红帽企业Linux主机

    简化配置管理

    通过系统角色实现不同版本主机配置

    安装rhel系统角色

    • 安装
    [root@node0 ~]# yum -y install rhel-system-roles
    ...
    Installed:
      rhel-system-roles-1.0-20.el8.noarch                                                                                                            
    
    Complete!
    
    
    • 查看已安装的系统角色
    [root@node0 ~]# ls /usr/share/ansible/roles/
    linux-system-roles.certificate      linux-system-roles.network     rhel-system-roles.kdump            rhel-system-roles.postfix
    linux-system-roles.kdump            linux-system-roles.postfix     rhel-system-roles.kernel_settings  rhel-system-roles.selinux
    linux-system-roles.kernel_settings  linux-system-roles.selinux     rhel-system-roles.logging          rhel-system-roles.storage
    linux-system-roles.logging          linux-system-roles.storage     rhel-system-roles.metrics          rhel-system-roles.timesync
    linux-system-roles.metrics          linux-system-roles.timesync    rhel-system-roles.nbde_client      rhel-system-roles.tlog
    linux-system-roles.nbde_client      linux-system-roles.tlog        rhel-system-roles.nbde_server
    linux-system-roles.nbde_server      rhel-system-roles.certificate  rhel-system-roles.network
    
    

    默认路径包含/usr/share/ansible/roles/,playbook引用时就能找到

    [root@node0 ~]# vim /etc/ansible/ansible.cfg 
    ...
    #roles_path    = /etc/ansible/roles #取消注释后更改路径就可能找不到了
    
    
    

    访问系统角色文档

    [root@node0 ~]# ll /usr/share/doc/rhel-system-roles/
    total 4
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 certificate
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 kdump
    drwxr-xr-x. 2 root root   72 Feb 23 22:45 kernel_settings
    drwxr-xr-x. 2 root root   72 Feb 23 22:45 logging
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 metrics
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 nbde_client
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 nbde_server
    drwxr-xr-x. 2 root root 4096 Feb 23 22:45 network
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 postfix
    drwxr-xr-x. 2 root root   93 Feb 23 22:45 selinux
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 storage
    drwxr-xr-x. 2 root root  136 Feb 23 22:45 timesync
    drwxr-xr-x. 2 root root   57 Feb 23 22:45 tlog
    
    

    创建角色

    从创建到使用分三步

    1. 创建角色目录结构
    2. 定义角色内容
    3. 在playbook中使用角色

    创建角色目录结构

    ansible-galaxy init命令创建新角色的目录结构

    [root@node0 project]# cd roles/
    [root@node0 roles]# ansible-galaxy init test #创建一个名为“测试”的角色
    - Role test was created successfully
    [root@node0 roles]# ll
    total 0
    drwxr-xr-x. 10 root root 154 Feb 24 10:34 test
    [root@node0 roles]# tree test/ #查看test的目录结构
    test/
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml
    
    

    定义角色内容

    [root@node0 test]# vim tasks/main.yml 
    
    ---
    # tasks file for test 定义创建用户任务
    - name: useradd
      user:
        name: {{ name }}  #引用变量1
        system: {{ state }} #引用变量2
    
    

    在playbook中使用角色

    添加tom用户

    [root@node0 project]# cat test.yml 
    ---
    - hosts: node1 
      gather_facts: no
      remote_user: root
      vars: 
        name: tom #作为变量嵌套在play的vars关键字中定义
        state: true
      roles:  #roles模块引用test角色
        - test
    //运行
    [root@node0 project]# ansible-playbook test.yml 
    [WARNING]: Found variable using reserved name: name
    
    PLAY [node1] ************************************************************************************************************************************
    
    TASK [test : useradd] ***************************************************************************************************************************
    changed: [node1]
    
    PLAY RECAP **************************************************************************************************************************************
    node1                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
    
    //node1验证
    [root@node1 ~]# id tom 
    uid=991(tom) gid=988(tom) groups=988(tom)
    
    

    通过变量更改角色行为

    • 在play的roles关键字中包含该角色时作为变量定义
    [root@node0 project]# vim test.yml 
    
    ---
    - hosts: node1
      gather_facts: no
      remote_user: root
      roles:
        - test
          name: tom
          state: true
    
    
    • 作为变量嵌套在play的vars关键字中定义
    [root@node0 project]# cat test.yml 
    ---
    - hosts: node1 
      gather_facts: no
      remote_user: root
      vars: 
        name: tom 
        state: true
      roles:  
        - test
    
    • 在清单文件中定义,作为主机变量或组变量
    [root@node0 project]# vim inventory 
    
    [lamp]
    node1 name=tom state=false
    node2
    node3
    [root@node0 project]# vim test.yml 
    
    ---
    - hosts: node1
      gather_facts: no
      remote_user: root
      roles:
        - test
    [root@node0 project]# ansible-playbook test.yml 
    [root@node1 ~]# id tom
    uid=1000(tom) gid=1000(tom) groups=1000(tom)
    
    
    • 在playbook项目的group_vars或host_vars目录下的YAML文件中定义
    [root@node0 project]# mkdir host_vars
    [root@node0 project]# vim host_vars/node1.yml
    
    name: tom
    state: true
    
    [root@node0 project]# ansible-playbook test.yml 
    [root@node1 ~]# id tom
    uid=991(tom) gid=988(tom) groups=988(tom)
    
    
    

    使用ansible-galaxy部署角色

    介绍

    Ansible Galaxy [https://galaxy.ansible.com]是一个Ansible内容公共资源库,这些内容由许许多多Ansible管理员和用户编写。

    获取帮助

    访问该网址获取帮助 [https://galaxy.ansible.com/docs/]

    浏览角色

    点击主页网站search,输入关键词查找

    ansible-galaxy命令行使用

    //搜索平台为EL的lamp角色
    [root@node0 ~]# ansible-galaxy search 'lamp' --platform EL
    
    
    //查看某个角色的详细信息
    [root@node0 ~]# ansible-galaxy info cyberitas.php73_remi
    
    Role: cyberitas.php73_remi
            description: Ansible role for installing php 7.3 on RHEL/CentOs from the Remi repository
            active: True
            commit: 99cc9a1acbe2a47d30837b4bb6057e75c64a7afb
            commit_message: php defaults
            commit_url: https://api.github.com/repos/Cyberitas/ansible-role-php73_remi/git/commits/99cc9a1acbe2a47d30837b4bb6057e75c64a7afb
            company: Cyberitas Technologies, LLC
            created: 2020-06-04T22:43:12.132717Z
            download_count: 71
            forks_count: 0
            github_branch: master
            github_repo: ansible-role-php73_remi
            github_user: Cyberitas
            id: 49072
            imported: 2020-06-04T20:05:00.366133-04:00
            is_valid: True
            issue_tracker_url: https://github.com/Cyberitas/ansible-role-php73_remi/issues
            license: license (MIT)
            min_ansible_version: 2.4
            modified: 2020-06-05T00:05:00.373057Z
            open_issues_count: 0
            path: ('/root/.ansible/roles', '/usr/share/ansible/roles', '/etc/ansible/roles')
            role_type: ANS
            stargazers_count: 0
            travis_status_url: 
    
    

    安装搜索到的角色

    默认安装在用户~/.ansible/roles/下,可在ansible配置文件更改路径或命令行添加- p指定存放路径

    //以httpd为例
    [root@node0 ~]# ansible-galaxy install robertdebock.httpd
    [root@node0 ~]# ll .ansible/roles/
    total 4
    drwxr-xr-x. 10 root root 4096 Feb 25 12:14 robertdebock.httpd
    
    
    [root@node0 ~]# ansible-galaxy install robertdebock.httpd -p project/roles/
    - downloading role 'httpd', owned by robertdebock
    [WARNING]: Unable to retrieve role (id=21855) data (versions), but this is not fatal so we continue: The read operation timed out
    - downloading role from https://github.com/robertdebock/ansible-role-httpd/archive/6.0.3.tar.gz
    - extracting robertdebock.httpd to /root/project/roles/robertdebock.httpd
    - robertdebock.httpd (6.0.3) was installed successfully
    [root@node0 ~]# ll project/roles/
    total 4
    drwxr-xr-x. 10 root root 4096 Feb 25 12:25 robertdebock.httpd
    drwxr-xr-x. 10 root root  154 Feb 24 10:34 test
    
    

    使用要求文件安装角色

    在项目目录创建roles/requirements.yml来指定特定角色

    [root@node0 project]# vim roles/requirements.yml
    
    - src: geerlingguy.redis #指定源
        version: "1.5.0" #指定版本
    
    
    [root@node0 project]# ansible-galaxy install -r roles/requirements.yml -p roles/ geerlingguy.redis
    - downloading role 'redis', owned by geerlingguy
    - downloading role from https://github.com/geerlingguy/ansible-role-redis/archive/1.5.0.tar.gz
    - extracting geerlingguy.redis to /root/project/roles/geerlingguy.redis
    - geerlingguy.redis (1.5.0) was installed successfully
    [root@node0 project]# ll roles/
    total 8
    drwxr-xr-x.  9 root root  174 Feb 25 12:48 geerlingguy.redis
    -rw-r--r--.  1 root root   44 Feb 25 12:47 requirements.yml
    drwxr-xr-x. 10 root root 4096 Feb 25 12:25 robertdebock.httpd
    drwxr-xr-x. 10 root root  154 Feb 24 10:34 test
    
    

    管理已下载的角色

    • 查看角色
    [root@node0 project]# ansible-galaxy list
    # /root/.ansible/roles
    # /usr/share/ansible/roles
    - linux-system-roles.certificate, (unknown version)
    - linux-system-roles.kdump, (unknown version)
    - linux-system-roles.kernel_settings, (unknown version)
    - linux-system-roles.logging, (unknown version)
    - linux-system-roles.metrics, (unknown version)
    ...
    
    • 移除已安装的角色
    [root@node0 project]# ansible-galaxy remove geerlingguy.redis -p roles/
    - successfully removed geerlingguy.redis
    
    
  • 相关阅读:
    include与php://input执行任意命令
    php Session反序列化漏洞
    php代码审计-file_get_contents()&file_put_contents()
    php代码审计-用户名和密码分开检验
    php -- get_magic_quotes_gpc()函数
    md5(“ffifdyop“,true)
    php弱类型相关
    BurpWeb安全学院之XXE
    ICMP隐藏通信隧道技术
    BurpWeb安全学院之敏感信息泄露
  • 原文地址:https://www.cnblogs.com/fangxinxin/p/14440666.html
Copyright © 2011-2022 走看看