• Ansible中的角色使用


    目录

    ansible roles

    ansible 角色简介

     roles目录结构

    role存放的路径在配置文件ansible.cfg中定义

    创建目录结构

    控制任务执行顺序

    ansible-galaxy命令工具

     安装选择的角色

    示例


    ansible roles

    ansible 角色简介

    Ansible roles 是为了层次化结构化的组织Playbook

    roles就是通过分别将变量文件任务模块及处理器放置于单独的目录中并可以便捷地include它们

    roles一般用于基于主机构建服务的场景中在企业复杂业务场景中应用的频率很高

    以特定的层级目录结构进行组织的 tasks variables handlers templates files 相当于函数的调用把各个功能切割成片段来执行。

     roles目录结构

    1. files //存放copy或script等模块调用的函数
    2. tasks //定义各种task,要有main.yml,其他文件include包含调用
    3. handlers //定义各种handlers,要有main.yml,其他文件include包含调用
    4. vars //定义variables,要有main.yml,其他文件include包含调用
    5. templates //存储由template模块调用的模板文本
    6. meta //定义当前角色的特殊设定及其依赖关系,要有main.yml的文件 defaults##要有main.yml的文件,用于设定默认变量
    7. tests //用于测试角色

    role存放的路径在配置文件ansible.cfg中定义

    roles_path = path / roles 默认目录 / etc / ansible / roles

    创建目录结构

    1. ansible-galaxy init httpd //创建目录结构
    2. ansible-galaxy list //显示当前 Ansible Galaxy 环境中安装的角色列表

    控制任务执行顺序

    1. hosts: server2
    2. roles:
    3. - role: role1 //角色任务
    4. pre_tasks: //角色执行前执行的play
    5. - tasks1
    6. tasks: //普通任务
    7. - tasks2
    8. post_tasks: //在角色和普通任务执行完毕后执行的play
    9. - tasks3

    ansible-galaxy命令工具

    Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站 可以帮助我们更好的定义和学习 roles
    ansible - galaxy 命令默认与 https :// galaxy.ansible.com 网站 API 通信 可以查找 下载各种社区开发的 Ansible 角色
    galaxy.ansible.com 网站查询 roles

     安装选择的角色

    1. install https://galaxy.ansible.com roles
    2. ansible-galaxy install geerlingguy.nginx
    3. install local roles
    4. vim install_apache_role.yml
    5. ---
    6. - src: file:///mnt/apache.tar.gz
    7. name: apache
    8. ansible-galaxy install -r install_apache_role.yml

    示例

    下载安装apache,根据变量更改配置文件,并在访问login.westos.org时需要用户认证

    ansible-galaxy init httpd        //创建目录结构

    生成认证文件

     htpasswd -cm htpasswd admin
    

    tasks主任务

    1. [devops@ansible1 .ansible]$ cat roles/httpd/tasks/main.yml
    2. ---
    3. # tasks file for httpd
    4. - name: install httpd
    5. yum:
    6. name: httpd
    7. - name: create vhosts configure
    8. template:
    9. src: vhosts.conf.j2
    10. dest: /etc/httpd/conf.d/vhosts.conf
    11. notify: restarted httpd
    12. - name: create auth file
    13. copy:
    14. src: htpasswd
    15. dest: /etc/httpd/htpasswd
    16. - name: start service
    17. service:
    18. name: httpd
    19. state: restarted
    20. enabled: yes

    vars变量模块

    1. [devops@ansible1 .ansible]$ cat roles/httpd/vars/main.yml
    2. ---
    3. # vars file for httpd
    4. WEBS:
    5. - docroot: /var/www/html
    6. index: www.westos.org
    7. - docroot: /var/www/virtual/westos.org/bbs/html
    8. name: bbs.westos.org
    9. index: bbs.westos.org
    10. - docroot: /var/www/virtual/westos.org/login/html
    11. name: login.westos.org
    12. index: login.westos.org

    templates模块

    1. [devops@ansible1 .ansible]$ cat roles/httpd/templates/vhosts.conf.j2
    2. {% for web in WEBS %}
    3. {% if web.name is not defined %}
    4. <VirtualHost _default_:80>
    5. {% endif %}
    6. {% if web.name is defined %}
    7. <VirtualHost *:80>
    8. ServerName {{web.name}}
    9. {% endif %}
    10. DocumentRoot {{web.docroot}}
    11. </VirtualHost>
    12. {% endfor %}
    13. <Directory /var/www/virtual/westos.org/login/html>
    14. AuthUserFile /etc/httpd/htpasswd
    15. AuthName "Please input username and password"
    16. AuthType basic
    17. Require valid-user
    18. </Directory>

    触发器模块

    1. [devops@ansible1 .ansible]$ cat roles/httpd/handlers/main.yml
    2. ---
    3. # handlers file for httpd
    4. - name: restarted httpd
    5. service:
    6. name: httpd
    7. state: restarted
    8. enabled: no
    9. - name: firewalld
    10. firewalld:
    11. service: httpd
    12. state: enabled
    13. permanent: yes
    14. immediate: yes

    启用模块

    1. [devops@ansible1 .ansible]$ cat httpd.yml
    2. - name: install httpd server
    3. hosts: all
    4. roles:
    5. - role: httpd
    6. pre_tasks:
    7. - name: show pre
    8. debug:
    9. msg: start
    10. post_tasks:
    11. - name: show post
    12. debug:
    13. msg: end

    测试

  • 相关阅读:
    【LeetCode贪心#12】图解监控二叉树(正宗hard题,涉及贪心分析、二叉树遍历以及状态转移)
    Java-多线程基础及线程安全
    Python学习:lambda,sort,filter,map,递归函数的运用
    Flutter 常见错误记录总结
    Jmeter的使用说明
    掌握JavaScript的练习之道:十个手写函数让你信手拈来!
    回转窑无线测温系统解决方案
    【王道代码】【2.3链表】d3
    能够解决传统FTP传输弊端的替代工具是什么样的?
    某零售企业招聘管理体系搭建咨询项目
  • 原文地址:https://blog.csdn.net/m0_64028800/article/details/134231725