基础环境:
修改主机名
修改hosts
[root@ansible ~]# cp /etc/hosts .
ansible节点配置yum源(从controller获取)
[root@ansible ~]# cat http.repo
[centos]
name=centos
baseurl=http://192.168.100.10/centos
gpgcheck=0
enabled=1
[openstack]
name=openstack
baseurl=http://192.168.100.10/openstack/iaas-repo
gpgcheck=0
enabled=1
[root@ansible ~]# yum install -y ansible
[root@ansible ~]# vi /etc/ansible/hosts
末尾添加
[hosts]
node1 ssh_pass=000000
node2 ssh_pass=000000
node3 ssh_pass=000000
[root@ansible ~]# vi /etc/ansible/ansible.cfg
host_key_checking = False
command_warnings = False
[root@ansible ~]# ansible all -m ping
测试通信是否成功
[root@ansible ~]# yum install -y mariadb-server mariadb-server-galera
[root@ansible ~]# cp /etc/my.cnf.d/mariadb-server.cnf .
[root@ansible ansible_work]# pwd
/root/ansible_work
[root@ansible ansible_work]# tree
.
├── cscc_install.yaml
└── roles
└── mariadb
└── tasks
└── main.yaml
3 directories, 2 files
下面是编写main.yaml哦:
[root@ansible ansible_work]# cat roles/mariadb/tasks/main.yaml
- name: cp hosts
copy: src=/root/hosts dest=/etc
- name: rm repo
shell: rm -rf /etc/yum.repos.d/*
- name: cp repo
copy: src=/root/ftp.repo dest=/etc/yum.repos.d/
- name: install
shell: yum -y install mariadb-server galera
- name: start mariadb
shell: systemctl start mariadb
- name: password
shell: mysqladmin password 123456
- name: grant
shell: mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
- name: vi conf
copy: src=/root/server.cnf dest=/etc/my.cnf.d/
- name: stop
shell: systemctl stop mariadb
- name: galera
shell: galera_new_cluster
when: ansible_fqdn=='node1'
- name: start
shell: systemctl start mariadb
when: ansible_fqdn=='node2'
- name: start
shell: systemctl start mariadb
when: ansible_fqdn=='node3'
以下是执行的配置文件和权限等:
[root@ansible ansible_work]# cat cscc_install.yaml
- hosts: hosts
remote_user: root
roles:
- mariadb
[root@ansible ansible_work]# vi /root/mariadb-server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://node1,node2,node3
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
接下来就可以执行啦:
完成后进行验证
执行playbook:
[root@ansible ansible_work]# ansible-playbook cscc_install.yaml
验证:
[root@ansible ansible_work]# ansible node1 -m shell -a 'mysql -uroot -p123456 -e"show status like \"wsrep_cluster_size\";"'
node1 | SUCCESS | rc=0 >>
Variable_name Value
wsrep_cluster_size 3