如何通过ansible-playbook创建新的系统服务

Zha*_* Yi 19 ansible ansible-playbook

我创建了一个脚本来启动/停止我的应用程序.现在我想将它添加为centos系统服务.首先,我创建了一个任务来创建从我的脚本到/etc/init.d/service_name的链接,如下所示.

---

- name: create startup link
  file: src={{ cooltoo_service_script }} dest={{ cooltoo_service_init }} state=link
Run Code Online (Sandbox Code Playgroud)

创建服务后,我想将其添加到系统服务.用于执行此操作的命令是"chkconfig --add service_name".我想知道是否有一个ansible模块来做,而不是在ansible-playbook文件中硬编码命令.我查看了这个页面http://docs.ansible.com/ansible/service_module.html但它只显示了如何管理服务而不是创建一个新服务.

小智 38

以下代码片段将在CentOS 7中创建服务.

任务

/tasks/main.yml

- name: TeamCity | Create environment file
  template: src=teamcity.env.j2 dest=/etc/sysconfig/teamcity
- name: TeamCity | Create Unit file
  template: src=teamcity.service.j2 dest=/lib/systemd/system/teamcity.service mode=644
  notify:
    - reload systemctl
- name: TeamCity | Start teamcity
  service: name=teamcity.service state=started enabled=yes
Run Code Online (Sandbox Code Playgroud)

模板

/templates/teamcity.service.j2

[Unit]
Description=JetBrains TeamCity
Requires=network.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/teamcity
ExecStart={{teamcity.installation_path}}/bin/teamcity-server.sh start
ExecStop={{teamcity.installation_path}}/bin/teamcity-server.sh stop
User=teamcity
PIDFile={{teamcity.installation_path}}/teamcity.pid
Environment="TEAMCITY_PID_FILE_PATH={{teamcity.installation_path}}/teamcity.pid"
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

\模板\ teamcity.env.j2

TEAMCITY_DATA_PATH="{{ teamcity.data_path }}"
Run Code Online (Sandbox Code Playgroud)

处理程序

\处理器\ main.yml

- name: reload systemctl
  command: systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

参考:

  • Ansible playbook结构:http://docs.ansible.com/ansible/playbooks_intro.html
  • SystemCtl:https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

  • 只是吹毛求疵,但根据 ArchLinux Wiki (https://wiki.archlinux.org/index.php/systemd#Writing_unit_files) `/etc/systemd/system/` 应该用于“系统管理员安装的单元”。而不是`(/usr)/lib/systemd/system/` (3认同)

Cam*_*err 17

'service'模块支持'enabled'参数.

这是剧本的一个示例部分,我将自由地承认它看起来像是一个新手尝试.这假定使用SysV的RHEL/CentOS 6.x,而不是systemd.

  - name: install rhel sysv supervisord init script
    copy: src=etc/rc.d/init.d/supervisord dest=/etc/rc.d/init.d/supervisord owner=root group=root mode=0755

  - name: install rhel sysv supervisord sysconfig
    copy: src=etc/sysconfig/supervisord dest=/etc/sysconfig/supervisord owner=root group=root mode=0640

  - name: enable sysv supervisord service
    service: name=supervisord enabled=yes

  - name: start supervisord
    service: name=supervisord state=started
Run Code Online (Sandbox Code Playgroud)

重要事项许多自定义初始化脚本将使用Ansible和SysV init失败; 原因是'status'选项(服务监督状态)需要返回符合LSB的返回码.否则,Ansible将不知道服务是启动还是关闭,并且幂等性将失败(重启仍然有效,因为这是无条件的)

这是脚本的一部分,我刚刚重写以利用/etc/init.d/functions中的'status'功能(你会注意到其他Red Hat提供的相同模式在/ etc /中提供了init脚本init.d中/

status)
    /usr/bin/supervisorctl $OPTIONS status
    status -p $PIDFILE supervisord
    # The 'status' option should return one of the LSB-defined return-codes,
    # in particular, return-code 3 should mean that the service is not
    # currently running. This is particularly important for Ansible's 'service'
    # module, as without this behaviour it won't know if a service is up or down.
    RETVAL=$?
    ;;
Run Code Online (Sandbox Code Playgroud)

参考:http://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html

如果请求了状态操作,则init脚本将返回以下退出状态代码.

0程序正在运行或服务正常1程序已死并且/ var /运行pid文件存在2程序已死并且/ var/lock锁定文件存在3程序未运行4程序或服务状态未知5-99保留供将来使用LSB使用100-149保留用于分发使用150-199保留用于应用程序200-254保留


udo*_*dan 5

service事实上,正如您所了解的,该模块仅管理已经注册的服务。据我所知,没有用于注册服务的模块。

您是否知道可以通过对 init.d 脚本进行一些修改来跳过此步骤?如果脚本遵循这些规则,您就可以使用该service模块来启用/启动服务。

  • 我已经有这样的脚本来启动/停止服务。我所要做的就是将其注册为系统服务。看来我得手动调用shell命令来注册服务了。 (2认同)