Ansible-角色部署LAMP

这篇博客详细介绍了如何利用Ansible自动化工具配置LAMP(Linux, Apache, MySQL, PHP)环境。首先,配置主机并创建角色,接着通过apache角色安装和配置httpd服务,包括yum源、编译脚本和环境变量设置。然后,利用mysql角色来配置MySQL数据库,涉及初始化脚本、上传二进制包和模板文件。最后,通过php角色来部署PHP,包括上传软件包、编写tasks任务和进行php编译。" 130938160,7513255,自定义数据集上训练YOLO NAS的实战指南,"['YOLO', '深度学习', '计算机视觉', '目标检测', '自定义数据集']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置主机

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# vim hosts
[dev]
node1

创建角色

[root@ansible ansible]# cd roles/
[root@ansible roles]# ls
[root@ansible roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@ansible roles]# ansible-galaxy init php
- Role php was created successfully
[root@ansible roles]# ls
apache  mysql  php

通过apache角色配置httpd服务
先编写tasks任务

[root@ansible roles]# vim apache/tasks/main.yml 
---
# tasks file for apache
- name: set firewalld
  service: 
    name: firewalld
    state: stopped
    enabled: no
- name: stop selinux
  lineinfile:
    path: /etc/sysconfig/selinux
    regexp: '^SELINUX='
    line: 'SELINUX=disabled'
- name: setenforce 0
  shell: setenforce 0
- name: yum.sh
  script: yum.sh
- name: install pkgs
  yum:
    name: "{
  
  { httpdpkgs }}"
    state: present
- name: uzip pkgs
  unarchive: 
    src: apr-1.6.5.tar.bz2
    dest: /opt/
- name: uzip pkgs1
  unarchive:
    src: apr-util-1.6.1.tar.bz2
    dest: /opt/
- name: uzip pkgs2
  unarchive:
    src: httpd-2.4.54.tar.bz2
    dest: /opt/
- name: create user 
  user: 
    name: apache
    system: yes
    shell: /sbin/nologin
    create_home: no
    state: present
- name: make install
  script: httpd.sh
- name: bianliang
  script: bianliang.sh
- name: set service
  template:
    src: httpd.service.j2
    dest: /usr/lib/systemd/system/httpd.service
- name: daemon-reload
  shell: systemctl daemon-reload
- name: restart httpd
  service: 
    name: httpd
    state: started
    enabled: yes

yum源脚本

[root@ansible apache]# vim files/yum.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

编译脚本

[root@ansible apache]# vim files/httpd.sh
#!/bin/bash
cd /opt/apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install

cd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install

cd /opt/httpd-2.4.54
./configure --prefix=/usr/local/apache \
 --sysconfdir=/etc/httpd24 \
 --enable-so \
 --enable-ssl \
 --enable-cgi \
 --enable-rewrite \
 --with-zlib \
 --with-pcre \
 --with-apr=/usr/local/apr \
 --with-apr-util=/usr/local/apr-util/ \
 --enable-modules=most \
 --enable-mpms-shared=all \
 --with-mpm=prefork
make
make install

环境变量脚本

[root@ansible apache]# vim  files/bianliang.sh
export PATH=/usr/local/apache/bin/:$PATH

vars变量

[root@ansible apache]# vim vars/main.yml 
---
# vars file for apache
httpdpkgs:
  - bzip2  
  - make 
  - wget 
  - openssl-devel 
  - pcre-devel 
  - expat-devel 
  - libtool 
  - gcc 
  - gcc-c++ 
  - libxml2-devel 

模板文件

[root@ansible apache]# vim  templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target 
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-u
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值