实战-使用 Playbook 批量部署多台 LAMP 环境

devtools/2025/3/5 0:14:35/

实战-使用 Playbook 批量部署多台 LAMP 环境

playbooks 使用步骤

playbook 是一个不同于使用 ansible 命令行执行方式的模式,功能更强大更灵活。

1、在 playbooks 中定义任务:
- name: task description #任务描述信息
module_name: module_args #需要使用的模块名字: 模块参数
2、ansible-playbook 执行 命令:
[root@xuegod63 ~]# ansible-playbook site.yml
playbook 是由一个或多个"play"组成的列表。play 的主要功能在于将事先归为一组的主机装扮成事先通过Ansible 中的 task 定义好的角色。
github 上提供了大量的实例供大家参考 https://github.com/ansible/ansible-examples

实战 1:使用 Playbook 批量部署多台 LAMP 环境

Playbook 常用文件夹作用:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件,handlers['hændlə z] 处理程序
meta:角色定义,可留空; meta ['metə] 元
tasks:需要进行的执行的任务;
templates:用于执行 lamp 安装的模板文件,一般为脚本; templates ['templɪts] 模板
vars:本次安装定义的变量

首先,我们可以在 Ansible 服务器上安装 LAMP 环境,然后,再将配置文件通过 Ansible 拷贝到远程主机上
第一步:安装 httpd 软件
[root@xuegod63 ~]# yum install httpd -y
第二部:安装 MySQL
root@xuegod63 ~]# yum install mariadb-server mariadb -y
[root@xuegod63 ~]# mkdir -p /mydata/data #创建目录作为数据存放的位置
[root@xuegod63 ~]# chown -R mysql:mysql /mydata/
[root@xuegod63 ~]# vim /etc/my.cnf #改变数据存放目录
改:2 datadir=/var/lib/mysql 
为:2 datadir=/mydata/data
[root@xuegod63 ~]# systemctl start mariadb #启动 MariaDB

第三步:安装 PHP 和 php-mysql 模块
[root@xuegod63 ~]# yum install php php-mysql –y
第四步:提供 php 的测试页
[root@xuegod63 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>
启动 httpd 服务,在浏览器中访问

[root@xuegod63 ~]# systemctl restart httpd 
[root@xuegod63 ~]# iptables -F
测试:http://192.168.1.63/index.php

确保已经出现上面的测试页,而且,要看到 MySQL 已经被整合进来了,才能进行下一步操作
定义组名:
[root@xuegod63 ~]# vim /etc/ansible/hosts #还使用之前定义好的,这里不用修改
[web-servers]
192.168.1.63
192.168.1.64

然后,将公钥信息复制到被控制节点,Ansible 和两个节点间通过 ssh 进行连接。下面 3 个命令之前已经做过,
不用执行了。
[root@xuegod63 ~]# ssh-keygen
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.63
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.64

使用 playbook 创建一个 LAMP 构建的任务

1、创建相关文件
[root@xuegod63 ~]# mkdir -pv 
/etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handler
s}
我们将上面搭建成功的 LAMP 环境的 httpd 和 MySQL 的配置文件拷贝到对应目录下
[root@xuegod63 ~]# cd /etc/ansible/ 
[root@xuegod63 ~]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
[root@xuegod63 ~]# cp /etc/my.cnf lamp/roles/mysql/files/写 prepare(前期准备)角色的 playbooks
[root@xuegod63 ansible]# vim lamp/roles/prepare/tasks/main.yml #复制以下红色内容到文件中,
配置好 yum 源
- name: delete yum config
 shell: rm -rf /etc/yum.repos.d/* #删除原有的 yum 配置文件
- name: provide yumrepo file
 shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #下
载新的 yum 配置文件
- name: clean the yum repo
shell: yum clean all #清除原有的 yum 缓存信息
- name: clean the iptables
 shell: iptables -F #清除原有防火墙规则,不然后可能上不了网

2、构建 httpd 的任务
[root@xuegod63 ansible]# cd /etc/ansible/lamp/roles 
[root@xuegod63 roles]# mv /var/www/html/index.php httpd/files/
[root@xuegod63 roles]# vim httpd/tasks/main.yml #将以下内容复制到文件中
- name: web server install
yum: name=httpd state=present #安装 httpd 服务
- name: provide test page
copy: src=index.php dest=/var/www/html #提供测试页
- name: delete apache config
 shell: rm -rf /etc/httpd/conf/httpd.conf #删除原有的 apache 配置文件,如果不删除,下面的 copy
任务是不会执行的,因为当源文件 httpd.conf 和目标文件一样时,copy 命令是不执行的。如果 copy 命令不执行,那么 notify 将不调用 handler。
- name: provide configuration file copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf #提供 httpd 的配置文件 notify: restart httpd #当前面的 copy 复制成功后,通过 notify 通知名字为 restart httpd 的 handlers运行。

扩展:notify 和 handlers notify [ˈnə ʊ tɪfaɪ] 通知
notify: 这个 action 可用于在每个 play 的最后被触发,这样可以避免多次有改变发生时,每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。

在 notify 中列出的操作称为 handler,也即 notify 中调用 handler 中定义的操作。
---- name: test.yml just for test 
 hosts: testserver 
 vars: 
 region: ap-southeast-1 
 tasks: 
 - name: template configuration
 file template: src=template.j2 dest=/etc/foo.conf 
 notify: 
 - restart memcached 

- restart apache 
 handlers: 
 - name: restart memcached 
 service: name=memcached state=restarted 
 - name: restart apache 
 service: name=apache state=restarted

handlers 概述:
Handlers 也是一些 task 的列表,通过名字来引用,它们和一般的 task 并没有什么区别。
Handlers 是由通知者进行 notify, 如果没有被 notify,handlers 不会执行。
不管有多少个通知者进行了 notify,等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次。
Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了。
3、构建 httpd 的 handlers
[root@xuegod63 roles]# vim httpd/handlers/main.yml
- name: restart httpd
service: name=httpd enabled=yes state=restarted
4、部署我们的 MariaDB 数据库
创建 MySQL 服务的任务,需要安装 MySQL 服务,改变属主信息,启动 MySQL
[root@xuegod63 roles]# cd /etc/ansible/lamp/roles 
[root@xuegod63 roles]# vim mysql/tasks/main.yml
- name: install the mysql
 yum: name=mariadb-server state=present #安装 mysql 服务
- name: mkdir date directory
 shell: mkdir -p /mydata/data #创建挂载点目录
- name: provide configration file
 copy: src=my.cnf dest=/etc/my.cnf #提供 mysql 的配置文件
- name: chage the owner
 shell: chown -R mysql:mysql /mydata/* #更改属主和属组
- name: start mariadb
 service: name=mariadb enabled=yes state=started #启动 mysql 服务
5、构建 PHP 的任务
[root@xuegod63 roles]# vim php/tasks/main.yml
- name: install php
yum: name=php state=present #安装 php
- name: install php-mysql
yum: name=php-mysql state=present #安装 php 与 mysql 交互的插件

6、定义整个的任务
[root@xuegod63 roles]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# vim site.yml #写入以下内容
- name: LAMP build

remote_user: root
 hosts: web-servers
 roles:
 - prepare
 - mysql
 - php 
- httpd

注:所有 yml 的配置文件中,空格必须严格对齐

开始部署:
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts 
/etc/ansible/lamp/roles/site.yml
然后,在浏览器中访问这两台节点主机,可以直接访问成功。
http://192.168.1.63/index.php

注:
1、默认情况下,首次登陆一台服务器,系统会提示是否要记住对端的指纹,用 Ansible 也会这样,这样会导致需要手工输入 yes 或 no,Ansible 才可以往下执行。如需避免这种情况,需要在 /etc/ansible/ansible.cfg 文件中设置 host_key_checking = False
例 1:
[root@xuegod63 roles]# rm -rf /root/.ssh/known_hosts 
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml #发现需要输入 yes,
来保存对端的指纹
解决:
[root@xuegod63 roles]# vim /etc/ansible/ansible.cfg
改:62 #host_key_checking = False #就是把前面的#号去了
为:host_key_checking = False 

[root@xuegod63 roles]# rm -rf /root/.ssh/known_hosts 
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml #发现不需要输入 yes,
可以自动安装了


http://www.ppmy.cn/devtools/164600.html

相关文章

Google chrome拦截某些下载内容

现在越来越多的单位和个人都开始使用Google chrome了&#xff0c;本人也觉得chrome浏览器很好用&#xff0c;页面加载速度极快&#xff0c;能快速呈现网页内容&#xff0c;提升浏览效率。扩展程序丰富&#xff0c;涵盖办公、学习、娱乐、开发等众多领域&#xff0c;可满足各种个…

CogFindCircleTool工具

CogFindCircleTool是专门用于在工业图像中自动检测圆形或圆弧的特征&#xff0c;它通过分析图像中的边缘信息&#xff0c;拟合出最优的圆形集合参数(如圆心坐标、半径)&#xff0c;常用于精密测量、定位或质量控制等场景。 效果图&#xff1a; CogFindCircleTool工具功能 圆…

PHP面试题--后端部分

本文章持续更新内容 之前没来得及整理时间问题导致每次都得找和重新背 这次整理下也方便各位小伙伴一起更轻松的一起踏入编程之路 欢迎各位关注博主不定期更新各种高质量内容适合小白及其初级水平同学一起学习 一起成为大佬 数组函数有那些 ps&#xff1a;本题挑难的背因为…

doOnNext() vs flatMap():区别与适用场景

在 Reactor&#xff08;Flux / Mono&#xff09;中&#xff0c;doOnNext() 和 flatMap() 都可以用来处理流中的元素&#xff0c;但它们有不同的作用和适用场景。 1. doOnNext() ✅ 作用 用于执行副作用&#xff08;side effects&#xff09;&#xff0c;但不会改变数据流。适…

解决Docker拉取镜像超时错误,docker: Error response from daemon:

当使用docker pull或docker run时遇到net/http: request canceled while waiting for connection的报错&#xff0c;说明Docker客户端在访问Docker Hub时出现网络连接问题。可以不用挂加速器也能解决&#xff0c;linux不好用clash。以下是经过验证的方法&#xff08;感谢轩辕镜…

Word快速替换修改学术论文所有中的中括号引用未上标格式

问题 如图是平时使用Word写完论文时候交叉引用的引用序号&#xff0c;由中括号和引用序号构成&#xff0c;如果不想手动修改使其上标&#xff0c;那么可以使用正则表达式来进行快速匹配替换使其上标&#xff0c;从而减少时间浪费&#xff0c;且能够保持交叉引用的跳转功能&…

陕西省地标-DB61/T 1121-2018 政务服务中心建设和运营规范

揭秘陕西省智慧政务服务中心新标准&#xff1a;打造高效便捷的服务新体验 随着信息化时代的深入发展&#xff0c;智慧政务已成为提升政府服务效率、优化营商环境的重要举措。陕西省作为全国政务改革的先行者&#xff0c;近期颁布了《陕西省地标-DB61_T 1121-2018 政务服务中心…

<el-cascader时只取最后一级数据

在用cascader时只取最后一级数据传给后端 组件的属性emitPath: false就可以做到&#xff0c;取值就是最后一级传给后端。并且后端放回的id 也直接可以做回显 <el-cascaderv-model"Type":options"Options":props"{ value: id, label: label, chil…