如何使用存储变量解密“明文 YAML 文件”?

Kle*_*ios 6 ansible ansible-vault

我正在使用 ansible 2.7.16。

\n\n

ansible 文档说:

\n\n
Single Encrypted Variable\n\nAs of version 2.3, Ansible can now use a vaulted variable that lives in an otherwise \xe2\x80\x98clear text\xe2\x80\x99 YAML file:\n\nnotsecret: myvalue\nmysecret: !vault |\n          $ANSIBLE_VAULT;1.1;AES256\n          66386439653236336462626566653063336164663966303231363934653561363964363833313662\n          6431626536303530376336343832656537303632313433360a626438346336353331386135323734\n          62656361653630373231613662633962316233633936396165386439616533353965373339616234\n          3430613539666330390a313736323265656432366236633330313963326365653937323833366536\n          34623731376664623134383463316265643436343438623266623965636363326136\nother_plain_text: othervalue\n
Run Code Online (Sandbox Code Playgroud)\n\n

我有以下 .yml 文件:

\n\n
user: dbuser\npass: !vault |\n          $ANSIBLE_VAULT;1.1;AES256\n          33633131346338633461336438656463643539396535656432306564636466353338373135346166\n          3838313236383739616239333265323131376231656633350a613333613239646263393330353930\n          31303935646330643831396130343031613063393839353433646338343034386432656435623934\n          6537356530643136310a373835323666393337346562613831613962323261346232323331343631\n          3838\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想获得解密的文件,然后我尝试了以下命令:

\n\n

ansible-playbook --vault-password-file pass.txt config.yml

\n\n

但我收到以下错误:

\n\n
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source\n\n [WARNING]: No inventory was parsed, only implicit localhost is available\n\n [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'\n\nERROR! playbooks must be a list of plays\n\nThe error appears to have been in '/tmp/config.yml': line 1, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nuser: dbuser\n^ here\n
Run Code Online (Sandbox Code Playgroud)\n\n

如何获取变量已解密的 .yml 文件?

\n

Vla*_*tka 1

问:“如何解密 .yml 文件?”

答:只需像使用任何其他带有变量的文件一样使用该文件即可。例如

shell> ansible-vault encrypt_string 'password' --name 'pass'
pass: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          65303631663061316538623639316439366538386430656239383735353237343762346364653230
          3163643637333966643762383733633465353065333564310a303762343732613363313864646661
          66633539363865386362613362663238353664356439386431303065646530666562326662356439
          3032313564373364360a623830613763616635383633363631356535316162393138373336386534
          3835
Run Code Online (Sandbox Code Playgroud)
shell> cat conf1.yml 
pass: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          65303631663061316538623639316439366538386430656239383735353237343762346364653230
          3163643637333966643762383733633465353065333564310a303762343732613363313864646661
          66633539363865386362613362663238353664356439386431303065646530666562326662356439
          3032313564373364360a623830613763616635383633363631356535316162393138373336386534
          3835
Run Code Online (Sandbox Code Playgroud)
shell> cat playbook.yml
- hosts: localhost
  tasks:
    - include_vars: conf1.yml
    - debug:
        var: pass
Run Code Online (Sandbox Code Playgroud)

给出

shell> ansible-playbook playbook.yml
...
    "pass": "password"
Run Code Online (Sandbox Code Playgroud)

答:可以选择以相同的方式编辑decrypt文件。例如encrypt

shell> cat conf.yml 
user: dbuser

shell> ansible-vault encrypt conf.yml 
Encryption successful

shell> cat conf.yml 
$ANSIBLE_VAULT;1.1;AES256
63313762343630623364653737643462373034653762616333663330613039623534633030666135
6633343263666465356537316430623834386130626231310a376639356234336664386239336461
31313935613565656639653532613639396536326662346234373563663065643564373939316539
3430643635623339390a393139326337306363623565356439626430643161356266323832313461
3633

shell> ansible-vault decrypt conf.yml 
Decryption successful

shell> cat conf.yml 
user: dbuser
Run Code Online (Sandbox Code Playgroud)


答:在剧本中,只需将其用作任何其他带有变量的文件即可。例如剧本

shell> cat playbook.yml
- hosts: localhost
  tasks:
    - include_vars: conf.yml
    - debug:
        var: user
Run Code Online (Sandbox Code Playgroud)

给出

shell> ansible-playbook playbook.yml
...
    "user": "dbuser"
Run Code Online (Sandbox Code Playgroud)

  • 嗨弗拉基米尔。我没有加密整个文件。我只加密变量值 `ansible-vault encrypt_string 'string' --name 'variable_name` (2认同)