Ansible实战MySQL安装
先做准备工作,准备一台client,再装备一台master机器。
检查连通性:
-
[root@ansible-master ~]# ansible mytest -m ping
-
10.128.25.175| SUCCESS => {
-
"ansible_facts": {
-
"discovered_interpreter_python": "/usr/bin/python"
-
},
-
"changed": false,
-
"ping": "pong"
-
}
-
[root@ansible-master ~]#
一.Ansible模块查找使用方法
1.ansible的yum模块查找
yum模块在线文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html
示例:
-
- name: Install the latest version of Apache
-
yum:
-
name: httpd
-
state: latest
-
-
- name: Install a list of packages (suitable replacement for2.11 loop deprecation warning)
-
yum:
-
name:
-
- nginx
-
- postgresql
-
- postgresql-server
-
state: present
-
-
- name: Install a list of packages with a list variable
-
yum:
-
name: "{{ packages }}"
-
vars:
-
packages:
-
- httpd
-
- httpd-tools
-
-
- name: Remove the Apachepackage
-
yum:
-
name: httpd
-
state: absent
2.ansible systemd服务管理模块
systemd模块在线帮助:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html
示例:
-
- name: Make sure a service is running
-
systemd:
-
state: started
-
name: httpd
-
-
- name: Stop service cron on debian, if running
-
systemd:
-
name: cron
-
state: stopped
-
-
- name: Restart service cron on centos, in all cases, also issue daemon-reload to pick up config changes
-
systemd:
-
state: restarted
-
daemon_reload: yes
-
name: crond
-
-
- name: Reload service httpd, in all cases
-
systemd:
-
name: httpd
-
state: reloaded
-
-
- name: Enable service httpd andensure it isnot masked
-
systemd:
-
name: httpd
-
enabled: yes
-
masked: no
二.安装MySQL到测试机
-
[root@ansible-master ~]# ansible test -m yum -a "name=mariadb,mariadb-server state=present"
-
[root@ansible-master ~]#
三.管理测试机的MySQL服务
1.查看服务启动
-
[root@ansible-master ~]# ansible mytest -m shell -a "ps -ef | grep mysql |grep -v grep"
-
10.128.25.175| CHANGED | rc=0>>
-
mysql 67071010:58? 00:00:00/bin/sh /usr/bin/mysqld_safe --basedir=/usr
-
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数据库运行情况
-
[root@ansible-master ~]# ansible mytest -m systemd -a "name=mariadb state=started"
-
10.128.25.175| SUCCESS => {
-
"ansible_facts": {
-
"discovered_interpreter_python": "/usr/bin/python"
-
},
-
"changed": false,
-
"name": "mariadb",
-
"state": "started",
-
"status": {
-
"ActiveEnterTimestamp": "日 2020-09-27 10:58:54 CST",
-
"ActiveEnterTimestampMonotonic": "7928162332",
-
"ActiveExitTimestampMonotonic": "0",
-
"ActiveState": "active",
-
"After": "network.target tmp.mount -.mount systemd-journald.socket syslog.target basic.target system.slice",
-
"AllowIsolate": "no",
-
"AssertResult": "yes",
-
"AssertTimestamp": "日 2020-09-27 10:58:49 CST",
-
"AssertTimestampMonotonic": "7922958508",
-
"Before": "shutdown.target",
-
"BlockIOAccounting": "no",
-
"BlockIOWeight": "18446744073709551615",
-
"CPUAccounting": "no",
-
"CPUQuotaPerSecUSec": "infinity",
-
"CPUSchedulingPolicy": "0",
-
"CPUSchedulingPriority": "0",
-
"CPUSchedulingResetOnFork": "no",
-
"CPUShares": "18446744073709551615",
-
"CanIsolate": "no",
-
"CanReload": "no",
-
"CanStart": "yes",
-
"CanStop": "yes",
-
"CapabilityBoundingSet": "18446744073709551615",
-
"ConditionResult": "yes",
-
"ConditionTimestamp": "日 2020-09-27 10:58:49 CST",
-
"ConditionTimestampMonotonic": "7922958508",
-
"Conflicts": "shutdown.target",
-
"ControlGroup": "/system.slice/mariadb.service",
-
"ControlPID": "0",
-
"DefaultDependencies": "yes",
-
"Delegate": "no",
-
"Description": "MariaDB database server",
-
"DevicePolicy": "auto",
-
"ExecMainCode": "0",
-
"ExecMainExitTimestampMonotonic": "0",
-
"ExecMainPID": "6707",
-
"ExecMainStartTimestamp": "日 2020-09-27 10:58:50 CST",
-
"ExecMainStartTimestampMonotonic": "7924819268",
-
"ExecMainStatus": "0",
-
"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 }",
-
"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 }",
-
"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 }",
-
"FailureAction": "none",
-
"FileDescriptorStoreMax": "0",
-
"FragmentPath": "/usr/lib/systemd/system/mariadb.service",
-
"Group": "mysql",
-
"GuessMainPID": "yes",
-
"IOScheduling": "0",
-
"Id": "mariadb.service",
-
"IgnoreOnIsolate": "no",
-
"IgnoreOnSnapshot": "no",
-
"IgnoreSIGPIPE": "yes",
-
"InactiveEnterTimestampMonotonic": "0",
-
"InactiveExitTimestamp": "日 2020-09-27 10:58:49 CST",
-
"InactiveExitTimestampMonotonic": "7922964930",
-
"JobTimeoutAction": "none",
-
"JobTimeoutUSec": "0",
-
"KillMode": "control-group",
-
"KillSignal": "15",
-
"LimitAS": "18446744073709551615",
-
"LimitCORE": "18446744073709551615",
-
"LimitCPU": "18446744073709551615",
-
"LimitDATA": "18446744073709551615",
-
"LimitFSIZE": "18446744073709551615",
-
"LimitLOCKS": "18446744073709551615",
-
"LimitMEMLOCK": "65536",
-
"LimitMSGQUEUE": "819200",
-
"LimitNICE": "0",
-
"LimitNOFILE": "4096",
-
"LimitNPROC": "7175",
-
"LimitRSS": "18446744073709551615",
-
"LimitRTPRIO": "0",
-
"LimitRTTIME": "18446744073709551615",
-
"LimitSIGPENDING": "7175",
-
"LimitSTACK": "18446744073709551615",
-
"LoadState": "loaded",
-
"MainPID": "6707",
-
"MemoryAccounting": "no",
-
"MemoryCurrent": "18446744073709551615",
-
"MemoryLimit": "18446744073709551615",
-
"MountFlags": "0",
-
"Names": "mariadb.service",
-
"NeedDaemonReload": "no",
-
"Nice": "0",
-
"NoNewPrivileges": "no",
-
"NonBlocking": "no",
-
"NotifyAccess": "none",
-
"OOMScoreAdjust": "0",
-
"OnFailureJobMode": "replace",
-
"PermissionsStartOnly": "no",
-
"PrivateDevices": "no",
-
"PrivateNetwork": "no",
-
"PrivateTmp": "yes",
-
"ProtectHome": "no",
-
"ProtectSystem": "no",
-
"RefuseManualStart": "no",
-
"RefuseManualStop": "no",
-
"RemainAfterExit": "no",
-
"Requires": "basic.target -.mount",
-
"RequiresMountsFor": "/var/tmp",
-
"Restart": "no",
-
"RestartUSec": "100ms",
-
"Result": "success",
-
"RootDirectoryStartOnly": "no",
-
"RuntimeDirectoryMode": "0755",
-
"SameProcessGroup": "no",
-
"SecureBits": "0",
-
"SendSIGHUP": "no",
-
"SendSIGKILL": "yes",
-
"Slice": "system.slice",
-
"StandardError": "inherit",
-
"StandardInput": "null",
-
"StandardOutput": "journal",
-
"StartLimitAction": "none",
-
"StartLimitBurst": "5",
-
"StartLimitInterval": "10000000",
-
"StartupBlockIOWeight": "18446744073709551615",
-
"StartupCPUShares": "18446744073709551615",
-
"StatusErrno": "0",
-
"StopWhenUnneeded": "no",
-
"SubState": "running",
-
"SyslogLevelPrefix": "yes",
-
"SyslogPriority": "30",
-
"SystemCallErrorNumber": "0",
-
"TTYReset": "no",
-
"TTYVHangup": "no",
-
"TTYVTDisallocate": "no",
-
"TimeoutStartUSec": "5min",
-
"TimeoutStopUSec": "5min",
-
"TimerSlackNSec": "50000",
-
"Transient": "no",
-
"Type": "simple",
-
"UMask": "0022",
-
"UnitFilePreset": "disabled",
-
"UnitFileState": "disabled",
-
"User": "mysql",
-
"Wants": "system.slice",
-
"WatchdogTimestamp": "日 2020-09-27 10:58:50 CST",
-
"WatchdogTimestampMonotonic": "7924819302",
-
"WatchdogUSec": "0"
-
}
-
}
停止服务后再查看:
[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