I have been using ansible for a while to install and configure several services, but this is the first time I am trying to set up a service that can have multiple instances running at the same time. I'm using a template for the service file, something like this:
[Unit]
Description=My Service
[Service]
WorkingDirectory=/var/lib/service/api
ExecStart=/usr/bin/python -O /var/lib/service/api/main.py -f /var/lib/service/api/cfg/service_api.cfg -p {{ port }}
Type=simple
And then my role for installing and configuring this service contains tasks like this:
- name: Configure api.service
template: src=api.service.j2 dest=/etc/systemd/system/api@{{port}}.service
- name: Start service
systemd: name=api@{{port}} state=restarted enabled=yes
Right now this is working for something like port = 80, but I don't think I made it generic enough. I don't fully understand how to create these instance services. How can I modify the service file and the role such that each listening port can have its own service file? Should I use the "Wants" keyword, or something like "WantedBy=multi-user.target"?