前期的Ansible的安装与配置和前一篇描述的一样,因此这一篇从上传Ansible脚本与执行开始;

上传脚本:

[root@oracle ~]# cd /software
[root@oracle software]#
[root@oracle software]# ls -ltr
total 329756
-rw-r--r-- 1 root root 337669864 Nov 26 23:43 ansible-database-mysql.zip
[root@oracle software]#
[root@oracle software]# unzip ansible-database-mysql.zip
Archive:  ansible-database-mysql.zip
   creating: ansible-database-mysql/
   creating: ansible-database-mysql/inventory/
   creating: ansible-database-mysql/inventory/group_vars/
  inflating: ansible-database-mysql/inventory/group_vars/all
  inflating: ansible-database-mysql/inventory/group_vars/all.bak
  inflating: ansible-database-mysql/inventory/hosts
   creating: ansible-database-mysql/playbooks/
  inflating: ansible-database-mysql/playbooks/deploy_database_mysql.yml
   creating: ansible-database-mysql/playbooks/roles/
   creating: ansible-database-mysql/playbooks/roles/linux/
   creating: ansible-database-mysql/playbooks/roles/linux/files/
   creating: ansible-database-mysql/playbooks/roles/linux/files/yum/
  inflating: ansible-database-mysql/playbooks/roles/linux/files/yum/CentOS-7.repo
   creating: ansible-database-mysql/playbooks/roles/linux/tasks/
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/linux_directory.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/linux_hosts.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/linux_kernel.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/linux_security.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/linux_user_group.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/linux_yum.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/main.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/tasks/main.yml.bak
   creating: ansible-database-mysql/playbooks/roles/linux/templates/
   creating: ansible-database-mysql/playbooks/roles/linux/templates/etc/
  inflating: ansible-database-mysql/playbooks/roles/linux/templates/etc/hosts.j2
  inflating: ansible-database-mysql/playbooks/roles/linux/templates/etc/hosts.j2.bak
   creating: ansible-database-mysql/playbooks/roles/linux/vars/
  inflating: ansible-database-mysql/playbooks/roles/linux/vars/main.yml
  inflating: ansible-database-mysql/playbooks/roles/linux/vars/main.yml.bak
   creating: ansible-database-mysql/playbooks/roles/mysql/
   creating: ansible-database-mysql/playbooks/roles/mysql/files/
   creating: ansible-database-mysql/playbooks/roles/mysql/files/install_media/
  inflating: ansible-database-mysql/playbooks/roles/mysql/files/install_media/Percona-Server-5.7.31-34-r2e68637-el7-x86_64-bundle.tar
   creating: ansible-database-mysql/playbooks/roles/mysql/files/yum/
  inflating: ansible-database-mysql/playbooks/roles/mysql/files/yum/mysql.repo
   creating: ansible-database-mysql/playbooks/roles/mysql/tasks/
  inflating: ansible-database-mysql/playbooks/roles/mysql/tasks/main.yml
  inflating: ansible-database-mysql/playbooks/roles/mysql/tasks/main.yml.bak
  inflating: ansible-database-mysql/playbooks/roles/mysql/tasks/mysql_install.yml
   creating: ansible-database-mysql/playbooks/roles/mysql/templates/
   creating: ansible-database-mysql/playbooks/roles/mysql/templates/conf/
  inflating: ansible-database-mysql/playbooks/roles/mysql/templates/conf/my.cnf.57.j2
  inflating: ansible-database-mysql/playbooks/roles/mysql/templates/conf/my.cnf.80.j2
   creating: ansible-database-mysql/playbooks/roles/mysql/templates/yum/
  inflating: ansible-database-mysql/playbooks/roles/mysql/templates/yum/mysql.repo.j2
   creating: ansible-database-mysql/playbooks/roles/mysql/vars/
  inflating: ansible-database-mysql/playbooks/roles/mysql/vars/main.yml
  inflating: ansible-database-mysql/playbooks/roles/mysql/vars/main.yml.bak
[root@oracle software]#
[root@oracle software]# ls -ltr
total 329756
-rw-r--r-- 1 root root 337669864 Nov 26 23:43 ansible-database-mysql.zip
drwxr-xr-x 4 root root        40 Nov 27  2021 ansible-database-mysql
[root@oracle software]#

测试连通性:

[root@oracle software]# cat ansible-database-mysql/inventory/hosts
[mysql]
127.0.0.1
[root@oracle software]#
[root@oracle software]# ansible -i ansible-database-mysql/inventory/hosts all -m ping
127.0.0.1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@oracle software]#

