首页 > 代码库 > ansible应用进阶playbook--LNMP+WordPress
ansible应用进阶playbook--LNMP+WordPress
前面写过了ansible常用的模块介绍,熟悉这些模块是基础,接下来我们通过一个实例来介绍playbook,planbook通过灵活的组装这些基础模块组件,可以实现各位复杂的业务操作场景
我们先来说明下playbook几个常用的概念
var:ansible里变量的概念就是重用某些定义值,主要是模板会用到
tasks:任务就是按照配置文件的定义去执行里面的操作
handles:当被操作的主机配置发送改变时要进行的操作,这里需要通知他,然后handles定义的动作会被执行,是task列表,这些task与前述的tasks并没有本质上的不同
roles:role在ansible可以将任务等模块化了,使他们复用性更强
下面我们通过一键安装LNMP环境并且部署WordPress博客来学习下(github上面的,里面的内容我们稍微修改下就行了)
[root@ansible wordpress-nginx]# tree . ├── group_vars │ ├── all │ └── all.0 ├── hosts ├── LICENSE.md ├── README.md ├── roles │ ├── common │ │ ├── files │ │ │ ├── epel.repo │ │ │ ├── iptables-save │ │ │ └── RPM-GPG-KEY-EPEL-6 │ │ ├── handlers │ │ │ └── main.yml │ │ └── tasks │ │ └── main.yml │ ├── mysql │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ └── my.cnf.j2 │ ├── nginx │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ └── default.conf │ ├── php-fpm │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ └── wordpress.conf │ └── wordpress │ ├── tasks │ │ └── main.yml │ └── templates │ └── wp-config.php ├── site.retry └── site.yml 21 directories, 23 files 总的目录结构在上面,site.yml是总的入口 下面我们简单看下mysql部分,其他的基本都差不多 [root@ansible wordpress-nginx]# cat site.yml - name: Install WordPress, MySQL, Nginx, and PHP-FPM hosts: wordpress remote_user: root roles: - common - mysql - nginx - php-fpm - wordpress [root@ansible wordpress-nginx]# cat hosts [wordpress] 172.16.80.127 [root@ansible wordpress-nginx]# cat roles/mysql/handlers/main.yml --- - name: restart mysql service: name=mysqld state=restarted [root@ansible wordpress-nginx]# cat roles/mysql/tasks/main.yml --- - name: Install Mysql package yum: name={{ item }} state=present with_items: - mysql-server - MySQL-python - libsemanage-python # with_items: 代表下面的列出的变量都会传到item里面 - name: Create Mysql configuration file template: src=http://www.mamicode.com/my.cnf.j2 dest=/etc/my.cnf>我们来运行下这个配置
[root@ansible wordpress-nginx]# ansible-playbook -i hosts site.yml [root@ansible wordpress-nginx]# ansible-playbook -i hosts site.yml PLAY [Install WordPress, MySQL, Nginx, and PHP-FPM] **************************** TASK [setup] ******************************************************************* ok: [172.16.80.127] TASK [common : Copy the EPEL repository definition] **************************** ok: [172.16.80.127] TASK [common : Create the GPG key for EPEL] ************************************ ok: [172.16.80.127] TASK [common : Set up iptables rules] ****************************************** ok: [172.16.80.127] TASK [mysql : Install Mysql package] ******************************************* ok: [172.16.80.127] => (item=[u‘mysql-server‘, u‘MySQL-python‘, u‘libsemanage-python‘]) TASK [mysql : Create Mysql configuration file] ********************************* ok: [172.16.80.127] TASK [mysql : Start Mysql Service] ********************************************* ok: [172.16.80.127] TASK [nginx : Install nginx] *************************************************** ok: [172.16.80.127] TASK [nginx : Copy nginx configuration for wordpress] ************************** changed: [172.16.80.127] TASK [php-fpm : Install php-fpm and deps] ************************************** ok: [172.16.80.127] => (item=[u‘php‘, u‘php-fpm‘, u‘php-enchant‘, u‘php-IDNA_Convert‘, u‘php-mbstring‘, u‘php-mysql‘, u‘php-PHPMailer‘, u‘php-process‘, u‘php-simplepie‘, u‘php-xml‘]) TASK [php-fpm : Disable default pool] ****************************************** ok: [172.16.80.127] TASK [php-fpm : Copy php-fpm configuration] ************************************ ok: [172.16.80.127] TASK [wordpress : Download WordPress] ****************************************** changed: [172.16.80.127] TASK [wordpress : Extract archive] ********************************************* changed: [172.16.80.127] [WARNING]: Consider using unarchive module rather than running tar TASK [wordpress : Add group "wordpress"] *************************************** changed: [172.16.80.127] TASK [wordpress : Add user "wordpress"] **************************************** changed: [172.16.80.127] TASK [wordpress : Fetch random salts for WordPress config] ********************* changed: [172.16.80.127 -> localhost] [WARNING]: Consider using get_url or uri module rather than running curl TASK [wordpress : Create WordPress database] *********************************** changed: [172.16.80.127] TASK [wordpress : Create WordPress database user] ****************************** changed: [172.16.80.127] TASK [wordpress : Copy WordPress config file] ********************************** changed: [172.16.80.127] TASK [wordpress : Change ownership of WordPress installation] ****************** changed: [172.16.80.127] TASK [wordpress : Start php-fpm Service] *************************************** changed: [172.16.80.127] RUNNING HANDLER [nginx : restart nginx] **************************************** changed: [172.16.80.127] PLAY RECAP ********************************************************************* 172.16.80.127 : ok=23 changed=12 unreachable=0 failed=0
截图看看
最后打开浏览器测试访问http://172.16.80.127
可以看到它自动跳转到安装界面http://172.16.80.127/wp-admin/install.php
本文出自 “厚德载物” 博客,谢绝转载!
ansible应用进阶playbook--LNMP+WordPress
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。