集群运维自动化工具ansible使用playbook安装mysql

上次介绍了如何使用ansible playbook安装zabbix客户端(http://www.jb51.net/article/52158.htm),这次介绍一下如何使用playbook安装mysql。

下面是安装mysql的信息:

mysql_basedir: /data/mysql/basedir     源码目录
mysql_datadir: /data/mysql/datadir     数据目录
mysql_user: mysql          mysql用户
mysql_database_user: root        数据库用户
mysql_passwd: 'E4yR3WnoluSFTCBAI'      数据库密码
mysql_port: 3306          mysql监听端口
mysql_sock: /data/mysql/datadir/mysql.sock   mysql的sock
mysql_charset: utf8         mysql字符集
mysql_collation: utf8_general_ci      mysql排序方式
mysql_version: Percona-Server-5.5.21-rel25.1.tar.gz mysql版本

下面是安装mysql的playbook结构

10:24:06 # tree mysql_*
mysql_delete
├── files
├── handlers
├── meta
│ └── main.yml
├── tasks
│ ├── delete.yml
│ └── main.yml
├── templates
└── vars
 └── main.yml
mysql_install
├── files
│ └── mysql.tar.gz
├── handlers
├── meta
│ └── main.yml
├── tasks
│ ├── copy.yml
│ ├── delete.yml
│ ├── install.yml
│ └── main.yml
├── templates
│ ├── install_mysql.sh
│ ├── my.cnf
│ ├── mysqld
│ └── mysql_security.sh
└── vars
 └── main.yml

12 directories, 15 files

playbook的mysql安装的是

10:32:06 # cat mysql_install.yml
---
- hosts: "{{host}}"
 remote_user: "{{user}}"
 gather_facts: True
 roles:
 - common
 - mysql_install

playbook的mysql协助的是

10:32:44 # cat mysql_delete.yml
---
- hosts: "{{host}}"
 remote_user: "{{user}}"
 gather_facts: True
 roles:
 - mysql_delete

由于在上篇安装zabbix客户端已经介绍了结构,所以现在不介绍了。

10:26:00 # time ansible-playbook mysql_install.yml --extra-vars "host=192.168.240.17 user=root" --private-key=/root/test.pem 

PLAY [192.168.240.17] ********************************************************* 

GATHERING FACTS ***************************************************************
ok: [192.168.240.17]

TASK: [common | Install initializtion require software] ***********************
changed: [192.168.240.17]

TASK: [mysql_install | Copy Mysql Software To Redhat Client] ******************
changed: [192.168.240.17]

TASK: [mysql_install | Create Mysql User In Redhat Client] ********************
changed: [192.168.240.17]

TASK: [mysql_install | Copy Mysql Start Script To Redhat Client] **************
changed: [192.168.240.17]

TASK: [mysql_install | Copy Install Mysql Script To Redhat Client] ***********
changed: [192.168.240.17]

TASK: [mysql_install | Copy Mysql Config To Redhat Client] ********************
changed: [192.168.240.17]

TASK: [mysql_install | Copy Mysql Security Script To Redhat Client] ***********
changed: [192.168.240.17]

TASK: [mysql_install | Create Mysql Install Dir] ******************************
ok: [192.168.240.17]

TASK: [mysql_install | Uncompression Mysql Software To Redhat Client] *********
changed: [192.168.240.17]

TASK: [mysql_install | Modify Mysql Dir Permission In Redhat Client] **********
ok: [192.168.240.17] => (item=/data/mysql/datadir)
ok: [192.168.240.17] => (item=/data/mysql/basedir)

TASK: [mysql_install | Install Mysql Script In Redhat Client] *****************
changed: [192.168.240.17]

TASK: [mysql_install | Start Myql Security Script In Redhat Client] ***********
changed: [192.168.240.17]

TASK: [mysql_install | Add Boot Start Mysql Service In Redhat Client] *********
changed: [192.168.240.17]

TASK: [mysql_install | Delete Mysql compression Software In Redhat Client] ****
changed: [192.168.240.17]

PLAY RECAP ********************************************************************
192.168.240.17    : ok=15 changed=12 unreachable=0 failed=0 

real 2m1.596s
user 0m8.815s
sys 0m0.848s

2、安装后测试

登陆192.168.240.17的测试机查看mysql安装情况

[root@ip-10-10-240-21 tmp]# ll
total 16
lrwxrwxrwx 1 root root 30 Jul 13 22:27 mysql.sock -> /data/mysql/datadir/mysql.sock
-rw------- 1 root root 256 Jul 10 06:07 tmp.0PLkgCq81n
-rw------- 1 root root 197 Jul 9 05:35 yum_save_tx-2014-07-09-09-35ibcBiO.yumtx
-rw-rw-r-- 1 zabbix zabbix 320 Jul 10 21:39 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix 5 Jul 10 21:39 zabbix_agentd.pid
[root@ip-10-10-240-21 tmp]# ps -ef|grep mysql
root  21333  1 0 22:27 ?  00:00:00 /bin/sh /data/mysql/basedir/bin/mysqld_safe --defaults-file=/data/mysql/datadir/my.cnf --datadir=/data/mysql/datadir --pid-file=/data/mysql/datadir/mysql.pid
mysql 22156 21333 2 22:27 ?  00:00:08 /data/mysql/basedir/bin/mysqld --defaults-file=/data/mysql/datadir/my.cnf --basedir=/data/mysql/basedir --datadir=/data/mysql/datadir --plugin-dir=/data/mysql/basedir/lib/plugin --user=mysql --log-error=/data/mysql/datadir/mysql-error.log --open-files-limit=10240 --pid-file=/data/mysql/datadir/mysql.pid --socket=/data/mysql/datadir/mysql.sock --port=3306
root  22440 20233 0 22:33 pts/0 00:00:00 grep mysql
[root@ip-10-10-240-21 tmp]# ll /data/mysql/
total 8
drwxr-xr-x 13 mysql mysql 4096 Jun 26 03:59 basedir
drwxr-xr-x 4 mysql mysql 4096 Jul 13 22:28 datadir
[root@ip-10-10-240-21 tmp]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10905 Jul 13 22:27 /etc/init.d/mysqld
[root@ip-10-10-240-21 tmp]# /etc/init.d/mysqld status
MySQL (Percona Server) running (22156)      [ OK ]
[root@ip-10-10-240-21 tmp]# chkconfig --list|grep mysql
mysqld   0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@ip-10-10-240-21 tmp]# mysql -h 192.168.240.17 -u root -pE4yR3WnoluSFTCBAI
ERROR 1045 (28000): Access denied for user 'root'@'192.168.240.17' (using password: YES)
[root@ip-10-10-240-21 tmp]# mysql -h 10.10.240.21 -u root -pE4yR3WnoluSFTCBAI
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.21-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| mysql    |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user,password from user;
+--------------+--------+-------------------------------------------+
| host   | user | password         |
+--------------+--------+-------------------------------------------+
| 10.10.240.21 | root | *BE78618CBAFFF409CE17D81579C1678B94439BE1 |
| %   | zabbix | *DEEF4D7D88CD046ECA02A80393B7780A63E7E789 |
+--------------+--------+-------------------------------------------+
2 rows in set (0.00 sec)

