ansible生產環境使用場景(四):encrypt_string加密和ansible-lint調試

前言:html

​ 有時須要對yaml文件中的某些敏感字段進行加密,這時就須要‘ansible-vault encrypt_string ’加密字符串,在使用過程當中發現報錯:Vault format unhexlify error: Odd-length string fatal,使用ansible-lint工具進行調試排查錯誤,本文記錄了在使用過程當中報錯及解決的詳細過程。bash

環境說明:服務器

主機名 操做系統版本 ip ansible version 備註
ansible Centos 7.6.1810 172.27.34.51 2.9.9 ansible管理服務器
test85 Centos 7.6.1810 172.27.34.85 / 被管服務器

1、encrypt_string

1.原始yaml文件

---
- hosts: test85
  gather_facts: false
  vars:
    test_user: "testuser"
    test_passwd: "test123"
  tasks:
  - name: echo user and passwd
    debug:
      msg: "user is {{ test_user }},passwd is {{ test_passwd }}"

用戶名爲testuser,密碼爲test123ide

2.對test_passwd字段加密

[root@ansible yaml]# more encrypt_string.txt 
abc123
[root@ansible yaml]# ansible-vault encrypt_string --vault-id encrypt_test@encrypt_string.txt  --name password test123
password: !vault |
          $ANSIBLE_VAULT;1.2;AES256;encrypt_test
          32623436636338383062356661633433613439333566356263643065306463326130323035366630
          3635643531303466356463383537373131383336666533370a386265616664616130613464343632
          61623332303433396663346563316236666239303862646632393565626364626238343638396535
          6134616331363463320a623538336336393232303039353261643261336337373366353838336165
          3832
Encryption successful

image-20200814172827953

使用‘ansible-vault encrypt_string’對密碼字段test123加密;‘--vault-id’指定加解密字符串test123的密碼爲文件‘encrypt_string.txt’即abc123,後面運行encrypt_string.yaml會用到;‘encrypt_test’爲加密test123的提示符;‘--name password’指定密碼變量名爲password。工具

3.修改yaml並運行

[root@ansible yaml]# more encrypt_string.yaml 
---
- hosts: test85
  gather_facts: false
  vars:
    test_user: "testuser"
    test_passwd: !vault |
          $ANSIBLE_VAULT;1.2;AES256;encrypt_test
          32623436636338383062356661633433613439333566356263643065306463326130323035366630
          3635643531303466356463383537373131383336666533370a386265616664616130613464343632
          61623332303433396663346563316236666239303862646632393565626364626238343638396535
          6134616331363463320a623538336336393232303039353261643261336337373366353838336165
          3832 
  tasks:
  - name: echo user and passwd
    debug:
      msg: "user is {{ test_user }},passwd is {{ test_passwd }}"

將生成的密文替換test123並運行加密

[root@ansible yaml]# ansible-playbook encrypt_string.yaml --vault-id encrypt_string.txt

image-20200814173145998

‘--vault-id’指定運行encrypt_string.yaml的密碼文件爲encrypt_string.txt。spa

運行後發現報錯: FAILED! => {"msg": "Vault format unhexlify error: Odd-length string"}操作系統

2、ansible-lint

ansible-lint是用於檢測playbook的命令行工具,可用於yaml文件和role的語法檢查。命令行

1.安裝ansible-lint

[root@ansible ansible]# pip install ansible-lint

image-20200814173800909

2.檢查yaml

[root@ansible yaml]# ansible-lint  encrypt_string.yaml 
[201] Trailing whitespace
encrypt_string.yaml:12
          3832

image-20200814173852383

提示第12行字符3832後面有空格debug

3.修復yaml並從新運行

按提示發現字符3832後面確實存在空格,刪除並從新運行。

[root@ansible yaml]# ansible-playbook encrypt_string.yaml --vault-id encrypt_string.txt

image-20200814174245348

修復後運行結果符合預期。

 

結論:FAILED! => {"msg": "Vault format unhexlify error: Odd-length string"}該報錯由密碼文件後面的空格引發,能夠使用ansible-lint工具進行檢查並及時修復。

 

 

更多請點擊:ansible系列文章

相關文章
相關標籤/搜索