准备安装:

[root@oracle software]# id mysql
id: mysql: no such user
[root@oracle software]#
[root@oracle software]# ls -ltr / | grep data
[root@oracle software]#
[root@oracle software]# ansible-playbook -i ansible-database-mysql/inventory/hosts ansible-database-mysql/playbooks/deploy_database_mysql.yml

PLAY [linux] ************************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [127.0.0.1]

TASK [linux : account: group] *******************************************************************************
ok: [127.0.0.1] => (item={u'name': u'oracle', u'id': u'2000'})
ok: [127.0.0.1] => (item={u'name': u'oinstall', u'id': u'2001'})
ok: [127.0.0.1] => (item={u'name': u'oper', u'id': u'2002'})
ok: [127.0.0.1] => (item={u'name': u'dba', u'id': u'2003'})
changed: [127.0.0.1] => (item={u'name': u'mysql', u'id': u'2004'})

TASK [linux : account: user] ********************************************************************************
changed: [127.0.0.1] => (item={u'comment': u'Oracle Database OS User', u'shell': u'/bin/bash', u'group': u'oracle', u'name': u'oracle', u'id': u'2000', u'groups': [u'oinstall', u'oper', u'dba'], u'password': u'oracle', u'dir_home': u'/home/oracle'})
changed: [127.0.0.1] => (item={u'comment': u'MySQL Database OS User', u'shell': u'/bin/bash', u'group': u'mysql', u'name': u'mysql', u'id': u'2004', u'groups': [u'mysql'], u'password': u'oracle', u'dir_home': u'/home/mysql'})

TASK [linux : direcotry] ************************************************************************************
ok: [127.0.0.1] => (item={u'owner': u'root', u'path': u'/software', u'group': u'root', u'mode': u'0755'})
ok: [127.0.0.1] => (item={u'owner': u'root', u'path': u'/iso', u'group': u'root', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'root', u'path': u'/data', u'group': u'root', u'mode': u'0755'})
ok: [127.0.0.1] => (item={u'owner': u'root', u'path': u'/temp_me', u'group': u'root', u'mode': u'0755'})
ok: [127.0.0.1] => (item={u'owner': u'root', u'path': u'/coding', u'group': u'root', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/db', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/log', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/binlog', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/slowlog', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/tmp', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/redo', u'group': u'mysql', u'mode': u'0755'})
changed: [127.0.0.1] => (item={u'owner': u'mysql', u'path': u'/data/mysql/undo', u'group': u'mysql', u'mode': u'0755'})

TASK [linux : /etc/hosts] ***********************************************************************************
changed: [127.0.0.1]

TASK [linux : /etc/sysctl.conf] *****************************************************************************
changed: [127.0.0.1] => (item={u'key': u'net.core.rmem_max', u'value': 4194304})
changed: [127.0.0.1] => (item={u'key': u'net.core.rmem_default', u'value': 262144})
changed: [127.0.0.1] => (item={u'key': u'fs.file-max', u'value': 6815744})
changed: [127.0.0.1] => (item={u'key': u'fs.aio-max-nr', u'value': 1048576})
changed: [127.0.0.1] => (item={u'key': u'kernel.sem', u'value': u'250 32000 100 128'})
changed: [127.0.0.1] => (item={u'key': u'net.core.wmem_default', u'value': 262144})
changed: [127.0.0.1] => (item={u'key': u'kernel.shmall', u'value': 2097152})
changed: [127.0.0.1] => (item={u'key': u'net.core.wmem_max', u'value': 1048576})
changed: [127.0.0.1] => (item={u'key': u'kernel.shmmax', u'value': 4294967295})
changed: [127.0.0.1] => (item={u'key': u'kernel.shmmni', u'value': 4096})
changed: [127.0.0.1] => (item={u'key': u'vm.overcommit_memory', u'value': 1})
changed: [127.0.0.1] => (item={u'key': u'net.ipv4.ip_local_port_range', u'value': u'9000 65500'})

