s55.运维自运化之ANSIBLE(二)

1,466 阅读4分钟

“ 本文正在参加「金石计划 . 瓜分6万现金大奖」 ”

2.4实现 mysql 的角色

#实现 mysql5.6 的角色
[root@centos8 ansible]# mkdir -pv roles/mysql5.6/{tasks,files,vars}
mkdir: created directory 'roles/mysql5.6'
mkdir: created directory 'roles/mysql5.6/tasks'
mkdir: created directory 'roles/mysql5.6/files'
mkdir: created directory 'roles/mysql5.6/vars'

[root@centos8 mysql5.6]# vim files/my.cnf
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
:wq

[root@centos8 mysql5.6]# vim files/secure_mysql.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-01-27
#FileName:      /data/ansible/files/secure_mysql.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
/usr/local/mysql/bin/mysql_secure_installation <<EOF

y
123456
123456
y
y
y
y
EOF
:wq

[root@centos8 mysql5.6]# chmod +x files/secure_mysql.sh 

[root@centos8 mysql5.6]# ls files/
my.cnf  mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz  secure_mysql.sh

[root@centos8 mysql5.6]# vim tasks/main.yml
- include: install.yml
- include: group.yml                    
- include: user.yml
- include: unarchive.yml
- include: link.yml
- include: data.yml
- include: config.yml
- include: service.yml
- include: path.yml
- include: secure.yml
:wq

[root@centos8 mysql5.6]# vim tasks/install.yml
- name: install packages centos7
  yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
  when:
    - ansible_facts['distribution_major_version'] == "7"
- name: install packages centos8
  yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long,ncurses-compat-libs
  when:
    - ansible_facts['distribution_major_version'] == "8"  
:wq

[root@centos8 mysql5.6]# vim tasks/group.yml
- name: create mysql group
  group: name=mysql gid=306
:wq

[root@centos8 mysql5.6]# vim tasks/user.yml
- name: create mysql user
  user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
:wq

[root@centos8 mysql5.6]# vim tasks/unarchive.yml
- name: copy tar to remote host and file mode
  unarchive: src=mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root 
:wq

[root@centos8 mysql5.6]# vim tasks/link.yml
- name: create linkfile /usr/local/mysql
  file: src=/usr/local/mysql-{{mysql_version}}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
:wq

[root@centos8 mysql5.6]# vim tasks/data.yml
- name: data dir
  shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
:wq

[root@centos8 mysql5.6]# vim tasks/config.yml
- name: config my.cnf
  copy: src=my.cnf dest=/etc/my.cnf 
:wq

[root@centos8 mysql5.6]# vim tasks/service.yml
- name: service script
  shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: enable service
  shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
:wq

[root@centos8 mysql5.6]# vim tasks/path.yml
- name: PATH variable
  copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
:wq

[root@centos8 ansible]# vim roles/mysql5.6/tasks/secure.yml 
- name: secure script
  script: secure_mysql.sh  
:wq

[root@centos8 mysql5.6]# vim vars/main.yml
mysql_version: 5.6.51 
:wq

[root@centos8 mysql5.6]# tree
.
├── files
│   ├── my.cnf
│   ├── mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
│   └── secure_mysql.sh
├── tasks
│   ├── config.yml
│   ├── data.yml
│   ├── group.yml
│   ├── install.yml
│   ├── link.yml
│   ├── main.yml
│   ├── path.yml
│   ├── secure.yml
│   ├── service.yml
│   ├── unarchive.yml
│   └── user.yml
└── vars
    └── main.yml

3 directories, 15 files

[root@centos8 mysql5.6]# cd ../../
[root@centos8 ansible]# vim role_mysql5.6.yml 
---
- hosts: dbsrvs
  remote_user: root

  roles:
    - {role: mysql5.6,tags: ["mysql5.6","db"]}             
    - {role: nginx,tags: ["nginx","web"]}
:wq

[root@centos8 ansible]# ansible-playbook -t mysql5.6 role_mysql5.6.yml

PLAY [dbsrvs] ********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.0.0.57]
ok: [10.0.0.58]

TASK [mysql5.6 : install packages centos7] ***************************************************************************************
skipping: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql5.6 : install packages centos8] ***************************************************************************************
skipping: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : create mysql group] *********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : create mysql user] **********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : copy tar to remote host and file mode] **************************************************************************
changed: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql5.6 : create linkfile /usr/local/mysql] *******************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : data dir] *******************************************************************************************************
changed: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql5.6 : config my.cnf] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : service script] *************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : enable service] *************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : PATH variable] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.6 : secure script] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

