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/'