TASK [linux : /etc/security/limits.conf] ********************************************************************
changed: [127.0.0.1] => (item={u'dest': u'/etc/security/limits.conf', u'domain': u'oracle', u'limit_item': u'stack', u'value': u'10240', u'limit_type': u'soft'})
changed: [127.0.0.1] => (item={u'dest': u'/etc/security/limits.conf', u'domain': u'oracle', u'limit_item': u'stack', u'value': u'10240', u'limit_type': u'hard'})
changed: [127.0.0.1] => (item={u'dest': u'/etc/security/limits.conf', u'domain': u'oracle', u'limit_item': u'nofile', u'value': u'1024', u'limit_type': u'soft'})
changed: [127.0.0.1] => (item={u'dest': u'/etc/security/limits.conf', u'domain': u'oracle', u'limit_item': u'nofile', u'value': u'65536', u'limit_type': u'hard'})
changed: [127.0.0.1] => (item={u'dest': u'/etc/security/limits.conf', u'domain': u'oracle', u'limit_item': u'nproc', u'value': u'2047', u'limit_type': u'soft'})
changed: [127.0.0.1] => (item={u'dest': u'/etc/security/limits.conf', u'domain': u'oracle', u'limit_item': u'nproc', u'value': u'16384', u'limit_type': u'hard'})

TASK [linux : Linux hugepage] *******************************************************************************
changed: [127.0.0.1] => (item=echo never > /sys/kernel/mm/transparent_hugepage/enabled)
changed: [127.0.0.1] => (item=echo -e '
# disable Transparent Hugepage.
echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local)

TASK [linux : Linux pam.d] **********************************************************************************
changed: [127.0.0.1]

TASK [linux : firewalld] ************************************************************************************
ok: [127.0.0.1]

TASK [selinux: /etc/selinux/config] *************************************************************************
ok: [127.0.0.1]

TASK [selinux: disable] *************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "setenforce 0", "delta": "0:00:00.009451", "end": "2021-11-26 23:52:02.360987", "msg": "non-zero return code", "rc": 1, "start": "2021-11-26 23:52:02.351536", "stderr": "setenforce: SELinux is disabled", "stderr_lines": ["setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [selinux: check status] ********************************************************************************
changed: [127.0.0.1]

TASK [selinux: show status] *********************************************************************************
ok: [127.0.0.1] => {
    "msg": "SELinux status:                 disabled"
}

TASK [linux : /etc/yum.conf] ********************************************************************************
changed: [127.0.0.1]

TASK [linux : YUM Repo File] ********************************************************************************
changed: [127.0.0.1] => (item={u'dest': u'/etc/yum.repos.d/CentOS-7.repo', u'src': u'yum/CentOS-7.repo'})

TASK [linux : YUM makecache] ********************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'.  If you need to use command because yum
is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [127.0.0.1]

TASK [linux : YUM install] **********************************************************************************
changed: [127.0.0.1]

PLAY [mysql] ************************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [127.0.0.1]

TASK [mysql : MySQL install media] **************************************************************************
changed: [127.0.0.1] => (item=install_media/Percona-Server-5.7.31-34-r2e68637-el7-x86_64-bundle.tar)

TASK [mysql : MySQL createrepo] *****************************************************************************
changed: [127.0.0.1]

TASK [mysql : MySQL yum repo file] **************************************************************************
changed: [127.0.0.1]

TASK [mysql : MySQL cache yum] ******************************************************************************
changed: [127.0.0.1]

TASK [mysql : MySQL install rpm] ****************************************************************************
changed: [127.0.0.1]

TASK [mysql : MySQL config] *********************************************************************************
changed: [127.0.0.1]

TASK [mysql : MySQL service] ********************************************************************************
changed: [127.0.0.1]

PLAY RECAP **************************************************************************************************
127.0.0.1                  : ok=26   changed=21   unreachable=0    failed=0    skipped=0    rescued=0    ignored=1

[root@oracle software]#
[root@oracle software]# id mysql
uid=2004(mysql) gid=2004(mysql) groups=2004(mysql)
[root@oracle software]#
[root@oracle software]# ls -ltr / | grep data
drwxr-xr-x    3 root root   19 Nov 26 23:51 data
[root@oracle software]#
[root@oracle software]# du -sh /data/mysql/
3.1G    /data/mysql/
[root@oracle software]#
[root@oracle software]# ps -ef | grep mysql
mysql     25722      1  1 23:57 ?        00:00:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root      25792  17513  0 23:59 pts/0    00:00:00 grep --color=auto mysql
[root@oracle software]#
[root@oracle software]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-11-26 23:57:37 PST; 1min 36s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 25719 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 25644 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 25722 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─25722 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Nov 26 23:57:14 oracle systemd[1]: Starting MySQL Server...
Nov 26 23:57:37 oracle systemd[1]: Started MySQL Server.
[root@oracle software]#

可以看到,已经自动安装好了,并且MySQL的服务也启动了。


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

隐藏
变装