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)
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保留
归档时间: |
|
查看次数: |
41111 次 |
最近记录: |