簡體   English   中英

如何在分子 group_vars 中指定加密字符串? (無法確定標簽 '!vault' 的構造函數)

[英]How do I specify encrypted strings in molecule group_vars? (could not determine a constructor for the tag '!vault')

我的角色的var/main.yml文件中有這些加密的字符串...

---
# vars 
wiki_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          63343237 ... omitted for brevity ...

...一切正常。 但是我被告知我不需要在my_role/vars/main.yml文件中定義它,而是在 groups_vars 中定義它。 所以對於我的單元測試,我更新了我的molecule/defaults/molecule.yml文件:

... omitted ...

provisioner:
  name: ansible
  inventory:
    group_vars:
      all:
        ... other vars ...
        web_domain: "example.com"

        wiki_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          63343237 ... omitted for brevity ...    
          
... omitted ...

但是現在當我運行molecule test時,我得到了這個錯誤:

yaml.constructor.ConstructorError: could not determine a constructor for the tag '!vault'
  in "<unicode string>", line 50, column 28:
            wiki_password: !vault |

vault是 Ansible 特征,而不是分子特征。 Molecule 需要加載並驗證其配置文件,當它到達您的保險庫字符串時,ansible 尚未發揮作用。

解決上述問題的一種方法是使用供應商配置中的links功能(請參閱ansible 供應商文檔)。 在這種情況下,變量只會在 ansible 啟動時被讀取,而不是在 molecule 試圖創建相應的清單文件時讀取。

例如,這是我剛剛從頭開始進行的測試:

  • 用分子初始化一個角色
molecule init role acme.so_demo -d docker
cd so_demo
  • 創建庫存組文件
mkdir -p molecule/.inventory/group_vars/
echo -en "---\ntoto: $(ansible-vault encrypt_string --encrypt-vault-id your_id some_value)" > molecule/.inventory/group_vars/all.yml

結果在molecule/.inventory/group_vars/all.yml

---
toto: !vault |
          $ANSIBLE_VAULT;1.2;AES256;your_id
          38323137303132393932623963326164643834386333626166633734653338313331303331313638
          ...
  • 編輯供應商配置。 這就是我的molecule/default/molecule.yml的樣子:
---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance
    image: quay.io/centos/centos:stream8
    pre_build_image: true
provisioner:
  name: ansible
  inventory:
    links:
      group_vars: ../.inventory/group_vars/
verifier:
  name: ansible
  • tasks/main.yml中添加一個虛擬任務
---
- name: Debug vault var
  ansible.builtin.debug:
    var: toto

您現在可以運行示例

molecule converge

其中給出(刪節):

PLAY [Converge] ****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [instance]

TASK [Include acme.so_demo] ****************************************************

TASK [acme.so_demo : Debug vault var] ******************************************
ok: [instance] => {
    "toto": "some_value"
}

PLAY RECAP *********************************************************************
instance                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM