robertdebock.service

Ansible Role Service

This role allows you to add custom services to your Linux system.

Platform Link
GitHub GitHub
GitLab GitLab
Downloads Downloads
Version Version

Example Playbook

The following example is sourced from molecule/default/converge.yml and is verified with every update.

---
- name: Converge
  hosts: all
  become: true
  gather_facts: true

  vars:
    _service_test_command:
      default: /usr/bin/sleep
      Alpine: /bin/sleep
      Debian: /bin/sleep
      Ubuntu-16: /bin/sleep
      Ubuntu-18: /bin/sleep
    service_test_command: "{{ _service_test_command[ansible_distribution ~ '-' ~ ansible_distribution_major_version] | default(_service_test_command[ansible_os_family] | default(_service_test_command['default'])) }}" 

  roles:
    - role: robertdebock.service
      service_list:
        - name: simple-service
          description: Simple Service
          start_command: "{{ service_test_command }} 3600"
          state: started
          enabled: true
        - name: stopped-service
          description: Simple Service
          start_command: "{{ service_test_command }} 3601"
          state: stopped
          enabled: false
        - name: specific-stop-service
          description: Specific Stop Service
          start_command: "{{ service_test_command }} 1440"
          stop_command: /usr/bin/killall -f "sleep 1440"
        - name: specific-user-group-service
          description: Specific User Group Service
          start_command: "{{ service_test_command }} 28800"
          user_name: root
          group_name: root
        - name: specific-workingdirectory-service
          description: Specific Working Directory Service
          start_command: "{{ service_test_command }} 57600"
          working_directory: /tmp
        - name: specific-pattern-service
          description: Specific Status Pattern Service
          start_command: "{{ service_test_command }} 115200"
          status_pattern: 115200
        - name: variable-service
          description: Service with environment variables
          start_command: "{{ service_test_command }} ${time}"
          environment_variables:
            time: 230400
        - name: pidfile-service
          description: Service with pidfile
          start_command: "{{ service_test_command }} 460800"
          pidfile: /var/run/pidfile-service.pid
        - name: environmentfile-service
          description: Service with environmentfile
          start_command: "{{ service_test_command }} 921600"
          environmentfile: /environmentfile.txt

Make sure to prepare the system first. In CI, this can be done using molecule/default/prepare.yml:

---
- name: Prepare
  hosts: all
  become: true
  gather_facts: false
  serial: 30%

  roles:
    - role: robertdebock.bootstrap

  post_tasks:
    - name: Place /environmentfile.txt
      ansible.builtin.copy:
        content: "value=variable"
        dest: /environmentfile.txt
        mode: "0644"

For more details and examples, check out the complete guide on using these roles.

Role Variables

By default, variable values are defined in defaults/main.yml:

---
# defaults file for service

# service_list can include multiple services to add to the system.
# Required fields for each service:
# - name: The short name for the service, e.g., "tomcat".
# - description: A more detailed name, e.g., "Tomcat application server".
# - start_command: Command to start the service,
#   e.g., "/usr/local/bin/java -jar some.jar"
# Optional fields include:
# - stop_command: A custom command to stop the service if needed.
# - status_pattern: The program or pattern to check for service status.
# - type: Describes how the service starts; "simple" or "forking".
# - working_directory: Directory to operate in before starting the service.
# - environment_variables: A list for defining variables. For example:
#   environment_variables:
#     variable1: value1
#     variable2: value2
# - after: Ensures this service starts after a specified service.
# - restart_mode: Defines how to handle restarts, e.g., "always".
# - restart_seconds: Time allowed for a restart to finish.
service_list: []

Requirements

State of Used Roles

The following roles help set up the system. You can use alternative preparations if you prefer.

Requirement GitHub GitLab
robertdebock.bootstrap Build Status GitHub Build Status GitLab

Context

This role is part of numerous compatible roles. For more information, check the documentation of these roles.

Here’s an overview of related roles: dependencies

Compatibility

This role has been tested on the following container images:

Container Tags
EL 9
Debian all
Fedora all
Ubuntu all

The minimum required version of Ansible is 2.12, and testing has been done on:

  • The previous version.
  • The current version.
  • The development version.

If you encounter any issues, please report them on GitHub.

License

Apache-2.0.

Author Information

robertdebock

Please consider sponsoring me.

Informazioni sul progetto

Add custom services to your Linux system.

Installa
ansible-galaxy install robertdebock.service
Licenza
apache-2.0
Download
351.7k
Proprietario
I know my way around (Linux) infrastructure, have a passion for automation, Docker, Ansible, Molecule and ci/cd.