Linux - Ansible - use playbook to install mysql

[19:15:23 root@centos7 ansible]#tree
.
├── files
│   ├── my.cnf
│   └── mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
├── hosts
└── install_mysql8.0-v1.yaml

1 directory, 4 files


# set mysql configuration file
[19:15:27 root@centos7 ansible]#cat files/my.cnf 
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
explicit_defaults_for_timestamp=true
log_bin=/data/mysql/logbin/mysql-binlog
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
log_timestamps=SYSTEM
server-id=1

[client]
socket=/data/mysql/mysql.sock

[mysql]
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"


# upload mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz to files/

# set hosts file
[19:18:16 root@centos7 ansible]#cat hosts 
[webserver1]
10.0.0.112
[webserver2]
10.0.0.113


# set playbook
[19:18:34 root@centos7 ansible]#cat install_mysql8.0-v1.yaml 
---
# install mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
#
- hosts: webserver1
  remote_user: root
  gather_facts: no
  vars:
     mysql_version: 8.0.23
     mysql_file: mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.xz
     mysql_root_password: 123456

  tasks:
    - name: install packages
      yum:
        name: 
          - libaio
          - numactl-libs
        state: latest
    - name: create mysql group
      group: name=mysql gid=306
    - name: create mysql user
      user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
    - name: copy tar to remote host and file mode
      unarchive: src=/data/ansible/files/{{mysql_file}} dest=/usr/local/ owner=root group=root
    - name: create linkfile /usr/local/mysql
      file: src=/usr/local/mysql-{{mysql_version}}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
    - name: data dir
      shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
      tags: data
    - name: config my.cnf
      copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
      notify:
        - restart mysqld
        - notify message
      tags:
        - config
    - name: service script
      shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    - name: PATH variable
      copy: content=PATH=/usr/local/mysql/bin:$PATH dest=/etc/profile.d/mysql.sh
    - name: enable service
      shell: chkconfig --add mysqld;/etc/init.d/mysqld start
      tags: service
    - name: change password
      shell: /usr/local/mysql/bin/mysqladmin -uroot password {{mysql_root_password}}

  handlers:
    - name: restart mysqld
      service: name=mysqld state=restarted
    - name: notify message
      debug: msg="mysql is restarted"

# execute playbook
[18:59:19 root@centos7 ansible]#ansible-playbook install_mysql8.0-v1.yaml 

PLAY [webserver1] ****************************************************************************************************************************************

TASK [install packages] **********************************************************************************************************************************
ok: [10.0.0.112]

TASK [create mysql group] ********************************************************************************************************************************
ok: [10.0.0.112]

TASK [create mysql user] *********************************************************************************************************************************
ok: [10.0.0.112]

TASK [copy tar to remote host and file mode] *************************************************************************************************************
changed: [10.0.0.112]

TASK [create linkfile /usr/local/mysql] ******************************************************************************************************************
ok: [10.0.0.112]

TASK [data dir] ******************************************************************************************************************************************
changed: [10.0.0.112]

TASK [config my.cnf] *************************************************************************************************************************************
changed: [10.0.0.112]

TASK [service script] ************************************************************************************************************************************
changed: [10.0.0.112]

TASK [PATH variable] *************************************************************************************************************************************
ok: [10.0.0.112]

TASK [enable service] ************************************************************************************************************************************
changed: [10.0.0.112]

TASK [change password] ***********************************************************************************************************************************
changed: [10.0.0.112]

PLAY RECAP ***********************************************************************************************************************************************
10.0.0.112                 : ok=11   changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



# when the configuration file is changed, you can rerun from tags config
[19:13:54 root@centos7 ansible]#ansible-playbook -t config install_mysql8.0-v1.yaml 

PLAY [webserver1] ***************************************************************************************************************************************

TASK [config my.cnf] ************************************************************************************************************************************
changed: [10.0.0.112]

RUNNING HANDLER [restart mysqld] ************************************************************************************************************************
changed: [10.0.0.112]

RUNNING HANDLER [notify message] ************************************************************************************************************************
ok: [10.0.0.112] => {
    "msg": "mysql is restarted"
}

PLAY RECAP **********************************************************************************************************************************************
10.0.0.112                 : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

# then check 10.0.0.112 mysql
[19:17:25 root@node1 logbin]#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 15
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.

07:20:55(root@localhost) [(none)]> 

 

Linux - Ansible - use playbook to install mysql

上一篇:access数据库代码


下一篇:【Redis】Nosql非关系型数据库