PLAY RECAP ***********************************************************************************************************************
10.0.0.57                  : ok=12   changed=11   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.58                  : ok=12   changed=11   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

[root@centos8-6 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.51-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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> status
--------------
mysql  Ver 14.14 Distrib 5.6.51, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:		12
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.6.51-log MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/tmp/mysql.sock
Uptime:			23 sec

Threads: 1  Questions: 40  Slow queries: 0  Opens: 79  Flush tables: 2  Open tables: 9  Queries per second avg: 1.739
--------------

mysql> exit
Bye

[root@centos7-6 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.51-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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> status
--------------
mysql  Ver 14.14 Distrib 5.6.51, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:		12
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.6.51-log MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/tmp/mysql.sock
Uptime:			1 min 3 sec

Threads: 1  Questions: 40  Slow queries: 0  Opens: 79  Flush tables: 2  Open tables: 9  Queries per second avg: 0.634
--------------

mysql> exit
Bye


[root@centos8 ansible]# ansible-playbook -t nginx role_mysql5.6.yml

PLAY [dbsrvs] ********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.0.0.57]
ok: [10.0.0.58]

TASK [nginx : install] ***********************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [nginx : config file for centos7] *******************************************************************************************
skipping: [10.0.0.58]
changed: [10.0.0.57]

TASK [nginx : config file for centos8] *******************************************************************************************
skipping: [10.0.0.57]
changed: [10.0.0.58]

TASK [nginx : index.html] ********************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [nginx : start service] *****************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

RUNNING HANDLER [nginx : restart] ************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

PLAY RECAP ***********************************************************************************************************************
10.0.0.57                  : ok=6    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.58                  : ok=6    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
#实现 mysql5.7 的角色
[root@centos8 ansible]# mkdir -pv roles/mysql5.7/{tasks,files,vars}
mkdir: created directory 'roles/mysql5.7'
mkdir: created directory 'roles/mysql5.7/tasks'
mkdir: created directory 'roles/mysql5.7/files'
mkdir: created directory 'roles/mysql5.7/vars'

root@centos8 ansible]# cd roles/mysql5.7/
[root@centos8 mysql5.7]# vim files/my.cnf
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock                                                                                                   
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
:wq

[root@centos8 mysql5.7]# vim files/set_pass.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-02-05
#FileName:      files/set_pass.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
MYSQL_ROOT_PASSWORD=123456
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin  -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/null 
:wq

[root@centos8 mysql5.7]# chmod +x files/set_pass.sh 

[root@centos8 mysql5.7]# ls files/
my.cnf  mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz  set_pass.sh

[root@centos8 mysql5.7]# vim tasks/main.yml
- include: install.yml
- include: group.yml                    
- include: user.yml
- include: unarchive.yml
- include: link.yml
- include: path.yml
- include: config.yml
- include: data.yml
- include: service.yml
- include: set_pass.yml
:wq

[root@centos8 mysql5.7]# vim tasks/install.yml
- name: install packages centos7
  yum: name=libaio,perl-Data-Dumper
  when:
    - ansible_facts['distribution_major_version'] == "7"
- name: install packages centos8
  yum: name=libaio,perl-Data-Dumper,ncurses-compat-libs
  when:           
    - ansible_facts['distribution_major_version'] == "8"
:wq

[root@centos8 mysql5.7]# vim tasks/group.yml
- name: cteate mysql group
  group: name=mysql gid=306
:wq

[root@centos8 mysql5.7]# vim tasks/user.yml
- name: create mysql user
  user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
:wq

[root@centos8 mysql5.7]# vim tasks/unarchive.yml
- name: copy tar to remote host and file mode
  unarchive: src=mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
:wq

[root@centos8 mysql5.7]# vim tasks/link.yml
- name: create linkfile /usr/local/mysql
  file: src=/usr/local/mysql-{{mysql_version}}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
:wq

[root@centos8 mysql5.7]# vim tasks/path.yml
- name: PATH variable
  copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: PATH variable entry
  shell: . /etc/profile.d/mysql.sh
:wq

[root@centos8 mysql5.7]# vim tasks/config.yml
- name: config my.cnf
  copy: src=my.cnf dest=/etc/my.cnf
:wq

[root@centos8 mysql5.7]# vim tasks/data.yml            
- name: data dir
  shell: chdir=/usr/local/mysql ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql
