ansible 之 service

service

用途:控制远程主机上的服务。对于Windows可以使用win_service,模块

官方说明

[root@ansible ~]# ansible-doc service
> SERVICE    (/usr/lib/python2.7/site-packages/ansible-2.8.0.dev0-py2.7.egg/ansible/modules/system/service.py)

        Controls services on remote hosts. Supported init systems include BSD init,
        OpenRC, SysV, Solaris SMF, systemd, upstart. For Windows targets, use the
        [win_service] module instead.

  * This module is maintained by The Ansible Core Team
  * note: This module has a corresponding action plugin.

OPTIONS (= is mandatory):

- arguments
        Additional arguments provided on the command line
        (Aliases: args)[Default: (null)]

- enabled
        Whether the service should start on boot. *At least one of state and enabled are
        required.*
        [Default: (null)]
        type: bool

= name
        Name of the service.


- pattern
        If the service does not respond to the status command, name a substring to look
        for as would be found in the output of the `ps' command as a stand-in for a
        status result.  If the string is found, the service will be assumed to be
        started.
        [Default: (null)]
        version_added: 0.7

- runlevel
        For OpenRC init scripts (ex: Gentoo) only.  The runlevel that this service
        belongs to.
        [Default: default]

- sleep
        If the service is being `restarted' then sleep this many seconds between the
        stop and start command. This helps to workaround badly behaving init scripts
        that exit immediately after signaling a process to stop.
        [Default: (null)]
        version_added: 1.3

- state
        `started'/`stopped' are idempotent actions that will not run commands unless
        necessary.  `restarted' will always bounce the service.  `reloaded' will always
        reload. *At least one of state and enabled are required.* Note that reloaded
        will start the service if it is not already started, even if your chosen init
        system wouldn't normally.
        (Choices: reloaded, restarted, started, stopped)[Default: (null)]

- use
        The service module actually uses system specific modules, normally through auto
        detection, this setting can force a specific module.
        Normally it uses the value of the 'ansible_service_mgr' fact and falls back to
        the old 'service' module when none matching is found.
        [Default: auto]
        version_added: 2.2
        


NOTES:
      * For Windows targets, use the [win_service] module instead.

AUTHOR: Ansible Core Team, Michael DeHaan
        METADATA:
          status:
          - stableinterface
          supported_by: core
        

EXAMPLES:
- name: Start service httpd, if not started
  service:
    name: httpd
    state: started

- name: Stop service httpd, if started
  service:
    name: httpd
    state: stopped

- name: Restart service httpd, in all cases
  service:
    name: httpd
    state: restarted

- name: Reload service httpd, in all cases
  service:
    name: httpd
    state: reloaded

- name: Enable service httpd, and not touch the state
  service:
    name: httpd
    enabled: yes

- name: Start service foo, based on running process /usr/bin/foo
  service:
    name: foo
    pattern: /usr/bin/foo
    state: started

- name: Restart network service for interface eth0
  service:
    name: network
    state: restarted
    args: eth0

参数解释

  • arguments
    默认值:null
    在命令行中提供额外的参数

  • enabled
    默认值:null
    类型:bool
    设置服务是否开机自启

  • name
    服务名称

  • pattern
    默认值:null

  • runlevel
    默认值:default

  • sleep
    默认值:null
    如果服务正在restarted,此值设置 stopstart的间隔时间

  • state
    默认值:null
    选项: reloaded, restarted, started, stopped
    选择服务状态

  • use
    默认值:auto

posted @ 2018-12-06 14:45  McSiberiaWolf  阅读(193)  评论(0编辑  收藏  举报