整个role目录的结构:
.
├── ansible.cfg
├── hosts
├── httpd-php_roles.retry
├── httpd-php_roles.yml
├── roles
│ ├── httpd
│ │ ├── files
│ │ │ ├── mysql_wp.sh
│ │ │ ├── wordpress-4.7.4-zh_CN.tar.gz
│ │ │ ├── wordpress.conf
│ │ │ └── wp-config.php
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── vars
│ ├── mariadb
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templetes
│ │ └── vars
│ └── php
│ ├── defaults
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── tmpletes
│ └── vars
└── wp-config.php
1.定义hosts目标主机:
vim /etc/ansible/hosts [httpd] 192.168.5.111
2.创建httpd相关的目录:
mkdir httpd/{tasks,vars,files} -pv
3.进入tasks目录创建并编辑main.yml
--- - name: install httpd yum: name=httpd state=present - name: conf httpd copy: src=wordpress.conf dest=/etc/httpd/conf.d/ - name: file wordpress copy: src=wordpress-4.7.4-zh_CN.tar.gz dest=/var/www/html - name: tar wordpress.tar.gz shell: "tar -xf wordpress-4.7.4-zh_CN.tar.gz && chown -R apache.apache wordpress && mv wordpress-4.7.4-zh_CN.tar.gz /usr/local/src" args: chdir: /var/www/html notify: start httpd #当上面的执行完成后会触发handler定义的同名的动作 - name: conf wp-config.php copy: src=wp-config.php dest=/var/www/html/wordpress - name: mysql_wp.sh script: mysql_wp.sh notify: restart httpd - name: restart httpd service: name=httpd state=restarted
4.将所需的文件放入roles/httpd/files中,文件中的内容下面用到会提及
files/ ├── mysql_wp.sh ├── wordpress-4.7.4-zh_CN.tar.gz ├── wordpress.conf └── wp-config.php
5.在handlers中编辑main.yml定义触发的动作
--- - name: start httpd service: name=httpd state=started - name: restart httpd service: name=httpd state=restarted
6.httpd的已经完成,接下来定义php模块的内容,同样在roles中创建php相关目录:
mkdir php/{tmpletes,tasks,files,handlers,meta,defaults,vars} -pv
7.编辑tasks中的main.yml文件,这里只是安装了一个php模块:
--- - name: install php yum: name=php state=present
8.定义mariadb相关的内容的目录:
mkdir mariadb/{templetes,handlers,vars,files,defaults,tasks} -vp
9.编辑roles/mariadb/tasks/main.yml文件,顺带安装一下几个模块:
--- - name: install mariadb yum: name=mariadb-server state=present - name: install httpd-mysql yum: name=php-mysql state=present - name: install php-mbstring yum: name=php-mbstring state=present - name: restart mariadb service: name=mariadb state=restarted
10.wordpress程序需要手动更改一个配置文件,事先编辑好直接推送wordpress/目录下,这就是roles/httpd/files中的wp-conf.php,需要更改内容如下:
define(‘DB_NAME‘, ‘wordpress‘); /** MySQL数据库用户名 */ define(‘DB_USER‘, ‘wpuser‘); /** MySQL数据库密码 */ define(‘DB_PASSWORD‘, ‘wppd‘); /** MySQL主机 */ define(‘DB_HOST‘, ‘localhost‘);
11.上面定义的数据库需要数据库等需要自行创建,将其写脚本files/mysql_wp.sh文件:
#!/bin/bash # mysql -uroot -e " CREATE DATABASE IF NOT EXISTS wordpress CHARACTER SET ‘utf8‘; GRANT ALL ON wordpress.* TO ‘wpuser‘@‘localhost‘ IDENTIFIED BY ‘wppd‘; "
12.与roles同级创建一个yml文件,将这三个项目整合起来运行:
cd /etc/ansible vim httpd-php_roles.yml --- - hosts: httpd remote_user: root roles: - { role: php, tags: [ php-tag,ap-tag ] } - { role: mariadb, tags: [ mariadb-tag,ap-tag ] } - { role: httpd, tags: [ httpd-tag,ap-tag ] }
13.向定义的目标主机进行发布程序:
~]# ansible-playbook /etc/ansible/httpd-php_roles.yml
只要目标主机yum源可用,没有遇到报错基本OK.
访问验证一下:
本文出自 “老城小叙” 博客,请务必保留此出处http://cityx.blog.51cto.com/9857477/1945820
Ansible roles实现LAMP部署wordpress
原文地址:http://cityx.blog.51cto.com/9857477/1945820