利用playbook安装nginx服务
[root@myserver_1 ansible]# vim install_nginx.yml
---
- hosts: webservers
remote_user: root
#收集信息取消
gather_facts: no
tasks:
- name: add group nginx
user: name=nginx state=present
- name: add user nginx
user: name=nginx state=present group=nginx
- name: install nginx
yum: name=nginx state=present
- name: start nginx
service: name=nginx state=started enabled=yes
检查语法正确性和执行语句
[root@myserver_1 ansible]# ansible-playbook -C install_nginx.yml
[root@myserver_1 ansible]# ansible-playbook install_nginx.yml
[root@myserver_1 ansible]# ansible dbservers -m shell -a 'ss -ntl'
利用playbook安装mysql
下载mysql-5.6.46-linux-glibc2.12到ansible所在的服务器上
[root@myserver_1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
--2021-01-04 00:12:08-- https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 343184888 (327M) [application/x-gzip]
Saving to: ‘mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz’
100%[===================================================>] 343,184,888 10.0MB/s in 30s
2021-01-04 00:12:39 (10.8 MB/s) - ‘mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz’ saved [343184888/343184888]
[root@myserver_1 ~]# ls -lrt
total 392360
-rw-r--r-- 1 root root 28296 Jan 10 2018 percona-zabbix-templates-1.1.8-1.noarch.rpm
-rw-r--r-- 1 root root 58546654 Aug 15 2019 grafana-6.3.3-1.x86_64.rpm
-rw-r--r-- 1 root root 343184888 Jun 2 2020 mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
-rw-------. 1 root root 2246 Dec 23 22:08 anaconda-ks.cfg
-rw-r--r--. 1 root root 2277 Dec 23 22:11 initial-setup-ks.cfg
drwxr-xr-x 2 root root 70 Jan 3 23:28 ansible
编辑mysql的所需要的配置文件和安全加固设置文件
[root@myserver_1 ~]#
移动到指定路径下
[root@myserver_1 ansible]# mkdir -p /data/ansible/files
[root@myserver_1 ansible]# cd /data/ansible/files/
[root@myserver_1 files]# mv mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz /data/ansible/files
#mysql的配置文件设置
[root@myserver_1 files]# vim my.cnf
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
#mysql安全加固设置
[root@myserver_1 files]# vim secure_mysql.sh
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF
y
test123
test123
y
y
y
y
EOF
#目录下的结构
[root@myserver_1 files]# tree
.
├── my.cnf
├── mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
└── secure_mysql.sh
0 directories, 3 files
[root@myserver_1 files]#
编辑install_mysql.yml
[root@myserver_1 files]# vim /root/ansible/install_mysql.yml
- hosts: dbsrvs
remote_user: root
gather_facts: no
tasks:
- name: install packages
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
- name: create mysql group
group: name=mysql gid=306
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
- name: copy tar to remote host and file mode
unarchive: src=/data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
- name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: data dir
shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
tags: data
- name: config my.cnf
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: enable service
shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
tags: service
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: secure script
script: /data/ansible/files/secure_mysql.sh
tags: script
检查语法并执行
[root@myserver_1 ansible]# ansible-playbook -C install_mysql.yml
执行脚本
报错信息如下
[root@myserver_1 ansible]# ansible-playbook install_mysql.yml
PLAY [dbservers] ********************************************************************************************************************************************
TASK [install packages] *************************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql group] ***********************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql user] ************************************************************************************************************************************
ok: [192.168.1.40]
TASK [copy tar to remote host and file mode] ****************************************************************************************************************
ok: [192.168.1.40]
TASK [create linkfile /usr/local/mysql] ********************************************************************************************************************
ok: [192.168.1.40]
TASK [data dir] *********************************************************************************************************************************************
fatal: [192.168.1.40]: FAILED! => {"changed": true, "cmd": "./scripts/mysql_install_db --datadir=/data/mysql --user=mysql", "delta": "0:00:00.030984", "end": "2021-01-04 01:13:43.883524", "msg": "non-zero return code", "rc": 1, "start": "2021-01-04 01:13:43.852540", "stderr": "", "stderr_lines": [], "stdout": "FATAL ERROR: The parent directory for the data directory '/data/mysql' does not exist.\nIf that path was really intended, please create that directory path and then\nrestart this script.\nIf some other path was intended, please use the correct path when restarting this script.", "stdout_lines": ["FATAL ERROR: The parent directory for the data directory '/data/mysql' does not exist.", "If that path was really intended, please create that directory path and then", "restart this script.", "If some other path was intended, please use the correct path when restarting this script."]}
PLAY RECAP **************************************************************************************************************************************************
192.168.1.40 : ok=5 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[root@myserver_1 ansible]#
在远程主机上新建/data目录再执行
[root@myserver_1 ansible]# ansible-playbook install_mysql.yml
PLAY [dbservers] ********************************************************************************************************************************************
TASK [install packages] *************************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql group] ***********************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql user] ************************************************************************************************************************************
ok: [192.168.1.40]
TASK [copy tar to remote host and file mode] ****************************************************************************************************************
ok: [192.168.1.40]
TASK [create linkfile /usr/local/mysql] ********************************************************************************************************************
ok: [192.168.1.40]
TASK [data dir] *********************************************************************************************************************************************
changed: [192.168.1.40]
TASK [config my.cnf] ****************************************************************************************************************************************
changed: [192.168.1.40]
TASK [service script] ***************************************************************************************************************************************
changed: [192.168.1.40]
TASK [enable service] ***************************************************************************************************************************************
changed: [192.168.1.40]
TASK [PATH variable] ****************************************************************************************************************************************
changed: [192.168.1.40]
TASK [secure script] ****************************************************************************************************************************************
changed: [192.168.1.40]
PLAY RECAP **************************************************************************************************************************************************
192.168.1.40 : ok=11 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@myserver_1 ansible]#
远程主机mysql验证
mysql安装检查
[root@myserver_3 ~]# mysql -uroot -ptest123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.49-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.49-log |
+------------+
1 row in set (0.00 sec)
mysql>
ansible的roles目录编排