[mysql]
127.0.0.1
touch db-server-playbook.yml
1 ---
2 - hosts: mysql
3
4 vars:
5 mysql_root_password: password
6
7 tasks:
8 - name: install mysql
9 apt:
10 name: mysql
11 update_cache: yes
12 cache_valid_time: 3600
13 state: present
14 - name: start up the mysql service
15 shell: "service mysql start"
16 - name: ensure mysql is enabled to run on startup
17 service:
18 name:mysql
19 state:started
20 enabled:true
21 - name: update mysql root password for all root accounts
22 mysql_user:
23 name: root
24. host: "{{ item }}"
25 password: "{{ mysql_root_password }}"
26 login_user: root
27 login_password: "{{ mysql_root_password }}"
28 check_implicit_admin: yes
29 priv: "*.*:ALL,GRANT"
30 with_items:
31 - "{{ ansible_hostname }}"
32 - 127.0.0.1
33 - ::1
34 - localhost
35 - name: create a new database
36 mysql_db:
37 name: testdb
38 state: present
39 login_user: root
40 login_password: "{{ mysql_root_password }}"
41 - name: add sample data to database
42 copy:
43 src:dump.sql
44 dest:/tmp/dump.sql
45 - name: insert sample data into database
46 mysql_db:
47 name: testdb
48 state: import
49 target: /tmp/dump.sql
50 login_user: root
51 login_password: "{{ mysql_root_password }}"
touch dump.sql
CREATE TABLE IF NOT EXISTS test (
message varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO test(message) VALUES('Ansible To Do List');
INSERT INTO test(message) VALUES('Get ready');
INSERT INTO test(message) VALUES('Ansible is fun')
ansible-playbook -i hosts db-server-playbook.yml