Ansible-角色部署LAMP

配置主机

[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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值