systemd サービスを正常に開始し、systemd サービスが Amazon EC2 インスタンスで実行されていることを確認するには、Ansible プレイブックにどの特定の構文を使用する必要がありますか?
現在の構文:
Ansible プレイブックの次の構文は、プレイブックの実行時に正常に合格しましたが、後でmyapp.service
検査のためにEC2インスタンスに入れたときに実行されないことがわかりました。
- 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 }}"
現在の失敗の結果:
次に、EC2インスタンスに入ると、次のような結果が得られます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 ~]$
答え1
答えは簡単です。エラーを解決するには、source /etc/profile
コマンドに以下を追加するだけです。ExecStart
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/'