:wq

[root@centos8 mysql5.7]# vim tasks/service.yml
- name: service script
  shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: enable service
  shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
:wq

[root@centos8 mysql5.7]# vim tasks/set_pass.yml
- name: set mysql user password
  script: set_pass.sh
:wq

[root@centos8 mysql5.7]# vim vars/main.yml
mysql_version: 5.7.33
:wq

[root@centos8 mysql5.7]# tree
.
├── files
│   ├── my.cnf
│   ├── mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
│   └── set_pass.sh
├── tasks
│   ├── config.yml
│   ├── data.yml
│   ├── group.yml
│   ├── install.yml
│   ├── link.yml
│   ├── main.yml
│   ├── path.yml
│   ├── service.yml
│   ├── set_pass.yml
│   ├── unarchive.yml
│   └── user.yml
└── vars
    └── main.yml

3 directories, 15 files

[root@centos8 mysql5.7]# cd ../../
[root@centos8 ansible]# vim role_mysql5.7.yml
---
- hosts: dbsrvs
  remote_user: root

  roles:
    - role: mysql5.7
:wq

[root@centos8 ansible]# ansible-playbook role_mysql5.7.yml 

PLAY [dbsrvs] ********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.0.0.57]
ok: [10.0.0.58]

TASK [mysql5.7 : install packages centos7] ***************************************************************************************
skipping: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql5.7 : install packages centos8] ***************************************************************************************
skipping: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : cteate mysql group] *********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : create mysql user] **********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : copy tar to remote host and file mode] **************************************************************************
changed: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql5.7 : create linkfile /usr/local/mysql] *******************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : PATH variable] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : PATH variable entry] ********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : config my.cnf] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : data dir] *******************************************************************************************************
changed: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql5.7 : service script] *************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : enable service] *************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql5.7 : set mysql user password] ****************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

PLAY RECAP ***********************************************************************************************************************
10.0.0.57                  : ok=13   changed=12   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.58                  : ok=13   changed=12   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

