部署环境
ip | 需要安装的服务 | 192.168.75.142 | ansible | 192.168.75.143 | mysql | 192.168.75.150 | apache | 192.168.75.128 | php |
思路:
1.在/opt/palybook目录下分别创建mysql,apache,php的yml文件,编写脚本
2..在ansible主机上安装httpd服务,更改配置文件,然后复制一份移动到/opt/playbook/apache目录下备用
3.在/opt/目录下创建lamp.yml,编写脚本文件,引用三项服务的脚本
4.执行lamp.yml,查看结果
httpd配置
[root@ansible ~]# cp /etctpd/conftpd.conf /optaybook/apachetpd.j2
[root@ansible ~]# cd /optaybook/apache/
[root@ansible apache]# vim httpd.j2
166 <IfModule dir_module>
167 DirectoryIndex index.php index.html //添加index.php,使其可以读取php测试文件
168 </IfModule>
285 #
286 AddType application/x-compress .Z
287 AddType application/x-gzip .gz .tgz
288 AddType application/x-httpd-php .php //添加下面两行
289 AddType application/x-httpd-php-source ,phps
290 #
[root@ansible apache]# vim httpd.yml
[root@ansible apache]# cat httpd.yml
---
- name: install httpd //安装httpd的脚本yml文件
hosts: 192.168.75.150
tasks:
- name: install
service:
name: httpd
enabled: yes
mysql
//创建mysql脚本文件
[root@ansible mysql]# cat mysql.yml
---
- name: install mysql
hosts: 192.168.75.143
tasks:
- name: install mariadb
yum:
name: mariadb , mariadb-server
state: present
- name: enabled
service:
name: mariadb
enabled: true
state: started
php
//创建php脚本文件
[root@ansible php]# cat php.yml
---
- name:
hosts: php
tasks:
- name: config php
lineinfile:
path: /etc/php-fpm.d/www.conf
regexp: '^listen ='
line: listen = 192.168.72.135:9000
- name: php
lineinfile: //修改php的配置文件
path: /etc/php-fpm.d/www.conf
regexp: '^listen.allowed_clients ='
line: listen.allowed_clients = 192.168.72.135
lamp.yml
[root@ansible opt]# cat playbook/lamp.yml
---
- name: httpd
import_playbook: /opt/playbook/apache/httpd.yml
- name: mysql
import_playbook: /opt/playbook/mysql/mysql.yml
- name: php
import_playbook: /opt/playbook/php/php.yml
- hosts: httpd
tasks:
- name: httpd config
template:
src: /opt/playbook/apache/httpd.j2
dest: /etc/httpd/conf/httpd.conf
- name: start httpd
service:
name: httpd
enabled: yes
state: started
- hosts: php
tasks:
- name: index.php
file:
path: /var/www/html/index.php
state: touch
- name: index
lineinfile:
path: /var/www/html/index.php
line: |
<?php
phpinfo();
?>
state: present
- name: start php
service:
name: php-fpm
state: started
enabled: yes
执行脚本,然后进行访问
[root@master xk]# ansible-playbook lamp.yml
PLAY [install httpd] ***************************************************************************************************************************************************
PLAY [install mysql] ***************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [192.168.75.143]
TASK [install mariadb] *************************************************************************************************************************************************
ok: [192.168.75.143]
TASK [enabled] *********************************************************************************************************************************************************
ok: [192.168.75.143]
PLAY [php] *****************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: [192.168.75.128]
TASK [config php] **********************************************************************************************************************
changed: [192.168.75.128]
TASK [php] *****************************************************************************************************************************
changed: [192.168.75.128]
PLAY [httpd] ***************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.75.150]
TASK [httpd config] ********************************************************************************************************************
changed: [192.168.75.150]
TASK [start httpd] *********************************************************************************************************************
ok: [192.168.75.150]
PLAY [php] *****************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.75.128]
TASK [index.php] ***********************************************************************************************************************
changed: [192.168.75.128]
TASK [index] ***************************************************************************************************************************
changed: [192.168.75.128]
PLAY RECAP *****************************************************************************************************************************
192.168.75.128 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.75.150 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
|