有人可以幫助我找到在檔案中宣告所有變數并在 ansible playbook 中定義檔案路徑的最佳方法嗎?這是我的 ansible-playbook
---
- hosts: myhost
vars:
- var: /root/dir.txt
tasks:
- name: print variables
debug:
msg: "username: {{ username }}, password: {{ password }}"
這些是dir.txt里面的內容
username=test1
password=mypassword
當我運行這個時,我面臨一個錯誤
TASK [print variables] *********************************************************************************************************
fatal: [121.0.0.7]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'username' is undefined\n\nThe error appears to be in '/root/test.yml': line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: print variables\n ^ here\n"}
預期輸出是通過這種方法列印變數任何幫助將不勝感激
uj5u.com熱心網友回復:
您的代碼不起作用有兩個原因。
- 變數檔案應該是 YAML 或 JSON 格式,以便 Ansible 加載它
- 這些變數檔案是用
vars_files指令或include_vars任務加載的,而不是vars
所以一個名為 的 YAML 檔案/root/dir.yml:
username: test1
password: mypassword
可以在劇本中使用,如:
---
- hosts: myhost
vars_files:
- /root/dir.yml
tasks:
- name: print variables
debug:
msg: "username: {{ username }}, password: {{ password }}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/354116.html
上一篇:列出寫入檔案的檔案夾的子檔案夾
