【问题标题】:ansible vault encrypt stringansible Vault 加密字符串
【发布时间】:2019-04-25 12:50:01
【问题描述】:

我正在尝试加密一些密码,但只想加密部分字符串而不是整个文件。因此,有一个命令 ansible-vault encrypt_string 为您提供加密输出,但是当我将其添加到我的 .yaml 剧本时,它无法解密。

用于加密简单密码的命令:

ansible-vault encrypt_string 'Test123!' --name 'ansible_password'

结果:

ansible_password: !vault | $ANSIBLE_VAULT;1.1;AES256 30333733643939646130396638646138636338636162316536313236666334656338306634353434 3132326265313639623039653261336265343733383730340a663565323932636138633365386332 36363534326263326633623238653464376637646632363839313464333830363436643561626534 6338613837393539350a383962663766373466376138376666393639373631313861663866333663 6137 加密成功

^

长字符串格式有点笨拙

所以我试图像这样将这个值放入我的剧本中:

---
- name: Copy needed files to target machine
  hosts: prod
  vars:
          ansible_user: test_admin
          ansible_password: !vault $ANSIBLE_VAULT;1.1;AES256;303337336439396461303966386461386363386361623165363132366663346563383066343534343132326265313639623039653261336265343733383730340a663565323932636138633365386332363635343262633266336232386534643766376466323638393134643338303634366435616265346338613837393539350a3839626637663734663761383766663936393736313138616638663336636137
          ansible_connection: winrm
          ansible_winrm_transport: credssp
          ansible_winrm_server_cert_validation: ignore


  tasks:
  - name: Copy test
    win_copy:
       src: /etc/winmachines/hosts
       dest: C:\test\

然后我想用命令执行剧本:

ansible-playbook copy.yaml -i hosts.ini

结果:

PLAY [Copy needed files to target machine] ********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************
fatal: [10.5.17.5]: FAILED! => {"msg": "Attempting to decrypt but no vault secrets found"}
        to retry, use: --limit @deleted

PLAY RECAP ****************************************************************************************************************************************
10.5.17.5                  : ok=0    changed=0    unreachable=0    failed=1

当我使用参数--ask-vault-password 运行剧本时:

ansible-playbook copy.yaml -i hosts.ini --ask-vault-pass

PLAY [Copy needed files to target machine] ********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************
 [WARNING]: There was a vault format error: Vault vaulttext format error: need more than 1 value to unpack

fatal: [10.5.17.5]: FAILED! => {"msg": "Vault vaulttext format error: need more than 1 value to unpack"}
        to retry, use: --limit @deleted

PLAY RECAP ****************************************************************************************************************************************
10.5.17.5                  : ok=0    changed=0    unreachable=0    failed=1

我尝试过以各种方式加密输出,但每次都因为这个问题或语法问题而失败。

当 Put in 从输出中传递时,我收到如下语法错误:

ERROR! Syntax Error while loading YAML.
  could not find expected ':'

The error appears to have been in : line 8, column 11, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

          $ANSIBLE_VAULT;1.1;AES256
          34323264326266303364656561616663306566356636616238623931613032343131643839336338
          ^ here

当所有内容都在同一行时,删除“|”收到此错误:

TASK [Gathering Facts] ****************************************************************************************************************************
fatal: [10.5.17.5]: FAILED! => {"msg": "AES256 343232643262663033646565616166633065663566366162386239316130323431316438393363383331346231343361386432666461646639386135666335380a373862336531613034393262653432313038303432636536313139353932356637343139393938666536383061383233613136643063353761386339323562640a3963306632393865613237666364386566623938356465336363613163646338 cipher could not be found"}

有什么想法吗?

