简介
了解ansible 后写的一些 小案例,加油学习,实践出功夫
编写 安装mysql的yaml文件
查看hosts
root@ecs-3c98-0002:~# tail -5 /etc/ansible/hosts
[mysql]
localhost ansible_connection=local
---
- hosts: mysql
remote_user: root
tasks:
- name: 安装依赖库
apt:
name: libaio1
state: present
- name: 创建目录
file:
path: /opt/tools
state: directory
- name: 下载软件包
get_url:
url: https://cdn.mysql.com/archives/mysql-5.6/mysql-server_5.6.33-1ubuntu14.04_amd64.deb-bundle.tar
dest: /opt/tools
- name: 解压软件包
unarchive:
src: /opt/tools/mysql-server_5.6.33-1ubuntu14.04_amd64.deb-bundle.tar
dest: /opt/tools
copy: no
mode: 0755
- name: 安装 mysql*.deb
apt:
deb: "{{ mysql_deb }}"
loop:
- /opt/tools/mysql-common_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/libmysqlclient18_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/libmysqlclient-dev_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/libmysqld-dev_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/mysql-community-client_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/mysql-client_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/mysql-community-server_5.6.33-1ubuntu14.04_amd64.deb
- /opt/tools/mysql-server_5.6.33-1ubuntu14.04_amd64.deb
loop_control:
loop_var: mysql_deb
- name: 启动mysql
systemd:
daemon_reload: yes
name: mysql
state: restarted
enabled: yes
- name: 查看状态
shell: systemctl status mysql
register: mysql
- debug:
var: mysql.stdout_lines
开始安装
# 检查yaml 文件是否存在语法格式错误
ansible-playbook --check --syntax-check mysql.yaml
# 模拟运行 -C 或者 --check
ansible-playbook -C mysql.yaml
# 没有问题的话执行安装
ansible-playbook mysql.yaml
使用ansible 安装mysql 5.6.33 的话默认是没有密码的,可以无密登录
这样并不安全,我们设置一下root密码,在禁用无密登录
设置root密码
set password for root@localhost = password('123456');
禁用无密登录
delete from mysql.user where user='';
delete from mysql.user where Password='';
#很关键的命令,刷新,不执行不生效
flush privileges;
测试一下
发现不输入密码现在登录不上了,输入用户名密码后可以登录,禁用还是生效的
当然可以脚本设置root密码和禁用无密登录
#!/bin/bash
#设置root密码
echo "set password for root@localhost = password('123456');" | mysql
#禁用无密登录
echo "delete from mysql.user where user='';"
echo "delete from mysql.user where Password='';"
echo "flush privileges;"