天天看點

Ansible下的機密檔案管理

1、為什麼要加密

當在使用ansible時,會涉及到一些敏感性的檔案的操作,但實際需求中,我們并不希望ansible的使用者可以對該檔案進行檢視,故需要對一些檔案在ansible的使用過程中加密,以保證系統資料的安全性。

2、如何加密

ansible中的加密工具:ansible-vualt

加密方式:ansible-vualt使用的加密方式是在python中內建的,而非自己的加密方式。

2.1 建立加密檔案:

#建立加密檔案,産生新檔案

[[email protected] min]# ansible-vault create secret.yml
New Vault password:                    ##輸入該加密檔案的密碼
Confirm New Vault password: 

#建立加密檔案的同時,将密碼儲存

[[email protected] min]# cat serpass  ##用檔案進行加密時,需将密碼提前寫入
linux
[[email protected] min]# ansible-vault create --vault-password-file=serpass secretp.yml
           

【注】用問價加密時,檔案中的字元串均為加密密碼

2.2 對原有檔案進行加密:

#對現有檔案加密
[[email protected] min]# ll
total 12
-rw-r--r--. 1 root root   0 Apr 12 03:04 new_file.yml  ##對該檔案加密
-rw-------. 1 root root 355 Apr 12 02:46 secretp.yml
-rw-------. 1 root root 355 Apr 12 02:43 secret.yml
-rw-r--r--. 1 root root   6 Apr 12 02:46 serpass
[[email protected] min]# ansible-vault encrypt new_file.yml  ##加密
New Vault password: 
Confirm New Vault password: 
Encryption successful
[[email protected] min]# 
           

可以同時對多個現有檔案加密

#對多個檔案進行加密
[[email protected] min]# ansible-vault encrypt file1 file2  ##加密file1 file2

#通過加密檔案,對其進行加密
[[email protected] min]# ansible-vault  encrypt old_file.ymml file.yml --vault-password-file=secpass
 Encryption successful  ##此時兩個問價密碼相同

           

3、編輯加密檔案(edit)

加密檔案的編輯原理:在加密時,打開編輯器,會臨時建立新的文本,當編輯完該文本後,系統會自動将該文本内容追加到加密文本下,并删除臨時建立的文本,達到加密的目的。

#編輯加密文本
[[email protected] min]# ansible-vault view old_file.ymml 
Vault password:               ##輸入密碼通過認證
 
           

4、檢視加密檔案(view)

當使用普通檔案檢視時,是一堆亂碼,隻有使用正确的檢視方式,通過認證,才可檢視檔案内容,

#檢視加密檔案
[[email protected] min]# ansible-vault view old_file.ymml  
Vault password:                       ##進行認證
this is a secert file

#普通方式檢視
[[email protected] min]# cat old_file.ymml 
$ANSIBLE_VAULT;1.1;AES256
34393265393666326337393363336639643861316565333334313232393533303735386339626465
3439326439626165666638363030613235633763326638300a633964663561366464633134663766
63653765326535646464303933636531653362646264353934383030643538333531613135346230
3536326364313962340a646230633230396634653864633834613332653434383761373463383837
32636530323534656463643332336564346432366130646234373162393761646235
           

5、密碼更新(rekey)

5.1 直接更新

#通過互動式界面更新
[[email protected] min]# ansible-vault rekey old_file.ymml  ##一次性可更新多個檔案
Vault password:                ##輸入原密碼
New Vault password:            ##兩次更新新密碼
Confirm New Vault password: 
Rekey successful               ##完成更新
           

 【注】雖然一次性可更新多個檔案,但要求多檔案密碼必須相同,否則不能通過認證并更新。

5.2 通過檔案更新

#通過檔案進行密碼更新
[[email protected] min]# ansible-vault rekey --new-vault-password-file=secpass old_file.ymml 
Vault password:    ##輸入原始認證密碼
Rekey successful
           

 【注】檔案中的字元全為密碼,且同時可更新多個原密碼相同檔案

6、加密檔案的使用

6.1 提供互動式的使用方式(--vault-id @prompt)

#playbook加密時,未輸入密碼
[[email protected] secert]#ansible-playbook user.yml 
ERROR! Attempting to decrypt but no vault secrets found

#playbook加密時,輸入錯誤密碼
[[email protected] secert]# ansible-playbook --vault-id @prompt user.yml 
Vault password (default): 
ERROR! Decryption failed (no vault secrets were found that could decrypt) on /etc/ansible/secert/user.yml

#playbook加密時,輸入密碼,通過認證
[[email protected] secert]# ansible-playbook --vault-id @prompt user.yml 
Vault password (default): 

PLAY [Configure User] *****************************************************************

TASK [Gathering Facts] ****************************************************************
ok: [servera.linux.com]
....
           

6.2 提供自動詢問方式 (--vault-password-file=passwordfile)

#通過檔案的方式進行認證
[[email protected] secert]# ansible-playbook --vault-password-file=passwd user.yml 

PLAY [Configure User] *****************************************************************

TASK [Gathering Facts] ****************************************************************
ok: [servera.linux.com]

TASK [Linux user] *********************************************************************
ok: [servera.linux.com]

....
           

7、解密(decrypt)

#對加密檔案解密
[[email protected] secert]# ansible-vault decrypt user.yml 
Vault password:        ##輸入認證
Decryption successful
[[email protected] secert]# cat user.yml 
---
- name: Configure User
  hosts: servera.linux.com      
  tasks:
    - name: Linux user
      user:
        name: linux
        uid: 1000
        state: present
...

           

解密時可保留原檔案,并進行檔案更名輸出

#檢視原加密檔案
[[email protected] secert]# cat sec.yml 
$ANSIBLE_VAULT;1.1;AES256
64376331643766343164666266626539633961643135623234613235353765393838623765316363
3066613232383365366539623930326561326663643733340a333664363866636434393237616136
65333231616662643631656663343732343764386332316133663965646131396562313566636139
3863623737306461310a626537623332306335373636353539653135633465323330393365666135
6237

#解密檔案,并重定向輸出
[[email protected] secert]# ansible-vault decrypt sec.yml --output=nopasswd
Vault password:        #密碼認證
Decryption successful

#檢視解密檔案
[[email protected] secert]# cat nopasswd 
asfawsfa

           
對于我們儲存的密碼檔案,可以通過權限設定使其對普通使用者不開放,進而提高安全性

繼續閱讀