Ansible实战MySQL安装

先做准备工作,准备一台client,再装备一台master机器。

检查连通性:

  1. [root@ansible-master ~]# ansible mytest -m ping

  2. 10.128.25.175| SUCCESS => {

  3. "ansible_facts": {

  4. "discovered_interpreter_python": "/usr/bin/python"

  5. },

  6. "changed": false,

  7. "ping": "pong"

  8. }

  9. [root@ansible-master ~]#

一.Ansible模块查找使用方法

1.ansible的yum模块查找

yum模块在线文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html

示例:

  1. - name: Install the latest version of Apache

  2. yum:

  3. name: httpd

  4. state: latest

  5.  

  6. - name: Install a list of packages (suitable replacement for2.11 loop deprecation warning)

  7. yum:

  8. name:

  9. - nginx

  10. - postgresql

  11. - postgresql-server

  12. state: present

  13.  

  14. - name: Install a list of packages with a list variable

  15. yum:

  16. name: "{{ packages }}"

  17. vars:

  18. packages:

  19. - httpd

  20. - httpd-tools

  21.  

  22. - name: Remove the Apachepackage

  23. yum:

  24. name: httpd

  25. state: absent

2.ansible systemd服务管理模块

systemd模块在线帮助:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html

示例:

  1. - name: Make sure a service is running

  2. systemd:

  3. state: started

  4. name: httpd

  5.  

  6. - name: Stop service cron on debian, if running

  7. systemd:

  8. name: cron

  9. state: stopped

  10.  

  11. - name: Restart service cron on centos, in all cases, also issue daemon-reload to pick up config changes

  12. systemd:

  13. state: restarted

  14. daemon_reload: yes

  15. name: crond

  16.  

  17. - name: Reload service httpd, in all cases

  18. systemd:

  19. name: httpd

  20. state: reloaded

  21.  

  22. - name: Enable service httpd andensure it isnot masked

  23. systemd:

  24. name: httpd

  25. enabled: yes

  26. masked: no

二.安装MySQL到测试机

  1. [root@ansible-master ~]# ansible test -m yum -a "name=mariadb,mariadb-server state=present"

  2. [root@ansible-master ~]#

三.管理测试机的MySQL服务

1.查看服务启动

  1. [root@ansible-master ~]# ansible mytest -m shell -a "ps -ef | grep mysql |grep -v grep"

  2. 10.128.25.175| CHANGED | rc=0>>

  3. mysql 67071010:58? 00:00:00/bin/sh /usr/bin/mysqld_safe --basedir=/usr

  4. mysql 68696707010:58? 00:00:00/usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

