
ansible service模块主要用于远程客户端各种服务管理,包括启动、停止、重启、重新加载等,service模块使用详解:enabled 是否开启启动服务;值:true|false name 服务名称; runlevel 服务启动级别; arguments 服务命令行参数传递; state 服务操作状态,状态包括:started、stopped、restarted、reloaded1.ansible service模块操作-远程重启keepalived服务ansible all -m service -a "name=keepalived state=restarted"Ansible Server[root@cent79-2 ~]# ansible all -m service -a "name=keepalived state=restarted" 192.168.10.100 | CHANGED = { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "keepalived", "state": "started", "status": { "ActiveEnterTimestamp": "Thu 2026-06-04 09:52:16 CST", "ActiveEnterTimestampMonotonic": "3530246701", "ActiveExitTimestamp": "Thu 2026-06-04 09:52:15 CST", "ActiveExitTimestampMonotonic": "3529234222", "ActiveState": "active", "After": "syslog.target systemd-journald.socket system.slice network-online.target basic.target", "AllowIsolate": "no", "AmbientCapabilities": "0", "AssertResult": "yes", "AssertTimestamp": "Thu 2026-06-04 09:52:16 CST", "AssertTimestampMonotonic": "3530242873", "Before": "multi-user.target shutdown.target", "BlockIOAccounting": "no", "BlockIOWeight": "18446744073709551615", "CPUAccounting": "no", "CPUQuotaPerSecUSec": "infinity", "CPUSchedulingPolicy": "0", "CPUSchedulingPriority": "0", "CPUSchedulingResetOnFork": "no", "CPUShares": "18446744073709551615", "CanIsolate": "no", "CanReload": "yes", "CanStart": "yes", "CanStop": "yes", "CapabilityBoundingSet": "18446744073709551615", "ConditionResult": "yes", "ConditionTimestamp": "Thu 2026-06-04 09:52:16 CST", "ConditionTimestampMonotonic": "3530242873", "Conflicts": "shutdown.target", "ControlGroup": "/system.slice/keepalived.service", "ControlPID": "0", "DefaultDependencies": "yes", "Delegate": "no", "Description": "LVS and VRRP High Availability Monitor", "DevicePolicy": "auto", "Documentation": "man:keepalived(8) man:keepalived.conf(5) man:genhash(1) https://keepalived.org", "EnvironmentFile": "/usr/local/keepalived/etc/sysconfig/keepalived (ignore_errors=yes)", "ExecMainCode": "0", "ExecMainExitTimestampMonotonic": "0", "ExecMainPID": "16128", "ExecMainStartTimestamp": "Thu 2026-06-04 09:52:16 CST", "ExecMainStartTimestampMonotonic": "3530246651", "ExecMainStatus": "0", "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "ExecStart": "{ path=/usr/local/keepalived/sbin/keepalived ; argv[]=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS ; ignore_errors=no ; start_time=[Thu 2026-06-04 09:52:16 CST] ; stop_time=[Thu 2026-06-04 09:52:16 CST] ; pid=16127 ; code=exited ; status=0 }", "FailureAction": "none", "FileDescriptorStoreMax": "0", "FragmentPath": "/usr/lib/systemd/system/keepalived.service", "GuessMainPID": "yes", "IOScheduling": "0", "Id": "keepalived.service", "IgnoreOnIsolate": "no", "IgnoreOnSnapshot": "no", "IgnoreSIGPIPE": "yes", "InactiveEnterTimestamp": "Thu 2026-06-04 09:52:16 CST", "InactiveEnterTimestampMonotonic": "3530242547", "InactiveExitTimestamp": "Thu 2026-06-04 09:52:16 CST", "InactiveExitTimestampMonotonic": "3530243485", "JobTimeoutAction": "none", "JobTimeoutUSec": "0", "KillMode": "process", "KillSignal": "15", "LimitAS": "18446744073709551615", "LimitCORE": "18446744073709551615", "LimitCPU": "18446744073709551615", "LimitDATA": "18446744073709551615", "LimitFSIZE": "18446744073709551615", "LimitLOCKS": "18446744073709551615", "LimitMEMLOCK": "65536", "LimitMSGQUEUE": "819200", "LimitNICE": "0", "LimitNOFILE": "4096", "LimitNPROC": "7850", "LimitRSS": "18446744073709551615", "LimitRTPRIO": "0", "LimitRTTIME": "18446744073709551615", "LimitSIGPENDING": "7850", "LimitSTACK": "18446744073709551615", "LoadState": "loaded", "MainPID": "16128", "MemoryAccounting": "no", "MemoryCurrent": "18446744073709551615", "MemoryLimit": "18446744073709551615", "MountFlags": "0", "Names": "keepalived.service", "NeedDaemonReload": "no", "Nice": "0", "NoNewPrivileges": "no", "NonBlocking": "no", "NotifyAccess": "none", "OOMScoreAdjust": "0", "OnFailureJobMode": "replace", "PIDFile": "/run/keepalived.pid", "PermissionsStartOnly": "no", "PrivateDevices": "no", "PrivateNetwork": "no", "PrivateTmp": "no", "ProtectHome": "no", "ProtectSystem": "no", "RefuseManualStart": "no", "RefuseManualStop": "no", "RemainAfterEx