我正在運行ansible的原始模組使用非 root 用戶並想要發出一個檢查某些條件的命令。例如,我想在刪除目錄之前檢查它是否存在。就像下面的命令一樣。
[ -d "$DIR" ] && echo "Yes"
現在我想授予非 root 使用者在 sudoers 檔案中執行此命令的權限。
xyzUser ALL = (ALL) NOPASSWD: 'something/that/lets/me/run/that/command'
所以,我想知道我可以在 sudoers 中編寫什麼,讓我運行此命令和所有其他基於條件的命令。
答案1
例如,我想在刪除目錄之前檢查它是否存在。
- name: Example playbook to make sure a dir is absent
hosts: my_host_group
# Connect with a non-root user
remote_user: my_remote_user
# Use sudo, as you say you need it. Configure it on the relevant host
become: true
vars:
dir_to_remove: /path/to/dir/on/host
tasks:
- name: "remove {{ dir_to_remove }} if exists. Do nothing otherwise"
file:
path: "{{ dir_to_remove }}"
state: absent