第12周-2022-02-25

1、主从复制及主主复制的实现

  • 主从复制:
    主节点配置:
#启用二进制日志:
[root@centos01 ~]# vi /etc/my.cnf
[mysqld]
server_id=147
log_bin=/data/mysql/logbin/mysql-bin

[root@centos01 ~]# mkdir -p /data/mysql/logbin
[root@centos01 ~]# chown -R mysql.mysql /data/mysql/
[root@centos01 ~]# systemctl restart mysqld

#登录mysql后,查看二进制位置:
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |       157 | No        |
+------------------+-----------+-----------+
1 row in set (0.01 sec)
#创建主从复制账号
mysql> create user repluser@'192.168.184.%' identified with mysql_native_password by 'Zhrmghg@2022';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to repluser@'192.168.184.%';
Query OK, 0 rows affected (0.00 sec)

从节点配置:

[root@centos01 ~]# vi /etc/my.cnf
[mysqld]
server_id=129
read-only
log_bin=/data/mysql/logbin/mysql-bin

[root@centos01 ~]# mkdir -p /data/mysql/logbin
[root@centos01 ~]# chown -R mysql.mysql /data/mysql/
[root@centos01 ~]# systemctl restart mysqld

#配置主从复制:
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.184.147',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='Zhrmghg@2022',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=157;
Query OK, 0 rows affected, 9 warnings (0.02 sec)

#开启io线程和sql线程:
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

#查看主从复制状态:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.184.147
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1022
               Relay_Log_File: centos02-relay-bin.000004
                Relay_Log_Pos: 1191
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1022
              Relay_Log_Space: 1764
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 147
                  Master_UUID: 723f7726-93c5-11ec-9d2e-000c294d790f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

验证:

#主节点创建数据库:
mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)

#从节点查看已经同步:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.01 sec)
  • 主主复制:
    master1和master2:
#master1启用二进制日志:
[root@centos01 ~]# vi /etc/my.cnf
[mysqld]
server_id=147
log_bin=/data/mysql/logbin/mysql-bin

[root@centos01 ~]# mkdir -p /data/mysql/logbin
[root@centos01 ~]# chown -R mysql.mysql /data/mysql/
[root@centos01 ~]# systemctl restart mysqld

#master2启用二进制日志:
[root@centos02 ~]# vi /etc/my.cnf
[mysqld]
server_id=129
log_bin=/data/mysql/logbin/mysql-bin

[root@centos02 ~]# mkdir -p /data/mysql/logbin
[root@centos02 ~]# chown -R mysql.mysql /data/mysql/
[root@centos02 ~]# systemctl restart mysqld

master1:

#记录master1的二进制位置:
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |       157 | No        |
+------------------+-----------+-----------+
1 row in set (0.00 sec)

#创建账号并授权
mysql> create user repluser@'192.168.184.%' identified with mysql_native_password by 'Zhrmghg@2022';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to repluser@'192.168.184.%';
Query OK, 0 rows affected (0.01 sec)

master2:

#配置单向复制:
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.184.147',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='Zhrmghg@2022',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=157;
Query OK, 0 rows affected, 9 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.184.147
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1257
               Relay_Log_File: centos02-relay-bin.000002
                Relay_Log_Pos: 1426
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1257
              Relay_Log_Space: 1639
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 147
                  Master_UUID: edd36952-944d-11ec-a11b-000c294d790f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)
#至此,master1-->master2单向复制完成。

master2:

#继续记录master2的二进制日志位置:
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |      1285 | No        |
+------------------+-----------+-----------+
1 row in set (0.00 sec)

master1:

#配置单向复制:
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.184.129',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='Zhrmghg@2022',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=1285;
Query OK, 0 rows affected, 9 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.184.129
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1285
               Relay_Log_File: centos01-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1285
              Relay_Log_Space: 539
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 129
                  Master_UUID: f0727b50-9451-11ec-8a1d-000c29b5218f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)
#至此,master2-->master1单向复制完成,双向复制全部完成。

验证:

#验证master1-->master2同步
#master1:
mysql> create database testdb1;
Query OK, 1 row affected (0.00 sec)
#master2:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb1            |
+--------------------+
5 rows in set (0.00 sec)

#验证master2-->master1同步
#master2:
mysql> create database testdb2;
Query OK, 1 row affected (0.00 sec)
#master1:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb1            |
| testdb2            |
+--------------------+
6 rows in set (0.00 sec)

