ansible【自动化配置】(thirty day)

回顾

1、mysql和python

(1)不需要执行mysql_ssl_rsa_setup

(2)Change_master_to.不需要get public key

2、可以使用pymysql非交互的管理mysql

(1)conn=pymysql.connect(host,user,password,database,prot)

(2)cursor=conn.cursor();

(3)Cursor.execute("create user ....")
(4)cursor.execute("grant 权限 on 库名.表名 to 用户")
(5) conn.commit()
(6)cursor.fetchall()

3、mycat中间件

(1)独属于mysql主从的负载均衡策略
(2)配置写主读从
(3)步骤
        ①安装jdk

tar -xf jdk_8u192.tar.gz
cp jdk/ /usr/local/jdk

sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile
sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile

java --version
javac --version

        ②mycat

tar -xf MyCat.tar.gz
cp -r mycat/ /usr/local/

#测试启动
/usr/local/mycat/bin/mycat console

        ③配置

                server.xml
                scheml.xml

4.启动和调试

(1)/usr/local/mycat/bin/mycat start
(2)Netstat -nput|grep 8066
(3)mysql -hmycat的ip或者域名-P8066 -userver.xml中填入账号 -p在service.xml中填入的密码
(4)Cat /usr/local/mysql/logs/wrapper.log
               Cause by.......

 一、自动化配置(anslible)

公司的服务器越来越多,维护一些简单的事情都会变得很繁琐。用shel脚本来管理少量服务器效率还行,服务器多了之后,shell脚本无法实现高效率运维。这种情况下,我们需要引入自动化运维工具,对多台服务器实现高效运维。

 1、管理内容的主要分类

文件目录管理(包括文件的创建,删除,修改,查看状态,远程拷贝等)
**用户和组管理*
cron时间任务管理I
yum源配置与通过yum管理软件包
服务管理
远程执行脚本
远程执行命令

2、常见的开源自动化运维工具比较

(1)puppet(拓展)

基于ruby语言,成熟稳定。适合于大型架构,相对于ansible和saltstack会复杂些。

(2)saltstack(拓展)

基于python语言。相对简单,大并发能力比ansible要好,需要维护被管理端的服务。如果服务断开,连接就会出问题。

(3)ansible

基于python语言。简单快捷,被管理端不需要启服务。直接走ssh协议,需要验证所以机器多的话速度会较慢。

二、搭建自动化部署

环境配置:

[root@M0 ~]# yum list installed | grep epel
[root@M0 ~]# yum -y install epel-release
[root@M0 ~]# yum -y install ansible
[root@M0 ~]# find /etc/ -name "*ansible*"
/etc/ansible
/etc/ansible/ansible.cfg

[root@M0 ~]# ssh-copy-id -i 192.168.1.51

[root@M0 ~]# ssh-keygen
[root@M0 ~]# ls ./.ssh/
id_rsa  id_rsa.pub
[root@M0 ~]# cat ./.ssh/id_rsa.pub
[root@M0 ~]# ssh-copy-id -i 192.168.1.51

[root@M0 ~]# ssh-copy-id -i 192.168.1.52

[root@M0 ~]# vim /etc/ansible/hosts 

[group01]
192.168.1.51
192.168.1.52

[group02]
192.168.1.51
192.168.1.52
192.168.1.53

[root@M0 ~]# ansible group01 -m ping

[root@M0 ~]# ansible group02 -m ping

[root@M0 ~]# vim /etc/ansible/hosts 

[group01]
192.168.1.51
192.168.1.52

other ansible_ssh_host=192.168.1.53 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1

[group02]
192.168.1.51
192.168.1.52
other

 [root@M0 ~]# ansible group02 -m ping

(1)ansible模块

[root@m0 ~]# ansible-doc -l     #查看所有支持的模块

(2)hostname 模块

用于修改主机名称
[root@M0 ~]# ansible group02 -m hostname -a 'name=ab.lxe.er'

 #将group02组中的主机名称分别改为ab lxe er

(3)file模块(重点)

file模块用于对文件相关的操作(创建,删除,软硬钟接等)

