Search code examples
jirajinja2salt-project

Managing service with Salt when service has random name


I'm trying to get salt to install and manage Jira. As part of the install, Jira installs itself as a service. The problem is, it picks a random service name during the install process.

I need to be able to manage the service state. I found the service.get_service_name() function, that let's me look up a service by display name. I'm unsure how to properly use this in my state. When I place it in the state, salt errors because it tries to get the service name before jira is installed and the service exists.

I've tried rearranging things to get install to happen before managing the state, but everything I try causes the error. This is my last attempt:

jira/init.sls

jira:
  pkg.installed:
    - refresh: true
    - require:
      - file: C:\Users\ADMINI~1\AppData\Local\Temp\response.varfile

C:\Users\ADMINI~1\AppData\Local\Temp\response.varfile:
  file.managed:
  - source: salt://jira/response.varfile

jira/jira_service.sls

include:
  - jira

jira_service:
  service.running:
    - name: {{ salt['service.get_service_name']('Atlassian JIRA')['Atlassian JIRA'] }}
    - require:
      - pkg: jira

If I manually install jira, everything works fine. How do I force jira to install before salt tries to parse the service section?


Solution

  • The problem you're running into is that jinja renders the files to yaml first, then the Salt State compiler evaluates all the individual states. So, indeed, it tries to get the service name before the jira service is installed.

    One workaround might be to have jinja not render the jira_service section if jira hasn't been installed yet.

    {% if '<Jira name>' in salt['pkg.list_pkgs'] %}
    jira_service:
      service.running:
        - name: {{ salt['service.get_service_name']('Atlassian JIRA')['Atlassian JIRA'] }}
        - require:
          - pkg: jira
    {% endif %}
    

    Replace with the name you expect to be in 'pkg.list_pkgs'. The drawback of this attempt will be that the first highstate won't ensure the service is running. The second highstate should work because Jira is now installed and Salt can find the package name.