| 
 一、测试环境| 节点名称 | IP地址 | 
|---|
 | node1 | 192.168.111.10 |  | node2 | 192.168.111.20 |  | node3 | 192.168.111.20 | 
 二、搭建过程1. 创建相关目录[root@node1 ~]
 2. ansible服务器的配置准备相关服务的配置文件、以及wordpress源码包 默认需要的文件这些都不存在,我们在ansible服务器上搭建一遍wordpress博客系统
[root@node1 ~]
[root@node1 ~]
拷贝对应的文件到对应的目录
[root@node1 ~]
[root@node1 ~]
数据库这些需要的文件根据自行配置设置
 3. 环境准备prepare 目录为被控端环境初始化用的
[root@node1 ~]
- name: install wget 
  yum: name=wget state=present 
- name: rm -rf *.repo
  shell: rm -rf /etc/yum.repos.d/*
- name: wget 163.repo
  shell: wget -O /etc/yum.repos.d/Centos-7.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
- name: updaet yum 
  shell: yum repolist 
- name: stop firewalld 
  service: name=firewalld state=stopped enabled=no
- name: stop iptables
  shell: iptables -F
 4. 编写httpd剧本[root@node1 ~]
- name: install httpd 
  yum: name=httpd state=present
- name: copy wordpress
  copy: src=wordpress-4.9.4-zh_CN.tar.gz dest=/opt
- name: copy httpd.conf
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf 
  notify:
    - restart httpd 
- name: tar wordperss
  shell: tar -xf /opt/wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html/
- name: start httpd 
  service: name=httpd state=started enabled=yes 
编写触发条件
[root@node1 ~]
- name: restart httpd  
  service: name=httpd state=restarted
 5. 编写mysql剧本[root@node1 ~]
- name: install mysql
  yum: name=mariadb-server state=present
- name: start mariadb 
  service: name=mariadb state=started enabled=yes
- name: create database wordpress
  shell: mysql -e "create database wordpress;"     
- name: cretae wordpress user   
  shell: mysql -e "grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by '123456';"
 6. 构建PHP剧本[root@node1 ~]
- name: install php
  yum: name=php state=present 
- name: install php-mysql
  yum: name=php-mysql state=present
~                                    
 7. 总剧本[root@node1 ~]
- hosts: all
  remote_user: root
  roles:
    - prepare
    - httpd
    - mysql
    - php
 
 
目录层级关系
[root@node1 roles]
.
├── httpd
│   ├── files
│   │   ├── httpd.conf
│   │   └── wordpress-4.9.4-zh_CN.tar.gz
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
├── mysql
│   ├── files
│   ├── handlers
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
├── php
│   ├── files
│   ├── handlers
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
└── prepare
    ├── files
    ├── handlers
    ├── tasks
    │   └── main.yaml
    ├── templates
    └── vars
 8. 执行效果[root@node1 ~]
 浏览器访问测试
  
 连接数据库
  
 最终效果图
 
 |