linux进阶-ansible实现role角色安装mysql5.6

角色安装mysql5.6服务

//之前我们提前创建好了目录文件
[root@node-17 roles]# ls /data/ansible/roles/
httpd  mysql  nginx
[root@node-17 roles]# tree ./mysql/
./mysql/
├── files
├── handlers
├── tasks
├── templates
└── vars

5 directories, 0 files
-------------------------------------------------
//新建编辑我们所需要的配置文件
[root@node-17 mysql]# 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
-----------------------------------------------------
//将提前下载好的安装包放到指定的文件目录下
[root@node-17 files]# rz -E
[root@node-17 files]# ls
my.cnf  mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
----------------------------------------------------
//新建编辑后面所需要安全加固应答脚本
[root@node-17 files]# vim secure_mysql.sh
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF

y
magedu
magedu
y
y
y
y
EOF
--------------------------------------------------------
//新建编辑/mysql/tasks/下的main.yml文件
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF

y
magedu
magedu
y
y
y
y
EOF
---------------------------------------------------------
//新建编辑/mysql/tasks/目录下的各个步骤的yml文件
[root@node-17 mysql]# vim ./tasks/install.yml
---
- name: install packages
  yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
-------------------------------------------------- 
//新建编辑group.yml文件
[root@node-17 mysql]# vim ./tasks/group.yml
---
- name: create mysql group
  group: name=mysql gid=306
----------------------------------------------------
//新建编辑user.yml文件
[root@node-17 mysql]# vim ./tasks/user.yml
---
- 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]# vim ./tasks/unarchive.yml
---
- 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]# vim ./tasks/link.yml
---
- 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]# vim ./tasks/data.yml
---
- 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]# vim ./tasks/config.yml
---
- name: config my.cnf
  copy: src=/data/ansible/mysql/files/my.cnf dest=/etc/my.cnf
-------------------------------------------------------
//新建编辑service.yml文件
[root@node-17 mysql]# vim ./tasks/service.yml
---
- 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]# vim ./tasks/path.yml
---
- name: PATH variable
  copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
--------------------------------------------------------
//新建编辑secure.yml文件
[root@node-17 mysql]# vim ./tasks/secure.yml
---
- name: secure script
  script: /data/ansible/roles/mysql/files/secure_mysql.sh
  tags: script

------------------------------------------------------------------
//新建编辑调用角色的nginx_role.yml文件
[root@node-17 ansible]# vim mysql_role.yml
---
- hosts: websrvs
  remote_user: root

  roles:
    - {role: mysql,tags: ["mysql" , "db"]}
//使用ansible-playbook 命令运行mysql_role.yml文件
[root@node-17 ansible]# ansible-playbook  mysql_role.yml 

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 ~]# ss -ntl
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port                          
LISTEN      0      80                                  [::]:3306                                            [::]:*                  
--------------------------------------------------------------------
[root@node-47 ~]# ss -ntl
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port                          
LISTEN      0      80                                  [::]:3306                                            [::]:*         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值