playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用的批量部署
Playbooks 的格式是YAML,语法做到最小化

这里讲述的是使用ansible-playbooks的roles安装Mysql应用

结构目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@Ansible ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── roles
│   └── yum_mysql
│   ├── files
│   │   └── yum.repo
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   └── my.cnf.j2
│   └── vars
│   └── main.yml
└── site.yml
7 directories, 8 files
[root@Ansible ~]#

Ansible之安装mysql

环境:
   ansible 2.2.1.0
   Centos 6.6
   Mysql 5.5.54

1
2
3
4
5
6
7
8
#入口文件
[root@Ansible ~]# cd /etc/ansible/
[root@Ansible ansible]# cat site.yml
- hosts: test_hosts
user: root
roles:
- yum_mysql
[root@Ansible ansible]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#tasks文件
[root@Ansible ansible]# cat roles/yum_mysql/tasks/main.yml
- name: Install libselinux-python package
yum: name=libselinux-python state=installed
- name: Check whether yum.repo file exist
shell: ls /etc/yum.repos.d/yum.repo
register: result
ignore_errors: True
- name: Copy yum.repo file
copy: src=yum.repo dest=/etc/yum.repos.d/ mode=0644 owner=root group=root
when: result|failed
- name: Rebuild the yum cache
shell: yum clean all && yum makecache
when: result|failed
- name: Install Mysql Packages
yum: name={{item.name}} state={{item.state|default("installed")}}
with_items:
- name: "mysql*"
state: "absent"
- name: "mysql.x86_64"
- name: "mysql-devel.x86_64"
- name: "mysql-server.x86_64"
- name: Configure Mysql file
template: src="templates/my.cnf.j2" dest="/etc/my.cnf" owner=root group=root mode=0644
notify: restart mysql
[root@Ansible ansible]#
1
2
3
4
5
6
7
8
#handlers
[root@Ansible ansible]# cat roles/yum_mysql/handlers/main.yml
- name: restart mysql
service: name=mysqld state=restarted enabled=yes
notify: "Set the mysql database password"
- name: Set the mysql database password
shell: /usr/bin/mysqladmin -u root password '123456'
[root@Ansible ansible]#
1
2
3
4
5
6
7
8
9
10
11
12
#变量
[root@Ansible ansible]# cat roles/yum_mysql/vars/main.yml
data_dir: /var/lib/mysql
socket_dir: /var/lib/mysql/mysql.sock
mysql_user: mysql
log_error_dir: /var/log/mysqld.log
pid_dir: /var/run/mysqld/mysqld.pid
port: 3306
log_bin: mysql_bin
binlog_format: mixed
server_id: 1
[root@Ansible ansible]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#执行过程
[root@Ansible ~]# ansible-playbook /etc/ansible/site.yml
PLAY [test_hosts] **************************************************************
TASK [setup] *******************************************************************
ok: [192.168.31.110]
TASK [yum_mysql : Install libselinux-python package] ***************************
changed: [192.168.31.110]
TASK [yum_mysql : Check whether yum.repo file exist] ***************************
fatal: [192.168.31.110]: FAILED! => {"changed": true, "cmd": "ls /etc/yum.repos.d/yum.repo", "delta": "0:00:00.004198", "end": "2017-03-19 21:22:56.861926", "failed": true, "rc": 2, "start": "2017-03-19 21:22:56.857728", "stderr": "ls: cannot access /etc/yum.repos.d/yum.repo: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [yum_mysql : Copy yum.repo file] ******************************************
changed: [192.168.31.110]
TASK [yum_mysql : Rebuild the yum cache] ***************************************
changed: [192.168.31.110]
[WARNING]: Consider using yum module rather than running yum
TASK [yum_mysql : Install Mysql Packages] **************************************
changed: [192.168.31.110] => (item={u'state': u'absent', u'name': u'mysql*'})
changed: [192.168.31.110] => (item={u'name': u'mysql.x86_64'})
changed: [192.168.31.110] => (item={u'name': u'mysql-devel.x86_64'})
changed: [192.168.31.110] => (item={u'name': u'mysql-server.x86_64'})
TASK [yum_mysql : Configure Mysql file] ****************************************
changed: [192.168.31.110]
RUNNING HANDLER [yum_mysql : restart mysql] ************************************
changed: [192.168.31.110]
RUNNING HANDLER [yum_mysql : Set the mysql database password] ******************
changed: [192.168.31.110]
PLAY RECAP *********************************************************************
192.168.31.110 : ok=9 changed=8 unreachable=0 failed=0
[root@Ansible ~]#
1
2
3
4
5
[root@Ansible ~]# ansible test_hosts -m shell -a 'netstat -tunlp | grep 3306'
192.168.31.110 | SUCCESS | rc=0 >>
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2086/mysqld
[root@Ansible ~]#

image

附件:ansible_yum_install_mysql.tar.gz


本文出自”Jack Wang Blog”:http://www.yfshare.vip/2017/03/16/Ansible-Playbooks之安装Mysql/