[root@centos8-6 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> status
--------------
mysql  Ver 14.14 Distrib 5.7.33, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:		3
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.33-log MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/data/mysql/mysql.sock
Uptime:			40 sec

Threads: 1  Questions: 9  Slow queries: 0  Opens: 109  Flush tables: 2  Open tables: 1  Queries per second avg: 0.225
--------------

mysql> exit
Bye
[root@centos8-6 ~]# ss -ntl
State            Recv-Q           Send-Q                     Local Address:Port                       Peer Address:Port           
LISTEN           0                128                              0.0.0.0:22                              0.0.0.0:*              
LISTEN           0                80                                     *:3306                                  *:*              
LISTEN           0                128                                 [::]:22                                 [::]:*   

[root@centos7-6 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> status
--------------
mysql  Ver 14.14 Distrib 5.7.33, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:		3
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.33-log MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/data/mysql/mysql.sock
Uptime:			1 min 38 sec

Threads: 1  Questions: 9  Slow queries: 0  Opens: 109  Flush tables: 2  Open tables: 1  Queries per second avg: 0.091
--------------

mysql> exit
Bye
[root@centos7-6 ~]# ss -ntl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:*                  
LISTEN     0      80                                 [::]:3306                                           [::]:* 
#实现 mysql8.0 的角色
[root@centos8 mysql8.0]# mkdir -pv roles/mysql8.0/{tasks,files,vars}
mkdir: created directory 'roles/mysql8.0'
mkdir: created directory 'roles/mysql8.0/tasks'
mkdir: created directory 'roles/mysql8.0/files'
mkdir: created directory 'roles/mysql8.0/vars'

[root@centos8 ansible]# cd roles/mysql8.0/
[root@centos8 mysql8.0]# vim files/my.cnf 
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock                                                                                                   
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
:wq

[root@centos8 mysql8.0]# vim files/set_pass.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-02-05
#FileName:      files/set_pass.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
MYSQL_ROOT_PASSWORD=123456
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin  -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/null
:wq

[root@centos8 mysql5.7]# chmod +x files/set_pass.sh
[root@centos8 mysql8.0]# ls files/
my.cnf  mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz  set_pass.sh

[root@centos8 mysql8.0]# vim tasks/main.yml 
- include: install.yml
- include: group.yml                    
- include: user.yml
- include: unarchive.yml
- include: link.yml
- include: path.yml
- include: config.yml
- include: data.yml
- include: service.yml
- include: set_pass.yml
:wq

[root@centos8 mysql8.0]# vim tasks/install.yml 
- name: install packages centos7
  yum: name=libaio,perl-Data-Dumper
  when:
    - ansible_facts['distribution_major_version'] == "7"
- name: install packages centos8
  yum: name=libaio,perl-Data-Dumper,ncurses-compat-libs
  when:          
    - ansible_facts['distribution_major_version'] == "8"
:wq

[root@centos8 mysql8.0]# vim tasks/group.yml 
- name: cteate mysql group
  group: name=mysql gid=306
:wq

[root@centos8 mysql8.0]# vim tasks/user.yml 
- name: create mysql user
  user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
:wq

[root@centos8 mysql8.0]# vim tasks/unarchive.yml 
- name: copy tar to remote host and file mode
  unarchive: src=mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.xz dest=/usr/local/ owner=root group=root
:wq

[root@centos8 mysql8.0]# vim tasks/link.yml 
- name: create linkfile /usr/local/mysql
  file: src=/usr/local/mysql-{{mysql_version}}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
:wq

[root@centos8 mysql8.0]# vim tasks/path.yml 
- name: PATH variable
  copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: PATH variable entry
  shell: . /etc/profile.d/mysql.sh
:wq

[root@centos8 mysql8.0]# vim tasks/config.yml 
- name: config my.cnf
  copy: src=my.cnf dest=/etc/my.cnf
:wq

[root@centos8 mysql8.0]# vim tasks/data.yml 
- name: data dir
  shell: chdir=/usr/local/mysql ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql
:wq

[root@centos8 mysql8.0]# vim tasks/service.yml 
- name: service script
  shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: enable service
  shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
:wq

[root@centos8 mysql8.0]# vim tasks/set_pass.yml 
- name: set mysql user password
  script: set_pass.sh
:wq

[root@centos8 mysql8.0]# vim vars/main.yml 
mysql_version: 8.0.23
:wq

[root@centos8 mysql8.0]# tree
.
├── files
│   ├── my.cnf
│   ├── mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
│   └── set_pass.sh
├── tasks
│   ├── config.yml
│   ├── data.yml
│   ├── group.yml
│   ├── install.yml
│   ├── link.yml
│   ├── main.yml
│   ├── path.yml
│   ├── service.yml
│   ├── set_pass.yml
│   ├── unarchive.yml
│   └── user.yml
└── vars
    └── main.yml

3 directories, 15 files


[root@centos8 mysql8.0]# cd ../../
[root@centos8 ansible]# vim role_mysql8.0.yml
---
- hosts: dbsrvs
  remote_user: root

  roles:
    - role: mysql8.0 
:wq

[root@centos8 ansible]# ansible-playbook role_mysql8.0.yml 

PLAY [dbsrvs] ********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.0.0.57]
ok: [10.0.0.58]

TASK [mysql8.0 : install packages centos7] ***************************************************************************************
skipping: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql8.0 : install packages centos8] ***************************************************************************************
skipping: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : cteate mysql group] *********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : create mysql user] **********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : copy tar to remote host and file mode] **************************************************************************
changed: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql8.0 : create linkfile /usr/local/mysql] *******************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : PATH variable] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : PATH variable entry] ********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : config my.cnf] **************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : data dir] *******************************************************************************************************
changed: [10.0.0.58]
changed: [10.0.0.57]

TASK [mysql8.0 : service script] *************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : enable service] *************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mysql8.0 : set mysql user password] ****************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

PLAY RECAP ***********************************************************************************************************************
10.0.0.57                  : ok=13   changed=12   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.58                  : ok=13   changed=12   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

