ansible剧本搭建lamp/lnmp练习

为192.168.122.111配置lamp或者lnmp环境
在服务器端安装ansible,为192.168.122.111设置域名为kvm1,推公钥,开启kvm1的sshd服务。
在/etc/ansible/hosts中配置kvm1的参数,做过解析写域名,没做过解析写ip
创建目录准备写剧本
[root@ansible ~]# mkdir /role
role目录总结构:
[root@ansible role]# tree . -L 1
.
├── apache (apache目录,定义安装httpd角色)
├── mariadb (mariadb目录,定义安装mariadb角色)
├── nginx (nginx目录,定义安装nginx角色)
├── php_apache (php_apache目录安装与httpd对应配置的php)
├── php_nginx (php_nginx目录安装与nginx对应配置的php)
└── site.yaml (site.yaml文件,存放哪个主机用哪个角色)
/role/apache目录总结构
[root@ansible ~]#cd apache/
[root@ansible apache ]#tree . -L 1
.
├── files (files目录,用来存放没有变量的文件)
├── handlers (目录,存放配置触发器的文件)
├── tasks (目录,存放剧本文件)
├── templates (目录,存放使用变量的文件,一般放准备好的配置文件)
└── vars (目录,存放定义整个角色变量的文件,键值对方式定义变量)
目录名字为相应的模块名,将每一个功能分离出来定义成角色更有条理性。
apache结构具体内容
[root@ansible apache ]# tree
.
├── files
│ └── index.html (文件,httpd测试用的网页文件)
├── handlers
│ └── main.yaml (文件,配置触发器内容)
├── tasks
│ └── main.yaml (文件,主要剧本)
├── templates
│ └── httpd.conf.j2 (文件,为httpd服务准备的配置文件,其中一些用了变量)
└── vars
└── main.yaml (文件,存放了httpd配置文件中的变量定义)
文件具体内容
[root@ansible apache ]# cat files/index.html
welcome to apache again

[root@ansible apache]# cat tasks/main.yaml    handlers/main.yaml  
---
- name: install httpd
  yum: name=httpd state=present

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

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

- name: remove nginx 
  yum: name=nginx state=absent

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

---
- name: reload httpd
  service: name=httpd state=reloaded

在tasks/main.yaml用notify调用了handlers/main.yaml 里名为reload httpd的任务列表,当配置文件有变化时,则reload httpd服务(因为httpd服务与nginx共用80端口,若有nginx服务正在运行则httpd服务无法启动,因此先卸载nginx再启动httpd)。
template模块(可以获取变量的值,copy模块不能)会自动识别templates目录里的以j2(Jinja2模板)结尾的文件,因此src不用绝对路径。
[root@ansible apache ]# cat vars/main.yaml
httpd_port: 80
将配置文件里的端口用变量表示,ansible中用{{ }}使用变量
[root@ansible apache ]# cat templates/httpd.conf.j2 | grep Listen
Listen {{ httpd_port }}

/role/mariadb目录总结构

[root@ansible mariadb]# tree
.
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── my.cnf.j2 (mariadb的配置文件)
└── vars
└── main.yaml
[root@ansible mariadb]# cat tasks/main.yaml handlers/main.yaml
在这里插入图片描述
在这里插入图片描述
[root@ansible mariadb]# cat vars/main.yaml
data_dir: /var/lib/mysql
socket_file: /var/lib/mysql/mysql.sock
[root@ansible mariadb]# cat templates/my.cnf.j2
[mysqld]
datadir= {{ data_dir }}
socket= {{ socket_file }}
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

/role/php_apache目录总结构
[root@ansible php_apache]# tree
.
├── files
│ └── index.php
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ ├── php.ini.j2
│ └── www.conf.j2
└── vars
└── main.yaml

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

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

- name: copy www.conf configure
  template: src=www.conf.j2 dest=/etc/php-fpm.d/www.conf
  notify: restart httpd

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

---
- name: restart httpd
  service: name=httpd state=restarted

[root@ansible php_apache]# cat files/index.php

<?php phpinfo(); ?>

[root@ansible php_apache]# cat vars/main.yaml
user: apache
num1: 2048M
num2: 2048M
num3: 2048M
[root@chenjiaqi php_apache]# cat templates/php.ini.j2 | grep num[0-9]
memory_limit = {{ num1 }}
post_max_size = {{ num2 }}
upload_max_filesize = {{ num3 }}
安装lamp的环境
在/role/site.yaml配置kvm1主机扮演角色如下
[root@ansible role]# pwd
/role
[root@ansible role]# cat site.yaml
在这里插入图片描述

lnmp环境和lamp环境中的mariadb配置相同
/role/nginx 目录总结构
[root@ansible nginx]# tree
.
├── files
│ └── index.html
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── nginx.conf.j2
└── vars
└── main.yaml
在这里插入图片描述
配置nginx来支持php
找到一个没有改过的nginx配置文件,发现在nginx安装目录下的conf.d/default.conf下有模版文件,直接copy到我们在templates里准备的nginx.conf中,添加以下行:

    server {
        listen       {{ nginx_port }} default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
location ~ \.php$ {
            root                {{ nginx_web_dir }};
            fastcgi_pass        127.0.0.1:9000;
            fastcgi_index       index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include             fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }



/role/php_nginx目录总结构
[root@ansible php_nginx]# tree
.
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ ├── php.ini.j2
│ └── www.conf.j2
└── vars
└── main.yaml

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

- name: start php-fpm service
  service: name=php-fpm state=started enabled=yes

- name: copy php configure
  template: src=php.ini.j2 dest=/etc/php.ini
  notify: restart nginx
---
- name: restart nginx
  service: name=nginx state=restarted
user: apache
num1: 2048M
num2: 2048M
num3: 2048M

Nginx是个轻量级的HTTPserver,必须借助第三方的FCGI处理器(php-fpm)才可以对PHP进行解析。
[root@ansible php_nginx]# cat templates/php.ini.j2 |grep num[0-9]
memory_limit = {{ num1 }}
post_max_size = {{ num2 }}
upload_max_filesize = {{ num3 }}
安装lnmp的环境
在/role/site.yaml配置kvm1主机扮演角色如下

[root@ansible role]# cat site.yaml 
---
- hosts: kvm1
  roles:
  - nginx
  - mariadb
  - php_nginx

猜你喜欢

转载自blog.csdn.net/weixin_42275939/article/details/82762514