1

What specific syntax must be used to get an Ansible playbook to successfully start a systemd service and make sure that the systemd service is running on an Amazon EC2 instance?

CURRENT SYNTAX:

The following syntax in an Ansible Playbook passes as successful when the playbook is run, but then we find that the myapp.service is subsequently not running when we putty into the EC2 instance to examine:

- name: create the myapp service file.
  shell:
    cmd: |
      cat << 'EOF' >> /usr/lib/systemd/system/myapp.service
      [Unit]
      Description=myapp service
      After=syslog.target
      After=network.target
      [Service]
      User=myapp-host
      Type=simple
      ExecStart=/bin/sh -c 'cd /home/myapp-host/myapp-files/ && /home/myapp-host/bin/bundle exec myapp serve --source /home/myapp-host/myapp-files/'
      Restart=always
      StandardOutput=syslog
      StandardError=syslog
      SyslogIdentifier=myapp
      [Install] 
      WantedBy=multi-user.target
      EOF
  args:
    executable: /bin/bash
  become: true
  become_user: root
  vars:
    ansible_become_password: "{{ root_pass_myapp }}"

- name: Make sure myapp service is running
  systemd:
    state: started
    name: myapp
  become: true
  become_method: sudo
  become_user: root
  vars:
    ansible_become_password: "{{ root_pass_myapp }}"  

FAILED CURRENT RESULTS:

Next, when we putty into the EC2 instance, we get the following results from systemctl status myapp.service :

[myapp-host@ip-12-3-4-56 ~]$ sudo systemctl status myapp.service
● myapp.service - myapp service
   Loaded: loaded (/usr/lib/systemd/system/myapp.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Wed 2020-10-21 22:33:25 UTC; 2min 10s ago
  Process: 19716 ExecStart=/bin/sh -c cd /home/myapp-host/myapp-files/ && /home/myapp-host/bin/bundle exec myapp serve --source /home/myapp-host/myapp-files/ (code=exited, status=127)
 Main PID: 19716 (code=exited, status=127)

Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: myapp.service: main process exited, code=exited, status=127/n/a
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: Unit myapp.service entered failed state.
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: myapp.service failed.
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: myapp.service holdoff time over, scheduling restart.
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: start request repeated too quickly for myapp.service
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: Failed to start myapp service.
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: Unit myapp.service entered failed state.
Oct 21 22:33:25 ip-12-3-4-56.aws-region-n.compute.internal systemd[1]: myapp.service failed.
[myapp-host@ip-12-3-4-56 ~]$  
3
  • I'm a bit confused as to why Ansible reports the service as started. However, you should start by checking the service on the EC2 end first. If you manually run sudo systemctl start myapp.service, do you see the same error? If so, you can investigate further with journalctl. (1/2)
    – Haxiel
    Oct 22, 2020 at 4:23
  • Going one step further, I'm guessing that you're running a Ruby app (based on the bundle exec serve syntax). In that case, this QA from ServerFault may be helpful. It looks like a possible issue with paths and/or environment variables. (2/2)
    – Haxiel
    Oct 22, 2020 at 4:25
  • Can we take this to chat? I've created new room here.
    – Haxiel
    Oct 23, 2020 at 4:04

1 Answer 1

0

The answer was very simple. Just add source /etc/profile to the ExecStart command as follows to resolve the error:

ExecStart=/bin/sh -c 'source /etc/profile && cd /home/myapp-host/myapp-files/ && /home/myapp-host/bin/bundle exec myapp serve --source /home/myapp-host/myapp-files/'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .