ansible 中利用playbook部署lamp环境

该博客详细介绍了如何利用Ansible的playbook来部署LAMP环境,包括配置httpd、mariadb和php。内容包括安装相关软件包、配置文件的模板化处理以及服务的启动和启用。在httpd部分,展示了如何安装httpd,复制配置文件并启动服务。mariadb部分涉及安装数据库服务和启动。最后,php-apache部分说明了安装php组件,配置php.ini并设置首页。

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

基本文件目录如下:

httpd所需软件:httpd mariadb mariadb-server php  php-mysql   gd    php-gd

[root@ansible role]# ls
httpd          site_nginx.retry  site.retry
mariadb  php-apache  site_apache.yaml 

[root@ansible role]# cat site_apache.yaml
---
- hosts: group
  roles:
  - httpd
  - php-apache
  - mariadb

1.httpd目录

[root@ansible role]# cd httpd/
[root@ansible httpd]# ls
files  handlers  tasks  templates  vars

[root@ansible httpd]# cat files/index.html
hello world

[root@ansible httpd]# cat handlers/main.yaml
---
- name: reload httpd.service
  service: name=httpd state=reloaded

[root@ansible httpd]# cat files/index.html
hello world
[root@ansible httpd]# cat handlers/main.yaml
---
- name: reload httpd.service
  service: name=httpd state=reloaded
[root@ansible httpd]# cat tasks/main.yaml
---
- name: install httpd
  yum: name=httpd state=present

- name: copy httpd configure file
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: reload httpd.service

- name: copy index.html
  copy: src=index.html dest=/var/www/html/index.html

- name: start and enable httpd
  service: name=httpd state=started enabled=yes

[root@ansible httpd]# ls templates/
httpd.conf.j2

在httpd.conf.j2中定义变量

Listen {{ httpd_port }}

[root@ansible httpd]# cat vars/main.yaml
 httpd_port: 80

2.mariadb目录

[root@ansible role]# cd mariadb/
[root@ansible mariadb]# ls
handlers  tasks  templates  vars        //mariadb 不需要测试首页可以不写

[root@ansible mariadb]# cat handlers/main.yaml
---
- name: restart mariadb.service
  service: name=mariadb state=restarted

[root@ansible mariadb]# cat handlers/main.yaml
---
- name: restart mariadb.service
  service: name=mariadb state=restarted
[root@ansible mariadb]# cat tasks/main.yaml
---
- name: install mariadb
  yum: name={{ item }} state=present
  with_items:
  - mariadb
  - mariadb-server

- name: copy mariadb configure file
  template: src=my.cnf.j2 dest=/etc/
  notify: restart mariadb.service

- name: start and enable mariadb
  service: name=mariadb state=started enabled=yes

[root@ansible mariadb]# ls templates/
my.cnf.j2

[root@ansible mariadb]# cat vars/main.yaml
 datadir: /var/lib/mysql
 socket: /var/lib/mysql/mysql.sock

3.apache中php目录

[root@ansible php-apache]# ls
files  handlers  tasks  templates

[root@ansible php-apache]# cat handlers/main.yaml
---
- name: restart httpd
  service: name=httpd state=restarted

[root@ansible php-apache]# cat handlers/main.yaml
---
- name: restart httpd
  service: name=httpd state=restarted
[root@ansible php-apache]# cat tasks/main.yaml
---
- name: install php-fpm
  yum: name={{ item }} state=present
  with_items:
  - php
  - php-gd
  - php-mysql
  - gd

- name: copy php configure file
  template: src=php.ini.j2 dest=/etc/php.ini
  notify: restart httpd

- name: copy index.php
  copy: src=index.php dest=/var/www/html/index.php
  notify: restart httpd

[root@ansible php-apache]# ls templates/
php.ini.j2                                          //不定义变量可以不创建vars目录

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值