可以看到已经按照我的要求安装完成,下面在进行删除测试。

3、删除

10:49:18 # time ansible-playbook mysql_delete.yml --extra-vars "host=192.168.240.17 user=root" --private-key=/root/test.pem 

PLAY [192.168.240.17] ********************************************************* 

GATHERING FACTS ***************************************************************
ok: [192.168.240.17]

TASK: [mysql_delete | Stop Mysql Service] *************************************
changed: [192.168.240.17]

TASK: [mysql_delete | Delete Mysql Boot Start Script] *************************
changed: [192.168.240.17]

TASK: [mysql_delete | Delete Mysql Dir And Socket] ****************************
changed: [192.168.240.17]

TASK: [mysql_delete | Delete Mysql User] **************************************
changed: [192.168.240.17]

TASK: [mysql_delete | Delete Mysql Service Start Script] **********************
changed: [192.168.240.17]

PLAY RECAP ********************************************************************
192.168.240.17    : ok=6 changed=5 unreachable=0 failed=0 

real 0m25.248s
user 0m0.632s
sys 0m0.102s

4、删除后测试

[root@ip-10-10-240-21 tmp]# ll /tmp/
total 16
-rw------- 1 root root 256 Jul 10 06:07 tmp.0PLkgCq81n
-rw------- 1 root root 197 Jul 9 05:35 yum_save_tx-2014-07-09-09-35ibcBiO.yumtx
-rw-rw-r-- 1 zabbix zabbix 320 Jul 10 21:39 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix 5 Jul 10 21:39 zabbix_agentd.pid
[root@ip-10-10-240-21 tmp]# ll /data/
total 4
drwxr-xr-x 3 root root 4096 Jul 10 22:11 webroot
[root@ip-10-10-240-21 tmp]# ps -ef|grep mysql
root  22955 20233 0 22:50 pts/0 00:00:00 grep mysql
[root@ip-10-10-240-21 tmp]# ll /etc/init.d/mysql
ls: cannot access /etc/init.d/mysql: No such file or directory
[root@ip-10-10-240-21 tmp]# chkconfig --list|grep mysql
[root@ip-10-10-240-21 tmp]#