[root@centos8-6 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> status
--------------
mysql  Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		9
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.23 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/data/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			54 sec

Threads: 2  Questions: 9  Slow queries: 0  Opens: 130  Flush tables: 4  Open tables: 10  Queries per second avg: 0.166
--------------

mysql> exit
Bye
[root@centos8-6 ~]# ss -ntl
State            Recv-Q           Send-Q                     Local Address:Port                       Peer Address:Port           
LISTEN           0                128                              0.0.0.0:22                              0.0.0.0:*              
LISTEN           0                70                                     *:33060                                 *:*              
LISTEN           0                128                                    *:3306                                  *:*              
LISTEN           0                128                                 [::]:22                                 [::]:*  

[root@centos7-6 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> status
--------------
mysql  Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		9
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.23 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/data/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			1 min 32 sec

Threads: 2  Questions: 9  Slow queries: 0  Opens: 130  Flush tables: 4  Open tables: 10  Queries per second avg: 0.097
--------------

mysql> exit
Bye
[root@centos7-6 ~]# ss -ntl
State      Recv-Q Send-Q                    Local Address:Port                                   Peer Address:Port              
LISTEN     0      128                                   *:22                                                *:*                  
LISTEN     0      100                           127.0.0.1:25                                                *:*                  
LISTEN     0      70                                 [::]:33060                                          [::]:*                  
LISTEN     0      128                                [::]:3306                                           [::]:*                  
LISTEN     0      128                                [::]:22                                             [::]:*                  
LISTEN     0      100                               [::1]:25                                             [::]:* 
#实现 mariadb 10.2 的角色
[root@centos8 ansible]# mkdir -pv roles/mariadb10.2/{tasks,files,vars}
mkdir: created directory 'roles'
mkdir: created directory 'roles/mariadb10.2'
mkdir: created directory 'roles/mariadb10.2/tasks'
mkdir: created directory 'roles/mariadb10.2/files'
mkdir: created directory 'roles/mariadb10.2/vars'

[root@centos8 mariadb10.2]# vim files/my.cnf 
[mysqld]               
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
:wq

[root@centos8 mariadb10.2]# ls files/
mariadb-10.2.36-linux-x86_64.tar.gz  my.cnf

[root@centos8 mariadb10.2]# vim tasks/main.yml
- include: install.yml
- include: group.yml
- include: user.yml
- include: datadir.yml                 
- include: unarchive.yml
- include: link.yml
- include: data.yml
- include: config.yml
- include: service.yml
- include: path.yml
:wq

[root@centos8 mariadb10.2]# vim tasks/install.yml
- name: install packages centos7
  yum: name=libaio
  when:
    - ansible_facts['distribution_major_version'] == "7"
- name: install packages centos8
  yum: name=libaio,ncurses-compat-libs
  when:
    - ansible_facts['distribution_major_version'] == "8" 
  :wq
  
[root@centos8 mariadb10.2]# vim tasks/group.yml
- name: create group
  group: name=mysql gid=27 system=yes 
:wq

[root@centos8 mariadb10.2]# vim tasks/user.yml
- name: create user
  user: name=mysql uid=27 system=yes group=mysql shell=/sbin/nologin home=/data/mysql create_home=no
:wq

[root@centos8 mariadb10.2]# vim tasks/datadir.yml 
- name: mkdir datadir          
  file: path=/data/mysql owner=mysql group=mysql state=directory
:wq

[root@centos8 mariadb10.2]# vim tasks/unarchive.yml
- name: unarchive package
  unarchive: src=mariadb-{{mysql_version}}-linux-x86_64.tar.gz dest=/usr/local/ owner=root group=root
:wq

[root@centos8 mariadb10.2]# vim tasks/link.yml
- name: link
  file: src=/usr/local/mariadb-{{mysql_version}}-linux-x86_64 path=/usr/local/mysql state=link
:wq

[root@centos8 mariadb10.2]# vim tasks/data.yml
- name: install database
  shell: chdir=/usr/local/mysql ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
:wq

[root@centos8 mariadb10.2]# vim tasks/config.yml
- name: config file
  copy: src=my.cnf dest=/etc/ backup=yes
:wq

[root@centos8 mariadb10.2]# vim tasks/service.yml
- name: service script
  shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: start service
  service: name=mysqld state=started enabled=yes 
:wq

[root@centos8 mariadb10.2]# vim tasks/path.yml
- name: PATH variable
  copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
:wq

[root@centos8 mariadb10.2]# vim vars/main.yml
mysql_version: 10.2.36
:wq

[root@centos8 mariadb10.2]# tree
.
├── files
│   ├── mariadb-10.2.36-linux-x86_64.tar.gz
│   └── my.cnf
├── tasks
│   ├── config.yml
│   ├── datadir.yml
│   ├── data.yml
│   ├── group.yml
│   ├── install.yml
│   ├── link.yml
│   ├── main.yml
│   ├── path.yml
│   ├── service.yml
│   ├── unarchive.yml
│   └── user.yml
└── vars
    └── main.yml

3 directories, 14 files

[root@centos8 mariadb10.2]# cd ../../
[root@centos8 ansible]# vim role_mariadb10.2.yml
---
- hosts: dbsrvs
  remote_user: root

  roles:
    - role: mariadb10.2 
:wq


[root@centos8 ansible]# ansible-playbook role_mariadb10.2.yml 

PLAY [dbsrvs] *****************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
ok: [10.0.0.57]
ok: [10.0.0.58]

TASK [mariadb10.2 : install packages centos7] *********************************************************************************
skipping: [10.0.0.58]
changed: [10.0.0.57]

TASK [mariadb10.2 : install packages centos8] *********************************************************************************
skipping: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : create group] *********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : create user] **********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : mkdir datadir] ********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : unarchive package] ****************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : link] *****************************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : install database] *****************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : config file] **********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : service script] *******************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : start service] ********************************************************************************************
[WARNING]: The service (mysqld) is actually an init script but the system is managed by systemd
changed: [10.0.0.57]
changed: [10.0.0.58]

