欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

高級開發(fā)運維測試必須掌握的envsubst命令使用詳解

 更新時間:2023年04月13日 10:14:08   作者:爛筆頭  
這篇文章主要為大家介紹了高級開發(fā)運維測試必須掌握的envsubst命令使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

0 | 是什么

envsubst 縮寫來自于 environment variable substitution,即環(huán)境變量替換,是一個用于替換 shell 環(huán)境變量的工具。

它可以讀取輸入并在其中查找環(huán)境變量,然后將這些環(huán)境變量替換為其對應的值,最后輸出結(jié)果。在Linux和Unix操作系統(tǒng)上默認自帶。

通過這個命令,我們可以配置一些模板文件,然后通過定義環(huán)境變量的方式,將變量傳遞到模板文件中,從而動態(tài)地生成配置文件。在 linux 運維里,屬于比較高階但是很實用的一個命令。

1 | 怎么用

Usage: envsubst [OPTION] [SHELL-FORMAT]
Substitutes the values of environment variables.
Operation mode:
  -v, --variables             output the variables occurring in SHELL-FORMAT
Informative output:
  -h, --help                  display this help and exit
  -V, --version               output version information and exit
In normal operation mode, standard input is copied to standard output,
with references to environment variables of the form $VARIABLE or ${VARIABLE}
being replaced with the corresponding values.  If a SHELL-FORMAT is given,
only those environment variables that are referenced in SHELL-FORMAT are
substituted; otherwise all environment variables references occurring in
standard input are substituted.
When --variables is used, standard input is ignored, and the output consists
of the environment variables that are referenced in SHELL-FORMAT, one per line.

翻譯一下:

在正常操作模式下,標準輸入被復制到標準輸出,對 $VARIABLE 或 ${VARIABLE} 形式的環(huán)境變量的引用被替換為相應的值。如果給出了 SHELL-FORMAT,則僅替換那些在 SHELL-FORMAT 中引用的環(huán)境變量;否則,標準輸入中出現(xiàn)的所有環(huán)境變量引用都將被替換。

使用--variables時,標準輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成,每行一個。

2 | 舉例說明

首先,假設我有一個模板文件如下:

$ cat envsubst-template.yaml
apiVersion: v1
kind: Deploy
metadata:
  name: $name
  namespace: $ns

接著,分幾種 case 來使用 envsubst,以便于更好的理解。

  • case 1 | envsubst-template 從 envsubst 里拿變量 name 和 ns 的值,然后將替換后的 yaml 重定向到 envsubst-1.yaml 里
$ name='hello' ns='world' envsubst < envsubst-template.yaml > envsubst-1.yaml
$ cat envsubst-1.yaml
apiVersion: v1
kind: Deploy
metadata:
  name: hello
  namespace: world

注意, $ name='hello' ns='world' envsubst < envsubst-template.yaml > envsubst-1.yaml 也可以用export的方式定義:

$ export name='hello' 
$ export ns='world' 
$ envsubst < envsubst-template.yaml > envsubst-1.yaml
  • case 2 | 和 case 1 不同的地方是指定了 SHELL-FORMAT 為 $name, 意思是只將模板 yaml 里的 $name 變量用 envsubst 傳入的變量替換掉,其他($ns)保持不變
$ name='hello' ns='world' envsubst '$name'< envsubst-template.yaml > envsubst-2.yaml
$ cat envsubst-2.yaml 
apiVersion: v1
kind: Deploy
metadata:
  name: hello
  namespace: $ns
  • case 3 | 主要展示的是 --variables 這個 option 的作用:可以看到,當加上了這個變量后,標準輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成
$ name='hello' ns='world' envsubst --variables '$name'&lt; envsubst-template.yaml &gt; envsubst-3.yaml
$ cat envsubst-3.yaml 
name
  • case 4 | 主要展示的是 --variables 這個 option 的作用:可以看到,當加上了這個變量后,標準輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成, 每行一個
$ name='hello' ns='world' envsubst --variables '$name,$ns'< envsubst-template.yaml > envsubst-4.yaml
$ cat envsubst-4.yaml 
name
ns

以上就是高級開發(fā)運維測試必須掌握的envsubst命令使用詳解的詳細內(nèi)容,更多關于開發(fā)運維測試envsubst命令的資料請關注腳本之家其它相關文章!

相關文章

最新評論