【问题讨论】:

    标签: ansible ansible-vault


    【解决方案1】:

    您的格式不正确。 ansible-vault encrypt_string 'Test123!' --name 'ansible_password' 给出的格式是你应该使用的。

    供参考, https://github.com/ansible/ansible/blob/devel/lib/ansible/parsing/vault/init.py#L153 。 Vault 解析器正在寻找 $ANSIBLE_VAULT;1.1;AES256(或类似的;vaulttext_envelope)位于 第一行,由 splitlines() 确定。删除换行符打破了这种预期。

    encrypt_string 的输出中,语法完全正确,可以粘贴到您的剧本中。 |(管道字符)在 yaml 中有特殊的含义。它是一个保留换行符的多行块。它应该是该行的最后一个字符。使用多行块时保持一致的缩进。因为余数是joined on https://github.com/ansible/ansible/blob/devel/lib/ansible/parsing/vault/init.py#L165,所以您不需要在密文本身中保留新行。

    这意味着以下任何一个都应该起作用:

    ansible_password: !vault |
        $ANSIBLE_VAULT;1.1;AES256
        30333733643939646130396638646138636338636162316536313236666334656338306634353434
        3132326265313639623039653261336265343733383730340a663565323932636138633365386332
        36363534326263326633623238653464376637646632363839313464333830363436643561626534
        6338613837393539350a383962663766373466376138376666393639373631313861663866333663
        6137
    
    ansible_password: !vault |
        $ANSIBLE_VAULT;1.1;AES256
        303337336439396461303966386461386363386361623165363132366663346563383066343534343132326265313639623039653261336265343733383730340a663565323932636138633365386332363635343262633266336232386534643766376466323638393134643338303634366435616265346338613837393539350a3839626637663734663761383766663936393736313138616638663336636137
    
    ansible_password: !vault "$ANSIBLE_VAULT;1.1;AES256\r\n303337336439396461303966386461386363386361623165363132366663346563383066343534343132326265313639623039653261336265343733383730340a663565323932636138633365386332363635343262633266336232386534643766376466323638393134643338303634366435616265346338613837393539350a3839626637663734663761383766663936393736313138616638663336636137"
    

    注意在最后一个例子中,我没有使用管道,\r\n 是换行符。

    另请参考:https://yaml-multiline.info/

    为清楚起见添加完整示例:

    ---
    - name: Copy needed files to target machine
      hosts: prod
      vars:
        ansible_user: test_admin
        ansible_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256;
          30333733643939646130396638646138636338636162316536313236666334656338306634353434
          30333733643939646130396638646138636338636162316536313236666334656338306634353434
          3132326265313639623039653261336265343733383730340a663565323932636138633365386332
          36363534326263326633623238653464376637646632363839313464333830363436643561626534
          6338613837393539350a383962663766373466376138376666393639373631313861663866333663
          6137
        
        ## Notice indentation above
        ## the block started with | will end when indentation changes.
        ansible_connection: winrm
        ansible_winrm_transport: credssp
        ansible_winrm_server_cert_validation: ignore
    

    顺便说一句,选择制表符或空格,但不要同时使用。我一般推荐空格,但对于 python 和 yaml 更是如此。如果您的编辑器/ide 不做语法高亮,您应该切换到一个做的。并获得某种 yaml linter。 yamllint.com 在短期内可以工作,但从长远来看,你应该得到一些自动化的东西并内置到你的编辑器中。

    【讨论】:

    • 感谢您的重播,我已经尝试了两种方法,但是我收到如下语法错误:错误!加载 YAML 时出现语法错误。找不到预期的“:”错误似乎出现在:第 8 行,第 11 列,但可能在文件中的其他位置,具体取决于确切的语法问题。违规行似乎是: $ANSIBLE_VAULT;1.1;AES256 34323264326266303364656561616663306566356636616238623931613032343131643839336338 ^ 这里
    • 我已经更新了我的帖子,以便您可以更好地看到错误输出
    • 该错误意味着您的 yaml 在 ansible 到达之前不正确。 Yaml 对间距非常敏感。从错误中猜测,在第 6 行,您应该有 ansible_password: !vault |。第 7 行应该是$ANSIBLE_VAULT;1.1;AES256。第 7 行必须比第 6 行进一步缩进。机密的所有行必须缩进与第 7 行相同的级别。以正确的间距查看整个文件会很有帮助。请查阅本指南并使用“代码围栏”或类似工具来帮助在发布时保持正确的代码格式。 stackoverflow.com/editing-help#comment-formatting
    • 谢谢,问题出在空格上!
    猜你喜欢
    • 2022-11-18
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多