角色安装mysql5.6服务
//之前我们提前创建好了目录文件
[root@node-17 roles]
httpd mysql nginx
[root@node-17 roles]
./mysql/
├── files
├── handlers
├── tasks
├── templates
└── vars
5 directories, 0 files
-------------------------------------------------
//新建编辑我们所需要的配置文件
[root@node-17 mysql]
[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
-----------------------------------------------------
//将提前下载好的安装包放到指定的文件目录下
[root@node-17 files]
[root@node-17 files]
my.cnf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
----------------------------------------------------
//新建编辑后面所需要安全加固应答脚本
[root@node-17 files]
/usr/local/mysql/bin/mysql_secure_installation <<EOF
y
magedu
magedu
y
y
y
y
EOF
--------------------------------------------------------
//新建编辑/mysql/tasks/下的main.yml文件
/usr/local/mysql/bin/mysql_secure_installation <<EOF
y
magedu
magedu
y
y
y
y
EOF
---------------------------------------------------------
//新建编辑/mysql/tasks/目录下的各个步骤的yml文件
[root@node-17 mysql]
---
- name: install packages
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
--------------------------------------------------
//新建编辑group.yml文件
[root@node-17 mysql]
---
- name: create mysql group
group: name=mysql gid=306
----------------------------------------------------
//新建编辑user.yml文件
[root@node-17 mysql]
---
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes createhome=no home=/data/mysql
------------------------------------------------
//新建编辑unarchive.yml文件
[root@node-17 mysql]
---
- name: copy tar to remote host and file mode
unarchive: src=/data/ansible/roles/mysql/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
------------------------------------------------
//新建编辑link.yml
[root@node-17 mysql]
---
- name: mkdir /usr/local/mysql
file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
-------------------------------------------------
//新建编辑data.yml文件
[root@node-17 mysql]
---
- name: data dir
shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
tags: data
---------------------------------------------------
//新建编辑config.yml文件
[root@node-17 mysql]
---
- name: config my.cnf
copy: src=/data/ansible/mysql/files/my.cnf dest=/etc/my.cnf
-------------------------------------------------------
//新建编辑service.yml文件
[root@node-17 mysql]
---
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld;/etc/init.d/mysqld start;chkonfing --add mysqld;chkconfig mysqld on
------------------------------------------------------
//新建编辑path.yml文件
[root@node-17 mysql]
---
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
--------------------------------------------------------
//新建编辑secure.yml文件
[root@node-17 mysql]
---
- name: secure script
script: /data/ansible/roles/mysql/files/secure_mysql.sh
tags: script
------------------------------------------------------------------
//新建编辑调用角色的nginx_role.yml文件
[root@node-17 ansible]
---
- hosts: websrvs
remote_user: root
roles:
- {role: mysql,tags: ["mysql" , "db"]}
//使用ansible-playbook 命令运行mysql_role.yml文件
[root@node-17 ansible]
PLAY [websrvs] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [mysql : install packages] *****************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [mysql : create mysql group] ***************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [mysql : create mysql user] ****************************************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [mysql : copy tar to remote host and file mode] ********************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [mysql : mkdir /usr/local/mysql] ***********************************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [mysql : data dir] *************************************************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [mysql : config my.cnf] ********************************************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [mysql : service script] *******************************************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
TASK [mysql : PATH variable] ********************************************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [mysql : secure script] ********************************************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
PLAY RECAP **************************************************************************************************************************
192.168.26.37 : ok=11 changed=5 unreachable=0 failed=0
192.168.26.47 : ok=11 changed=5 unreachable=0 failed=0
//查看远程主机端口信息
[root@node-37 ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 [::]:3306 [::]:*
--------------------------------------------------------------------
[root@node-47 ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 [::]:3306 [::]:*