可以看到都删除完成。

如果大家想使用我的例子,可以从百度云的地址下载(文件大于50M,没办法放到附件或者51下载中心)地址是http://pan.baidu.com/s/1mgG8jY4,然后放到/etc/ansible目录里,下面是压缩包里的内容

11:20:08 # unzip -v mysql_install.zip
Archive: mysql_install.zip
 Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
  113 Defl:N  89 21% 07-14-2014 10:32 aaed0763 mysql_install.yml
  99 Defl:N  82 17% 07-14-2014 10:32 70c2a028 mysql_delete.yml
  0 Stored  0 0% 07-03-2014 17:29 00000000 roles/common/
  0 Stored  0 0% 06-30-2014 15:58 00000000 roles/common/meta/
  267 Defl:N  178 33% 06-30-2014 15:58 31ee20ec roles/common/meta/main.yml
  0 Stored  0 0% 07-11-2014 09:31 00000000 roles/common/tasks/
  586 Defl:N  268 54% 07-08-2014 10:18 1c0af2a1 roles/common/tasks/main.yml
  0 Stored  0 0% 06-19-2014 13:43 00000000 roles/common/handlers/
  58 Defl:N  48 17% 06-19-2014 13:43 8d058053 roles/common/handlers/main.yml
  0 Stored  0 0% 06-19-2014 13:30 00000000 roles/common/vars/
  0 Stored  0 0% 06-19-2014 13:30 00000000 roles/common/templates/
  0 Stored  0 0% 06-19-2014 13:30 00000000 roles/common/files/
  0 Stored  0 0% 06-24-2014 11:27 00000000 roles/mysql_install/
  0 Stored  0 0% 07-08-2014 14:12 00000000 roles/mysql_install/meta/
  198 Defl:N  149 25% 07-08-2014 14:12 b03e00eb roles/mysql_install/meta/main.yml
  0 Stored  0 0% 07-14-2014 09:27 00000000 roles/mysql_install/tasks/
  201 Defl:N  153 24% 07-14-2014 09:27 51de730b roles/mysql_install/tasks/delete.yml
  65 Defl:N  41 37% 07-14-2014 09:27 a3197ca7 roles/mysql_install/tasks/main.yml
 1281 Defl:N  329 74% 07-14-2014 09:27 73d60454 roles/mysql_install/tasks/copy.yml
 1162 Defl:N  375 68% 07-14-2014 09:27 e5a7341c roles/mysql_install/tasks/install.yml
  0 Stored  0 0% 06-19-2014 13:30 00000000 roles/mysql_install/handlers/
  0 Stored  0 0% 07-14-2014 09:51 00000000 roles/mysql_install/vars/
  313 Defl:N  177 44% 07-14-2014 09:51 bf4476a7 roles/mysql_install/vars/main.yml
  0 Stored  0 0% 07-14-2014 11:11 00000000 roles/mysql_install/templates/
  518 Defl:N  241 54% 07-14-2014 09:15 529052bf roles/mysql_install/templates/install_mysql.sh
 10905 Defl:N  3765 66% 06-26-2014 14:06 e35e7b82 roles/mysql_install/templates/mysqld
 2122 Defl:N  823 61% 07-14-2014 09:49 f91c74ab roles/mysql_install/templates/my.cnf
  794 Defl:N  275 65% 07-03-2014 13:15 560ee63e roles/mysql_install/templates/mysql_security.sh
  0 Stored  0 0% 06-26-2014 13:59 00000000 roles/mysql_install/files/
61805835 Defl:N 60413846 2% 06-26-2014 16:18 f61e317d roles/mysql_install/files/mysql.tar.gz
  0 Stored  0 0% 06-24-2014 11:27 00000000 roles/mysql_delete/
  0 Stored  0 0% 07-08-2014 14:12 00000000 roles/mysql_delete/meta/
  197 Defl:N  147 25% 07-08-2014 14:12 fe6b0ef8 roles/mysql_delete/meta/main.yml
  0 Stored  0 0% 07-14-2014 09:39 00000000 roles/mysql_delete/tasks/
  692 Defl:N  245 65% 07-14-2014 09:39 dccd57b4 roles/mysql_delete/tasks/delete.yml
  22 Stored  22 0% 06-27-2014 13:30 728ce4aa roles/mysql_delete/tasks/main.yml
  0 Stored  0 0% 06-19-2014 13:30 00000000 roles/mysql_delete/handlers/
  0 Stored  0 0% 07-03-2014 13:16 00000000 roles/mysql_delete/vars/
  313 Defl:N  177 44% 07-14-2014 10:56 bf4476a7 roles/mysql_delete/vars/main.yml
  0 Stored  0 0% 06-27-2014 13:30 00000000 roles/mysql_delete/templates/
  0 Stored  0 0% 06-27-2014 13:30 00000000 roles/mysql_delete/files/
