python项目中通过环境变量的方式使用secret密钥参数(2)
- UID
- 1066743
|
python项目中通过环境变量的方式使用secret密钥参数(2)
在python项目中引用环境变量
参考python项目实现多环境配置,需要写settings文件以及引用pyhocon库。
default.conf中引用环境变量如下:
mongodb {
host = ${MONGO_HOST}
port = ${MONGO_PORT}
username = ${MONGO_USERNAME}
password = ${MONGO_PASSWORD}
}
mysql {
host = ${MYSQL_HOST}
port = ${MYSQL_PORT}
username = ${MYSQL_USERNAME}
password = ${MYSQL_PASSWORD}
}
则可在py文件中使用如下
def print_mongodb_detail():
host = config['mongodb.host']
port = config['mongodb.port']
username = config['mongodb.username']
password = config['mongodb.password']
print("mongodb"+" "+host+" "+port+" "+username+" "+password)
logger.info('The mongodb config is (%s,%s,%s,%s) ' %
(host, port, username, password))
def print_mysql_detail():
host = config['mysql.host']
port = config['mysql.port']
username = config['mysql.username']
password = config['mysql.password']
print("mysql"+" "+host + " " + port + " " + username + " " + password)
logger.info('The mysql config is (%s,%s,%s,%s) ' %
(host, port, username, password))
增加annotations注释说明
apiVersion: v1
kind: Secret
metadata:
name: mongodb-factory
annotations:
description: "factory mongodb(host 192.168.20.166 port 27017) username and password"
type: Opaque
data:
username: YW3343=
password: SW4243223 |
|
|
|
|
|