2、xtrabackup实现全量+增量+binlog恢复库

原数据库备份:

#下载安装xtrabackup(原备份主机和目标恢复主机都需要安装)
[root@centos01 ~]# wget https://downloads.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0.27-19/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.27-19.1.el7.x86_64.rpm
[root@centos01 ~]# yum install -y percona-xtrabackup-80-8.0.27-19.1.el7.x86_64.rpm

#导入测试数据后,完全备份
[root@centos01 ~]# mkdir /backup
[root@centos01 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/base

#第一次插入数据
mysql> insert students values (null,'xiao ming',20,'M',4,4);
Query OK, 1 row affected (0.01 sec)
#第一次增量备份
[root@centos01 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base

#第二次插入数据
mysql> insert students values (null,"xiao hong",19,"F",2,1);
Query OK, 1 row affected (0.00 sec)
#第二次增量备份
[root@centos01 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1

#第三次插入数据
mysql> insert students values (null,"xiao zhao",23,"F",1,3);
Query OK, 1 row affected (0.01 sec)

将备份拷贝到目标主机:
scp -r /backup/* 目标主机:/backup/

假设原数据库服务器此时出现故障,但binlog日志未损坏

目标数据库恢复:

#停止mysql服务
[root@centos02 data]# systemctl stop mysqld
#清空mysql数据目录、二进制目录
[root@centos02 ~]# rm -rf /data/mysql/*
[root@centos02 data]# rm -rf logbin/*

#恢复
[root@centos02 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base

#合并第一次增量备份到完全备份
[root@centos02 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1

#合并第二次增量备份到完全备份,最后一次还原不需要加选项--apply-log-only
[root@centos02 ~]# xtrabackup --prepare --target-dir=/backup/base --incremental-dir=/backup/inc2

#复制到数据库目录
[root@centos02 data]# xtrabackup --copy-back --target-dir=/backup/base

#恢复属性
[root@centos02 data]# chown -R mysql:mysql /data/logbin/ /data/mysql/

#启动mysql
[root@centos02 data]# systemctl start mysqld

#登录恢复后的数据查看数据
mysql> select * from students;
...省略...
|    26 | xiao ming     |  20 | M      |       4 |         4 |
|    27 | xiao hong     |  19 | F      |       2 |         1 |
+-------+---------------+-----+--------+---------+-----------+
#还缺少xiao zhao的数据,需要使用binlog日志恢复

#继续查看binlog位置
mysql> show master logs
    -> ;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000005 |       155 | No        |
| mysql-bin.000006 |       155 | No        |
+------------------+-----------+-----------+
2 rows in set (0.01 sec)
#mysql-bin.000006为数据库启动后的日志,因此这里mysql-bin.000005的155为最后一次增量恢复完成的位置,即还需要通过binlog恢复在此之后的数据

#原数据库二进制日志从mysql-bin.000005的155后导出数据:
[root@centos01 logbin]# mysqlbinlog mysql-bin.000005 --start-position=155 > ~/binlog.sql

#拷贝到目标数据库
[root@centos01 logbin]# scp ~/binlog.sql 目标数据库:~
#目标数据库上导入binlog.sql
mysql> source ~/binlog.sql

#查看最终恢复后的数据
mysql> select * from students;
...省略...
|    26 | xiao ming     |  20 | M      |       4 |         4 |
|    27 | xiao hong     |  19 | F      |       2 |         1 |
|    28 | xiao zhao     |  23 | F      |       1 |         3 |
+-------+---------------+-----+--------+---------+-----------+
28 rows in set (0.00 sec)

至此,3条数据全部恢复

3、MyCAT实现MySQL读写分离

环境:
192.168.184.147 master
192.168.184.129 slave
192.168.184.134 mycat

mycat服务器:

#安装jdk
[root@centos03 ~]# java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)

#下载mycat
wget http://dl.mycat.org.cn/1.6.7.6/20220221174943/Mycat-server-1.6.7.6-release-20220221174943-linux.tar.gz
#解压
[root@centos03 ~]# tar zxf Mycat-server-1.6.7.6-release-20220221174943-linux.tar.gz -C /usr/local/
#配置环境变量
[root@centos03 ~]# echo 'PATH=/usr/local/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@centos03 ~]# source /etc/profile.d/mycat.sh

#启动mycat
[root@centos03 ~]# mycat start
#查看日志,启动成功
[root@centos03 ~]# tail -f /usr/local/mycat/logs/wrapper.log 
STATUS | wrapper  | 2022/02/24 13:16:09 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2022/02/24 13:16:09 | Launching a JVM...
INFO   | jvm 1    | 2022/02/24 13:16:14 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2022/02/24 13:16:14 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2022/02/24 13:16:14 | 
INFO   | jvm 1    | 2022/02/24 13:16:16 | MyCAT Server startup successfully. see logs in logs/mycat.log

#客户端尝试使用默认密码123456连接mycat
[root@centos01 ~]# mysql -uroot -p123456 -h192.168.184.134 -P8066

#在mycat 服务器上修改server.xml文件配置Mycat的连接信息
[root@centos03 ~]# vim /usr/local/mycat/conf/server.xml
...
#取消该段注释,并将8066改为3306端口
<property name="serverPort">3306</property>
<property name="managerPort">9066</property>
<property name="idleTimeout">300000</property>
<property name="authTimeout">15000</property>
<property name="bindIp">0.0.0.0</property>
<property name="dataNodeIdleCheckPeriod">300000</property> #此处删除5 * 60 * 1000L;
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property>
...
<user name="root" defaultAccount="true"> #此处可修改用户名
<property name="password">123456</property> #此处可修改密码
<property name="schemas">TESTDB</property> #数据库名要和schema.xml相对应

#修改schema.xml实现读写分离策略
[root@centos03 ~]# vi /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
  <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
  <dataNode name="dn1" dataHost="localhost1" database="hellodb" />
  <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
    writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
  <heartbeat>select user()</heartbeat>
  <writeHost host="host1" url="192.168.184.147:3306" user="root" password="Zhrmghg@2022">
  <readHost host="host2" url="192.168.184.129:3306" user="root" password="Zhrmghg@2022" />
  </writeHost>
  </dataHost>
</mycat:schema>

#重启mycat
[root@centos03 conf]# mycat restart

#在数据库主节点上创建用户并对mycat授权
mysql> create user root@'192.168.184.%' identified with mysql_native_password by 'Zhrmghg@2022';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to root@'192.168.184.%';
Query OK, 0 rows affected (0.00 sec)

验证:

#测试读
[root@centos01 ~]# mysql -uroot -p123456 -h 192.168.184.134
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.01 sec)

mysql> use TESTDB
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 @@server_id;
+-------------+
| @@server_id |
+-------------+
|         129 |
+-------------+
1 row in set (0.00 sec)

#主从节点开通用日志
mysql> show variables like 'general%';
mysql> set global general_log=1;

#测试从节点读
mysql> select * from students;
[root@centos02 ~]# tail -f /var/lib/mysql/centos02.log
...
2022-02-24T07:23:08.489851Z   148 Query select * from students
#测试主节点写
mysql> insert students values (null,"li si",22,"F",4,2);
[root@centos01 ~]# tail -f /var/lib/mysql/centos01.log
...
2022-02-24T07:26:32.299624Z   234 Query insert students values (null,"li si",22,"F",4,2)

#停止从节点(模拟从节点宕机)
[root@centos01 ~]# systemctl stop mysqld
#读已切换为主节点
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         147 |
+-------------+
1 row in set (0.00 sec)

4、ansible常用模块介绍

  • command模块
    功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项
    范列:ansible websrvs -m command -a 'chdir=/data ls -l'
  • shell模块
    功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, >
  • script模块:
    功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
    范列:ansible websrvs -m script -a '/root/test.sh'
  • copy模块:
    功能:从ansible服务器主控端复制文件到远程主机
    范列:ansible websrvs -m copy -a 'src=ssh_key.sh dest=/data/ssh.sh owner=root group=bin mode=700'
    注意:复制目录不加“/”
    范列:ansible websrvs -m copy -a 'src=/etc dest=/data'
    范列:指定内容,直接生成文件
    ansible websrvs -m copy -a "content='test line1\ntest line2\n' dest=/tmp/test.txt"
  • get_url模块:
    功能: 用于将文件从http、https或ftp下载到被管理机节点上
    ansible websrvs -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/nginx.tar.gz checksum="md5:b2d33d24d89b8b1f87ff5d251aa27eb8"'
  • fetch模块:
    功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录
    范列:ansible websrvs -m fetch -a 'src=/var/log/messages dest=/data/log'
  • file模块:
    功能: 用于将文件从http、https或ftp下载到被管理机节点上
    范列:创建空文件:
    ansible websrvs -m file -a 'path=/data/a.txt state=touch owner=root'
    范列:创建文件夹:
    ansible websrvs -m file -a 'path=/data/mysql state=directory'
    范列:创建软链接:
    ansible websrvs -m file -a 'src=/data/mysql-5.7 path=/data/mysql state=link'
    范列:递归修改目录属性,但不递归至子目录
    ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
    范列:递归修改目录及子目录的属性
    ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql recurse=yes"
    范列:删除目录
    ansible websrvs -m file -a 'path=/data/mysql-5.7 state=absent'
  • stat模块:
    功能:检查文件或文件系统的状态
    范列:ansible 127.0.0.1 -m stat -a 'path=/etc/passwd'
  • unarchive模块:
    功能:解包解压缩
    实现有两种用法:
    1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes,此为默认值,可省略
    2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
    范列:
    ansible all -m unarchive -a 'src=/data/nginx-1.18.0.tar.gz dest=/usr/local/src owner=wang group=bin'
    ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
    ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
  • archive模块:
    功能:打包压缩保存在被管理节点
    范列:ansible websrvs -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=root mode=0600'
  • hostname模块:
    功能:管理主机名
    范列:ansible 10.0.0.7 -m hostname -a 'name=test.magedu.org'
  • cron模块:
    功能:计划任务
    范列:
    ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'
    ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime"
  • yum和apt模块:
    功能:软件包管理
    范列:
    ansible 10.0.0.100 -m apt -a 'name=sl'
    ansible 10.0.0.100 -m yum -a 'name=httpd state=absent'
    ansible 10.0.0.100 -m yum -a 'name=httpd,vsftpd'
  • yum_repository模块:
    功能:yum仓库管理
    范列:
    - name: Add multiple repositories into the same file
    yum_repository:
    name: epel
    description: EPEL YUM repo
    file: external_repos
    baseurl: https://download.fedoraproject.org/pub/epel/releasever/basearch/
    gpgcheck: no
  • service模块:
    功能:管理服务
    范列:
    ansible 10.0.0.8 -m service -a 'name=httpd state=started enabled=yes'
    ansible 10.0.0.8 -m service -a 'name=httpd state=stopped enabled=no'
  • user模块:
    功能:管理用户
    范列:
    ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
    ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'
  • group模块:
    功能:管理组
    范列:
    ansible websrvs -m group -a 'name=nginx gid=88 system=yes'
    ansible websrvs -m group -a 'name=nginx state=absent'
  • lineinfile模块:
    regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被 删除。 如果想进行多行匹配进行替换需要使用replace模块
    范列:
    ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'"
    ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"'
  • replace模块:
    功能:该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
    ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.)' replace='#\1'"
    ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.
    )' replace='\1'"
  • setup模块:
    功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度
    范列:
    ansible all -m setup
    ansible all -m setup -a 'filter=ansible_python_version'
  • debug模块:
    功能:可以用于输出信息,并且通过 msg 定制输出的信息内容
    注意: msg后面的变量有时需要加 " " 引起来
    范列:
    ansible 10.0.0.18 -m debug
    10.0.0.18 | SUCCESS => {
    "msg": "Hello world!"
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,258评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,335评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,225评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,126评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,140评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,098评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,018评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,857评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,298评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,518评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,400评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,993评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,638评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,661评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容

  • day-37 1、基础端口 873 rsync 22 ssh 25 smtp 邮件发送服务 110 pop3 邮件...
    路边大呲花阅读 334评论 0 0
  • ansible关闭ssh首次连接时yes/no提示 使用ssh连接时,可以使用-o参数将StrictHostKey...
    张鑫泽_2109阅读 246评论 0 0
  • 1.ansible批量管理服务部署1.管理端服务器a.安装软件yum install -y ansible(要ep...
    噬魂老妖阅读 266评论 0 0
  • Day40 课堂笔记 2019年4月25日 playbook剧本介绍 playbook 什么是playbook? ...
    深渊下的一抹阳光阅读 211评论 0 0
  • SSH批量管理项目如何一键一秒钟完成:一秒完成。 1.ssh-keygen非交互式创建秘钥对: 具体命令:ssh-...
    将就灬阅读 152评论 0 1