--------   ------- ---       -------
61825741   60421430 2%       41 files
(0)

相关推荐

  • 集群运维自动化工具ansible之使用playbook安装zabbix客户端

    之前介绍了关于ansible的安装与使用(包括模块与playbook使用,地址是http://www.jb51.net/article/52154.htm),今天介绍一下如何使用playbook来部署zabbix客户端. ansible服务端的环境为centos 6.5 x86_64系统 ansible客户端环境为centos 6.3 x86_64系统 目前我的playbook只允许centos或redhat 6系列系统来安装zabbix客户端,并且客户端的版本是2.0.6. 下面是playbo

  • python中MySQLdb模块用法实例

    本文实例讲述了python中MySQLdb模块用法.分享给大家供大家参考.具体用法分析如下: MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接口,我们可以在python中连接MySQLdb来实现数据的各种操作. python连接mysql的方案有oursql.PyMySQL. myconnpy.MySQL Connector 等,不过本篇要说的确是另外一个类库MySQLdb,MySQLdb 是用于Python链接Mysql数据库的接口,它

  • Python下的Mysql模块MySQLdb安装详解

    默认情况下,MySQLdb包是没有安装的,不信? 看到类似下面的代码你就信了. 复制代码 代码如下: -bash-3.2# /usr/local/python2.7.3/bin/python get_cnblogs_news.py Traceback (most recent call last):  File "get_cnblogs_news.py", line 9, in <module>    import MySQLdbImportError: No module

  • linux 自动化运维工具ansible的使用详细教程

    一.ansible简介 1.ansible ansible是新出现的自动化运维工具,基于Python研发.糅合了众多老牌运维工具的优点实现了批量操作系统配置.批量程序的部署.批量运行命令等功能.仅需在管理工作站上安装ansible程序配置被管控主机的IP信息,被管控的主机无客户端.ansible应用程序存在于epel(第三方社区)源,依赖于很多python组件.主要包括: (1).连接插件connection plugins:负责和被监控端实现通信: (2).host inventory:指定操

  • ansible作为python模块库使用的方法实例

    前言 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能.ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架. 主要包括: (1).连接插件connection plugins:负责和被监控端实现通信: (2).host inventory:指定操作的主机,

  • Python subprocess模块学习总结

    一.subprocess以及常用的封装函数运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序.在Python中,我们通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序.subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用.另外subprocess还提供了一些管理标准流(standard stream)和管

  • Python利用ansible分发处理任务

    其实对python熟悉的人都可以自己用paramiko来写任务的分发系统,再结合gevent的协程就能实现异步的处理. 如果只想用工具的朋友可以使用一些工具,类似{puppet,saltstack,fabric,ansible,chef}等,其实这些工具的都是很好用的,不过于学习的成本,我建议大家使用ansible,这个模块封装的不错,功能也很齐全. 我们首先先安装ansible把 复制代码 代码如下: pip install ansible                          

  • 集群运维自动化工具ansible的安装与使用(包括模块与playbook使用)第1/2页

    我使用过puppet与salt,但这2个软件都需要安装客户端,并且更新很快,每次更新都是令人蛋疼的事,尤其是salt,喜欢他的命令功能,但bug太多,不敢在公司线上使用,puppet虽然稳定,但弄命令执行的时候,需要mco配置,非常麻烦,我公司由于跟多家公司合作,很多业务没办法安装客户端,所以没办法使用puppet与salt(虽然salt有ssh,但不太好使),最后找到了ansible,他既有命令执行也有配置管理,关键开发它的语言是python,paramiko进行ssh连接,跟我之前开发的自动

  • Python random模块(获取随机数)常用方法和使用例子

    random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成的随机数n: a <= n <= b.如果 a <b, 则 b <= n <= a 复制代码 代码如下: print random.uniform(10, 20)print rand

  • 集中化管理平台Ansible详解

    Ansible一种集成IT系统的配置管理,应用部署,执行特定任务的开源平台.Ansible具有如下特点: 部署简单,只需在主控制端部署Ansible环境,被控端无需做任何操作: 默认使用SSH协议对设备进行管理: 主从集中化管理: 配置简单,功能强大,扩展性强 通过Playbooks来定制强大的配置.状态管理; 一.Ansible安装 1.环境配置 角色 主机名 IP 组名 msster Automation 192.168.1.23 cleint test1 192.168.1.24 webs

随机推荐