TASK [mariadb10.2 : PATH variable] ********************************************************************************************
changed: [10.0.0.57]
changed: [10.0.0.58]

PLAY RECAP ********************************************************************************************************************
10.0.0.57                  : ok=12   changed=11   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.58                  : ok=12   changed=11   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

[root@centos8-6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> status
--------------
mysql  Ver 15.1 Distrib 10.2.36-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:		10
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		10.2.36-MariaDB-log MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/data/mysql/mysql.sock
Uptime:			32 sec

Threads: 8  Questions: 5  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 11  Queries per second avg: 0.156
--------------

MariaDB [(none)]> exit
Bye

[root@centos7-6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> status
--------------
mysql  Ver 15.1 Distrib 10.2.36-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:		10
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		10.2.36-MariaDB-log MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/data/mysql/mysql.sock
Uptime:			1 min 21 sec

Threads: 8  Questions: 5  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 11  Queries per second avg: 0.061
--------------

MariaDB [(none)]> exit
Bye

2.5实现多角色的选择

[root@centos8 ansible]# vim role_httpd_nginx.yml
---
- hosts: websrvs                                                                                                                  

  roles:
      - {role: httpd,tags: [httpd,web], when: ansible_distribution_major_version=="7" }
      - {role: nginx,tags: [nginx,web], when: ansible_distribution_major_version=="8" }
:wq

[root@centos8 ansible]# ansible-playbook -t nginx role_httpd_nginx.yml 

PLAY [websrvs] *******************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.0.0.47]
ok: [10.0.0.48]

TASK [nginx : install] ***********************************************************************************************************
skipping: [10.0.0.47]
ok: [10.0.0.48]

TASK [nginx : config file for centos7] *******************************************************************************************
skipping: [10.0.0.47]
skipping: [10.0.0.48]

TASK [nginx : config file for centos8] *******************************************************************************************
skipping: [10.0.0.47]
ok: [10.0.0.48]

TASK [nginx : index.html] ********************************************************************************************************
skipping: [10.0.0.47]
ok: [10.0.0.48]

TASK [nginx : start service] *****************************************************************************************************
skipping: [10.0.0.47]
ok: [10.0.0.48]

PLAY RECAP ***********************************************************************************************************************
10.0.0.47                  : ok=1    changed=0    unreachable=0    failed=0    skipped=5    rescued=0    ignored=0   
10.0.0.48                  : ok=5    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
[root@centos8 ansible]# ansible-playbook -t httpd role_httpd_nginx.yml

PLAY [websrvs] *******************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.0.0.47]
ok: [10.0.0.48]

TASK [httpd : create apache group] ***********************************************************************************************
skipping: [10.0.0.48]
changed: [10.0.0.47]

TASK [httpd : create apache user] ************************************************************************************************
skipping: [10.0.0.48]
changed: [10.0.0.47]

TASK [install httpd package] *****************************************************************************************************
skipping: [10.0.0.48]
changed: [10.0.0.47]

TASK [httpd : config file] *******************************************************************************************************
skipping: [10.0.0.48]
changed: [10.0.0.47]

TASK [httpd : index.html] ********************************************************************************************************
skipping: [10.0.0.48]
changed: [10.0.0.47]

TASK [httpd : start service] *****************************************************************************************************
skipping: [10.0.0.48]
changed: [10.0.0.47]

RUNNING HANDLER [nginx : restart] ************************************************************************************************
skipping: [10.0.0.47]

PLAY RECAP ***********************************************************************************************************************
10.0.0.47                  : ok=7    changed=6    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.48                  : ok=1    changed=0    unreachable=0    failed=0    skipped=6    rescued=0    ignored=0