2.查看mariadb数据库运行情况

  1. [root@ansible-master ~]# ansible mytest -m systemd -a "name=mariadb state=started"

  2. 10.128.25.175| SUCCESS => {

  3. "ansible_facts": {

  4. "discovered_interpreter_python": "/usr/bin/python"

  5. },

  6. "changed": false,

  7. "name": "mariadb",

  8. "state": "started",

  9. "status": {

  10. "ActiveEnterTimestamp": "日 2020-09-27 10:58:54 CST",

  11. "ActiveEnterTimestampMonotonic": "7928162332",

  12. "ActiveExitTimestampMonotonic": "0",

  13. "ActiveState": "active",

  14. "After": "network.target tmp.mount -.mount systemd-journald.socket syslog.target basic.target system.slice",

  15. "AllowIsolate": "no",

  16. "AssertResult": "yes",

  17. "AssertTimestamp": "日 2020-09-27 10:58:49 CST",

  18. "AssertTimestampMonotonic": "7922958508",

  19. "Before": "shutdown.target",

  20. "BlockIOAccounting": "no",

  21. "BlockIOWeight": "18446744073709551615",

  22. "CPUAccounting": "no",

  23. "CPUQuotaPerSecUSec": "infinity",

  24. "CPUSchedulingPolicy": "0",

  25. "CPUSchedulingPriority": "0",

  26. "CPUSchedulingResetOnFork": "no",

  27. "CPUShares": "18446744073709551615",

  28. "CanIsolate": "no",

  29. "CanReload": "no",

  30. "CanStart": "yes",

  31. "CanStop": "yes",

  32. "CapabilityBoundingSet": "18446744073709551615",

  33. "ConditionResult": "yes",

  34. "ConditionTimestamp": "日 2020-09-27 10:58:49 CST",

  35. "ConditionTimestampMonotonic": "7922958508",

  36. "Conflicts": "shutdown.target",

  37. "ControlGroup": "/system.slice/mariadb.service",

  38. "ControlPID": "0",

  39. "DefaultDependencies": "yes",

  40. "Delegate": "no",

  41. "Description": "MariaDB database server",

  42. "DevicePolicy": "auto",

  43. "ExecMainCode": "0",

  44. "ExecMainExitTimestampMonotonic": "0",

  45. "ExecMainPID": "6707",

  46. "ExecMainStartTimestamp": "日 2020-09-27 10:58:50 CST",

  47. "ExecMainStartTimestampMonotonic": "7924819268",

  48. "ExecMainStatus": "0",

  49. "ExecStart": "{ path=/usr/bin/mysqld_safe ; argv[]=/usr/bin/mysqld_safe --basedir=/usr ; ignore_errors=no ; start_time=[日 2020-09-27 10:58:50 CST] ; stop_time=[n/a] ; pid=6707 ; code=(null) ; status=0/0 }",

  50. "ExecStartPost": "{ path=/usr/libexec/mariadb-wait-ready ; argv[]=/usr/libexec/mariadb-wait-ready $MAINPID ; ignore_errors=no ; start_time=[日 2020-09-27 10:58:50 CST] ; stop_time=[日 2020-09-27 10:58:54 CST] ; pid=6708 ; code=exited ; status=0 }",

  51. "ExecStartPre": "{ path=/usr/libexec/mariadb-prepare-db-dir ; argv[]=/usr/libexec/mariadb-prepare-db-dir %n ; ignore_errors=no ; start_time=[日 2020-09-27 10:58:49 CST] ; stop_time=[日 2020-09-27 10:58:50 CST] ; pid=6624 ; code=exited ; status=0 }",

  52. "FailureAction": "none",

  53. "FileDescriptorStoreMax": "0",

  54. "FragmentPath": "/usr/lib/systemd/system/mariadb.service",

  55. "Group": "mysql",

  56. "GuessMainPID": "yes",

  57. "IOScheduling": "0",

  58. "Id": "mariadb.service",

  59. "IgnoreOnIsolate": "no",

  60. "IgnoreOnSnapshot": "no",

  61. "IgnoreSIGPIPE": "yes",

  62. "InactiveEnterTimestampMonotonic": "0",

  63. "InactiveExitTimestamp": "日 2020-09-27 10:58:49 CST",

  64. "InactiveExitTimestampMonotonic": "7922964930",

  65. "JobTimeoutAction": "none",

  66. "JobTimeoutUSec": "0",

  67. "KillMode": "control-group",

  68. "KillSignal": "15",

  69. "LimitAS": "18446744073709551615",

  70. "LimitCORE": "18446744073709551615",

  71. "LimitCPU": "18446744073709551615",

  72. "LimitDATA": "18446744073709551615",

  73. "LimitFSIZE": "18446744073709551615",

  74. "LimitLOCKS": "18446744073709551615",

  75. "LimitMEMLOCK": "65536",

  76. "LimitMSGQUEUE": "819200",

  77. "LimitNICE": "0",

  78. "LimitNOFILE": "4096",

  79. "LimitNPROC": "7175",

  80. "LimitRSS": "18446744073709551615",

  81. "LimitRTPRIO": "0",

  82. "LimitRTTIME": "18446744073709551615",

  83. "LimitSIGPENDING": "7175",

  84. "LimitSTACK": "18446744073709551615",

  85. "LoadState": "loaded",

  86. "MainPID": "6707",

  87. "MemoryAccounting": "no",

  88. "MemoryCurrent": "18446744073709551615",

  89. "MemoryLimit": "18446744073709551615",

  90. "MountFlags": "0",

  91. "Names": "mariadb.service",

  92. "NeedDaemonReload": "no",

  93. "Nice": "0",

  94. "NoNewPrivileges": "no",

  95. "NonBlocking": "no",

  96. "NotifyAccess": "none",

  97. "OOMScoreAdjust": "0",

  98. "OnFailureJobMode": "replace",

  99. "PermissionsStartOnly": "no",

  100. "PrivateDevices": "no",

  101. "PrivateNetwork": "no",

  102. "PrivateTmp": "yes",

  103. "ProtectHome": "no",

  104. "ProtectSystem": "no",

  105. "RefuseManualStart": "no",

  106. "RefuseManualStop": "no",

  107. "RemainAfterExit": "no",

  108. "Requires": "basic.target -.mount",

  109. "RequiresMountsFor": "/var/tmp",

  110. "Restart": "no",

  111. "RestartUSec": "100ms",

  112. "Result": "success",

  113. "RootDirectoryStartOnly": "no",

  114. "RuntimeDirectoryMode": "0755",

  115. "SameProcessGroup": "no",

  116. "SecureBits": "0",

  117. "SendSIGHUP": "no",

  118. "SendSIGKILL": "yes",

  119. "Slice": "system.slice",

  120. "StandardError": "inherit",

  121. "StandardInput": "null",

  122. "StandardOutput": "journal",

  123. "StartLimitAction": "none",

  124. "StartLimitBurst": "5",

  125. "StartLimitInterval": "10000000",

  126. "StartupBlockIOWeight": "18446744073709551615",

  127. "StartupCPUShares": "18446744073709551615",

  128. "StatusErrno": "0",

  129. "StopWhenUnneeded": "no",

  130. "SubState": "running",

  131. "SyslogLevelPrefix": "yes",

  132. "SyslogPriority": "30",

  133. "SystemCallErrorNumber": "0",

  134. "TTYReset": "no",

  135. "TTYVHangup": "no",

  136. "TTYVTDisallocate": "no",

  137. "TimeoutStartUSec": "5min",

  138. "TimeoutStopUSec": "5min",

  139. "TimerSlackNSec": "50000",

  140. "Transient": "no",

  141. "Type": "simple",

  142. "UMask": "0022",

  143. "UnitFilePreset": "disabled",

  144. "UnitFileState": "disabled",

  145. "User": "mysql",

  146. "Wants": "system.slice",

  147. "WatchdogTimestamp": "日 2020-09-27 10:58:50 CST",

  148. "WatchdogTimestampMonotonic": "7924819302",

  149. "WatchdogUSec": "0"

  150. }

  151. }

停止服务后再查看:

[root@ansible-master ~]# ansible mytest -m shell -a "ps -ef | grep mysql |grep -v grep"
10.128.25.175| FAILED | rc=1>>
non-zero return code
[root@ansible-master ~]#

原文地址:https://mp.weixin.qq.com/s/0WtWBxjppGj3Kf9R6arbhw

posted @ 2022-08-14 22:05  北极之光的博客  阅读(258)  评论(0编辑  收藏  举报