标签:自动化运维 ansible playbook saltstack pssh
前面写过了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=my.cnf.j2 dest=/etc/my.cnf notify: - restart mysql - name: Start Mysql Service service: name=mysqld state=started enabled=yes [root@ansible wordpress-nginx]# cat roles/mysql/templates/my.cnf.j2 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 port={{ mysql_port }} [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
我们来运行下这个配置
[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
标签:自动化运维 ansible playbook saltstack pssh
原文地址:http://huaxin.blog.51cto.com/903026/1845145