ansible-playbook部署LAMP和LNMP套件

LAMP

playbook编写

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---
- hosts: all
  tasks: 
    - name: install httpd  # 安装httpd
      yum: name=httpd state=latest
    - name: httpd start and enable # 启动,设置自启
      service: name=httpd state=started enabled=yes
    
    - name: install mariadb  # 安装mariadb
      yum: name={{item}} state=latest
      with_items:
        - mariadb
        - mariadb-server
    - name: mariadb start and enable
      service: name=mariadb state=started enabled=yes

    - name: install php   # 安装php
      yum: name={{item}} state=latest
      with_items:
        - php
        - php-devel
        - php-mysql
      notify:      # 当安装完php,通知handler执行httpd重启
        - httpd restart
  handlers:
    - name: httpd restart
      service: name=httpd state=restarted

然后我们可以使用

1
ansible-playbook xxx.yml --syntax-check

来检查yml是否有语法错误

使用

1
ansible-playbook -i /etc/ansible/hosts ./LAMP.yml

执行剧本

这里,做的时候,另外两台机器之前装过mariadb和httpd,所以这里显示change是0

确定每台机器都正常运行。

LNMP

playbook编写,我们可以直接从刚才的playbook上改动一点就行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---
- hosts: all
  tasks: 
    - name: install nginx  # 安装nginx
      yum: name=httpd state=latest
    - name: httpd start and enable # 启动,设置自启
      service: name=httpd state=started enabled=yes
    
    - name: install mariadb  # 安装mariadb
      yum: name={{item}} state=latest
      with_items:
        - mariadb
        - mariadb-server
    - name: mariadb start and enable
      service: name=mariadb state=started enabled=yes

    - name: install php   # 安装php
      yum: name={{item}} state=latest
      with_items:
        - php
        - php-devel
        - php-mysql
      notify:      # 当安装完php,通知handler执行httpd重启
        - nginx restart
  handlers:
    - name: nginx restart
      service: name=nginx state=restarted

确认无错误,即可开始运行

中途遇到个错误

端口被占用,才想起httpd还开着,然后赶紧关了重新执行剧本

完成安装后,写个php连接mysql就可以确定是否正常。

然后重启nginx,发现不能解析php,然后在/etc/nginx/nginx.conf中编辑

再次重启,又发现中文乱码,还是在nginx.conf中编辑

然后可以正常显示了,连接成功说明都是正常的