How to create new system service by ansible-playbook

The 'service' module supports an 'enabled' argument.

Here's an example part of a playbook, which I will freely admit does look like a newbie attempt. This assumes RHEL/CentOS 6.x, which uses SysV, not 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

IMPORTANT A lot of custom init scripts WILL FAIL with Ansible and SysV init; the reason being that the 'status' option (service supervisord status) needs to a return an LSB-compliant return code. Otherwise, Ansible will not know if a service is up or down, and idempotency will fail (restart will still work because that is unconditional)

Here's part of a script, which I've just rewritten to make use of the 'status' function within /etc/init.d/functions (you'll notice this same pattern in other Red Hat provided init-scripts in /etc/init.d/

    /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.


If the status action is requested, the init script will return the following exit status codes.

0 program is running or service is OK 1 program is dead and /var/run pid file exists 2 program is dead and /var/lock lock file exists 3 program is not running 4 program or service status is unknown 5-99 reserved for future LSB use 100-149 reserved for distribution use 150-199 reserved for application use 200-254 reserved

Indeed the service module only manages already registered services as you have figured out. To my knowledge there is no module to register a service.

Are you aware this step can be skipped with some modifications to your init.d script? If the script follows those rules you can just use the service module to enable/start the service.

The below code snippet will create Service in CentOS 7.




- 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
    - reload systemctl
- name: TeamCity | Start teamcity
  service: name=teamcity.service state=started enabled=yes



Description=JetBrains TeamCity
ExecStart={{teamcity.installation_path}}/bin/ start
ExecStop={{teamcity.installation_path}}/bin/ stop


TEAMCITY_DATA_PATH="{{ teamcity.data_path }}"



- name: reload systemctl
  command: systemctl daemon-reload

Reference :

  • Ansible playbook structure:
  • SystemCtl: