1.把需要部署的主机添加到web组:
[web]
192.168.127.136 user=root ansible_ssh_user=root ansible_ssh_pass=1
192.168.127.148 user=root ansible_ssh_user=root ansible_ssh_pass=1
192.168.127.135 user=root ansible_ssh_user=root ansible_ssh_pass=1
#注:192.168.127.136 为管理节点
2.管理节点和被管理节点建立免密登录:
vim /etc/ansible/ansible.cfg
打开 host_key_checking = False #禁用SSH密钥主机检查
方法1:
ls ~/.ssh/id_rsa || ssh-keygen -f ~/.ssh/id_rsa -N "" #生成指定路径不要密码的密钥对
ansible web -m authorized_key -a "user=root state=present key={{ lookup('file', '/root/.ssh/id_rsa.pub') }}" -e "ansible_ssh_user=root ansible_ssh_pass=1 "
ansible web -m ping #测试是否建立免密登录
方法2:
ls ~/.ssh/id_rsa || ssh-keygen -f ~/.ssh/id_rsa -N ""
[root@slave ~]#cat a.yaml
- name: 免密
hosts: web
gather_facts: no
tasks:
- name: web免密
authorized_key:
user: "{{user}}"
state: present
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
ansible-playbook a.yaml
3.vim deploy-mysql.yaml
---
- name: 部署mysql
hosts: web
gather_facts: no
remote_user: root
tasks:
#初始化服务器 关闭防火墙,selinux
- name: 关闭selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=disabled'
state: present
- name: 安装库,开发工具包
yum:
name:
- '@Development Tools'
- https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
state: present
- name: 禁用 MySQL 8.0 仓库
ini_file:
path: /etc/yum.repos.d/mysql-community.repo
section: mysql80-community
option: enabled
value: '0'
- name: 启用 MySQL 5.7 仓库
ini_file:
path: /etc/yum.repos.d/mysql-community.repo
section: mysql57-community
option: enabled
value: '1'
- name: 安装mysql
yum:
name: mysql-community-server
state: present
- name: start mysql
service:
name: mysqld
state: started
...
ansible-playbook deploy-mysql.yaml -vvv #运行