ansibleansible-vault

How to decrypt a 'clear text YAML file' with vaulted variable?


I'm using ansible 2.7.16.

The ansible documentation says:

Single Encrypted Variable

As of version 2.3, Ansible can now use a vaulted variable that lives in an otherwise ‘clear text’ YAML file:

notsecret: myvalue
mysecret: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          66386439653236336462626566653063336164663966303231363934653561363964363833313662
          6431626536303530376336343832656537303632313433360a626438346336353331386135323734
          62656361653630373231613662633962316233633936396165386439616533353965373339616234
          3430613539666330390a313736323265656432366236633330313963326365653937323833366536
          34623731376664623134383463316265643436343438623266623965636363326136
other_plain_text: othervalue

I have the following .yml file:

user: dbuser
pass: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          33633131346338633461336438656463643539396535656432306564636466353338373135346166
          3838313236383739616239333265323131376231656633350a613333613239646263393330353930
          31303935646330643831396130343031613063393839353433646338343034386432656435623934
          6537356530643136310a373835323666393337346562613831613962323261346232323331343631
          3838

I would like get a decrypted file, then I tried the command:

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

But I got the following error:

 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

ERROR! playbooks must be a list of plays

The error appears to have been in '/tmp/config.yml': line 1, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


user: dbuser
^ here

How could I get the .yml file with the variables decrypted ?


Solution

  • Q: "How could I get the .yml file decrypted ?"

    A: Simply use the file as any other file with variables. For example

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

    gives

    shell> ansible-playbook playbook.yml
    ...
        "pass": "password"
    

    A: Optionally decrypt the file the same way it was encrypted. For example

    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
    


    A: In a playbook, simply use it as any other file with variables. For example the playbook

    shell> cat playbook.yml
    - hosts: localhost
      tasks:
        - include_vars: conf.yml
        - debug:
            var: user
    

    gives

    shell> ansible-playbook playbook.yml
    ...
        "user": "dbuser"