Ansible学习笔记15

news/2024/10/25 3:18:07/

1、roles:(难点)

roles介绍:

roles(角色):就是通过分别将variables,tasks及handlers等放置于单独的目录中,并可以便捷地调用他们的一种机制。

假设我们要写一个playbook来安装管理lamp环境,那么这个playbook就会写很长。所以我们希望把这个很大的文件分成多个功能拆分,分成apache管理、php管理、mysql管理,然后在需要使用的时候直接调用就可以了,避免重复写。类似编程里的模块化的概念,已达到代码复用的效果。

创建roles的目录结构:

1、files:用来存放由copy模块或者scripts模块调用的模板;

2)tasks:至少有一个main.yml文件,定义各个tasks;

3)handlers:有一个main.yml文件,定义各handlers;

4)templates:用来存放jinja2模板;

5)vars:有一个main.yml,定义变量的;

6)meta:有一个main.yml文件,定义此角色的特殊设定及其依赖关系。

注意:在每个角色的目录中分别创建files,tasks,handlers,templates,vars和meta目录,用不到的目录可以创建为空目录。

通过roles实现lamp:

需要定制三个角色:apache、mysql、php

[root@localhost roles]# tree
.
├── httpd
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   └── vars
│       └── main.yml
├── mysql
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   └── vars
│       └── main.yml
└── php├── files├── handlers│   └── main.yml├── meta│   └── main.yml├── tasks│   └── main.yml├── templates└── vars└── main.yml21 directories, 12 files

这个roles,就并列写了三个roles。类似于调用了函数。

检查agent的文件:

[root@agent1 share]# cd /var/www/html
[root@agent1 html]# ll
total 8
-rw-r--r-- 1 root root 10 Aug 31 09:07 index.html
-rw-r--r-- 1 root root 22 Aug 31 09:07 test.php
[root@localhost httpd]# cat tasks/main.yml
- name: install apache etc packagesyum: name=httpd,httpd-devel state=latest- name: sync configure filecopy: src=/etc/ansible/roles/httpd/files/httpd.conf dest=/etc/httpd/conf/httpd.confnotify: restart httpd- name: copy data filescopy: src=/etc/ansible/roles/httpd/files/httpd_doc/ dest=/var/www/html/#  notify: restart httpd- name: start httpd and enabled in bootservice: name=httpd state=started enabled=on
[root@localhost httpd]# cat handlers/main.yml
- name: restart httpdservice: name=httpd state=restarted
[root@localhost roles]# cat mysql/tasks/main.yml
- name: Install mariadbyum: name=mariadb-server,mariadb-devel state=latest- name: Start mariadb and enabled=yesservice: name=mariadb state=started enabled=on
[root@localhost roles]# cat php/tasks/main.yml
- name: install php packagesyum: name=php,php-gd,php-ldap,php-odbc,php-pear,php-xml,php-xmlrpc,php-mbstring,php-snmp,php-soap,curl,curl-devel,php-bcmath state=presentnotify: restart httpd

http://www.ppmy.cn/news/1076046.html

相关文章

『Newsletter 丨第二期』PieCloudDB Database 新增控制台、LDAP 支持、虚拟数仓日志等多项功能

PieCloudDB Database 最新动态 云上云版「控制台」功能上线 PieCloudDB 云上云版「控制台」功能全新上线,控制台集成了组织、数仓、用户、费用、权限等多方位管理功能,支持在一个组织下创建和管理多个数仓,并支持独立的一套权限管理体系。管…

package.json 详解

文章目录 package.json1. name2. version3. description4. homepage5. bugs6. license7. author, contributors8. funding9. files10. main11. module12. browser13. bin14. man15. directories15.1 directories.bin15.2 directories.man 16. repository17. scripts18. config1…

​放弃数据库,改用Kafka!

长期以来,数据库一直充当着记录系统,它们以可靠且持久的方式存储和管理关键数据,也赢得了大多数公司的信赖。 但时代在变。许多新兴趋势正在影响当今数据的存储和管理方式,不得不让一些技术决策者们重新考虑数据存储究竟还有哪些…

计算机存储容量单位读法及换算B、KB、MB、GB、TB、PB、EB、ZB、YB、RB、QB

1KB1024B;1MB1024KB10241024B。 1B(byte,字节) 8 bit(位,比特); 1KB(Kilobyte,千字节)1024 B 2^10 B; 1MB(Megabyte&am…

深度学习(前馈神经网络)知识点总结

用于个人知识点回顾,非详细教程 1.梯度下降 前向传播 特征输入—>线性函数—>激活函数—>输出 反向传播 根据损失函数反向传播,计算梯度更新参数 2.激活函数(activate function) 什么是激活函数? 在神经网络前向传播中&#x…

xml中in的使用

目录 一、简介 二、使用 1、参数为list 2、参数为Array 3、参数为Map XML中大于、小于、不等于符号使用 一、简介 在xml中使用in查询需要使用foreach标签 <foreach item"item" collection"list" index"index" open"(" sep…

从传统到智能化:汽车内部通信的安全挑战与SecOC解决方案

01/需求背景 Demand background 在传统的汽车电子结构中&#xff0c;车内的电控单元&#xff08;ECU&#xff09;数量和复杂性受到限制&#xff0c;通信带宽也受到限制。因此&#xff0c;人们普遍认为车内各个ECU之间的通信是可靠的。只要ECU节点接收到相应的消息&#xff0c…

饮食保健杂志饮食保健杂志社饮食保健编辑部2023年第38期目录

论著 中青年人群非酒精性脂肪肝与颈动脉粥样硬化之间关系 徐晓萌1-4 阿司匹林与氯吡格雷对老年冠心病的治疗效果观察 张海峰5-8《饮食保健》投稿&#xff1a;yixuebj126.com 临床研究 骨折复位钢板固定联合自体骨泥植入治疗胫骨中下段骨折的研究 杨翼众 李东生 黄三明 刘禄林 曾…