微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

10.ansible 利用playbook部署LAMP环境

基本文件目录如下:
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 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目录

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