我在 ubuntu 中有一个 systemd 服务,我想检索环境部分中的秘密,而不是像这样硬编码:
Environment="PASSWORD=`aws secretsmanager get-secret-value --secret-id aws_pwds | jq -r '.SecretString | fromjson | .test_pwd'`"
命令在 shell 中工作,但服务无法获取任何建议,为什么? 我也尝试过使用 $()
你不能那样掏钱,而是需要做一个
ExecStartPre
行:
# Assign a value from aws secrets manager to PASSWORD environment variable
ExecStartPre=/bin/bash -c "/bin/systemctl set-environment PASSWORD=$(aws secretsmanager --region=eu-west-2 get-secret-value --secret-id aws_pwds | jq -r '.SecretString | fromjson | .test_pwd')"
# You can check it's exported in your env if you add this ExecStartPost
# but don't leave it in production 🙀
ExecStartPost=/bin/sh -c "env > /tmp/env.txt"