①创建目录和文件
[root@M0 ~]# ansible group01 -m file -a 'path=/tmp/abc state=directory'    #创建目录
[root@S0 ~]# ls /tmp/
abc
[root@S1 ~]# ls /tmp/
abc
[root@S2 ~]# ls /tmp
abc

[root@M0 ~]# ansible group02 -m file -a 'path=/tmp/abc/def state=touch'  #创建文件

[root@S0 ~]# ll /tmp/abc/
总用量 0
-rw-r--r-- 1 root root 0 8月  16 14:08 def

②递归修改(owner group mode

[root@M0 ~]# ansible group01 -m file -a 'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777'      #权限是777

[root@S0 ~]# ll /tmp/abc/
总用量 0
-rwxrwxrwt 1 bin daemon 0 8月  16 14:08 def

③删除目录(连同目录里的所有文件)

[root@M0 ~]# ansible group01 -m file -a 'path=/tmp/abc state=absent'

④创建文件并指定owner,group,mode等

[root@M0 ~]# ansible group01 -m file -a 'path=/tmp/xiaojiang state=touch owner=bin group=daemon mode=1777'

⑤删除文件

[root@M0 ~]# ansible group01 -m file -a 'path=/tmp/xiaojiang state=absent'
⑥创建软连接文件

[root@M0 ~]# ansible group01 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link'

[root@S0 ~]# ll /tmp/

lrwxrwxrwx  1 root root  10 8月  16 14:28 xxx -> /etc/fstab

⑦创建硬链接文件

[root@M0 ~]# ansible group01 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard'

[root@S0 ~]# ll /tmp/

-rw-r--r--. 2 root root 503 5月  26 21:16 xxx2

(硬链接创建文件,软连接指向硬链接)

(4)stat模块(了解)

[root@M0 ~]# ansible group02 -m stat -a 'path=/etc/fstab'

(5)copy模块(重点) 

[root@M0 ~]# mv mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz my57.tar.gz

[root@M0 ~]# ansible group02 -m copy -a 'src=./my57.tar.gz dest=~'
使⽤content参数直接往远程⽂件⾥写内容(会覆盖原内容)

[root@M0 ~]# ansible group1 -m copy -a 'content="ha ha\n" dest=/tmp/333'

注意:ansible中-a后⾯的参数⾥也有引号时,记得要单引双引交叉 使⽤,如果都为双引会出现问题

使⽤force参数控制是否强制覆盖

如果⽬标⽂件已经存在,则不覆盖

[root@M0 ~]# ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333 force=no'

如果⽬标⽂件已经存在,则会强制覆盖

[root@M0 ~]# ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333 force=yes'

使⽤backup参数控制是否备份⽂件

backup=yes表示如果拷⻉的⽂件内容与原内容不⼀样,则会备份⼀ 份 group1的机器上会将/tmp/333备份⼀份(备份⽂件命名加上时 间),再远程拷⻉新的⽂件为/tmp/333

[root@M0 ~]# ansible group1 -m copy -a 'src=/etc/fstab dest=/tmp/333 backup=yes owner=daemon group=daemon mode=1777'

copy模块拷⻉时要注意拷⻉⽬录后⾯是否带"/"符号

/etc/yum.repos.d后⾯不带/符号,则表示

把/etc/yum.repos.d整个⽬录拷⻉到/tmp/⽬录下

[root@M0 ~]# ansible group1 -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'

/etc/yum.repos.d/后⾯带/符号,则表示

把/etc/yum.repos.d/⽬录⾥的所有⽂件拷⻉到/tmp/⽬录下

[root@M0 ~]# ansible group1 -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'

(6)fetch模块 

(7)user模块

[root@M0 ~]# ansible group02 -m user -a 'name=aaa state=present'

[root@M0 ~]# ansible group02 -m file -a 'path=/user/local/mysql state=directory'
[root@M0 ~]# ansible group02 -m file -a 'path=/user/local/mysql/mysql-files state=directory owner=mysql group=mysql mode=750'

  • 22
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值