[本文基于Ansible 2.7]
設定切換使用者執行後,某任務報出以下錯誤:
Failed to change ownership of the temporary files Ansible needs to create despite connecting as a privileged user. Unprivileged become user would be unable to read the file.
看起來是說無法修改遠端臨時目錄的屬主。直接在源碼中搜尋錯誤資訊,找到如下代碼:
ansible/plugins/action/__init__.py: 447~451
res = self._remote_chown(remote_paths, self._play_context.become_user)
if res['rc'] != 0 and remote_user in self._get_admin_users():
# chown failed even if remote_user is administrator/root
raise AnsibleError('Failed to change ownership of the temporary files Ansible needs to create despite connecting as a privileged user. '
'Unprivileged become user would be unable to read the file.')
是以_remote_chown的傳回碼為非零值。
ansible/plugins/action/__init__.py: 484~490
def _remote_chown(self, paths, user, sudoable=False):
'''
Issue a remote chown command
'''
cmd = self._connection._shell.chown(paths, user)
res = self._low_level_execute_command(cmd, sudoable=sudoable)
return res
遠端執行指令的内容是由self._connection._shell.chown拼出來的。chown實作于ShellBase:
ansible/plugins/shell/__init__.py:100~105
def chown(self, paths, user):
cmd = ['chown', user]
cmd.extend(paths)
cmd = [shlex_quote(c) for c in cmd]
return ' '.join(cmd)
其實就簡單拼接了一下,shlex_quote就等于shlex.quote(有興趣可閱讀ansible/module_utils/six/__init__.py, 裡面關于MovedAttribute的定義)。
總之這個操作是為了将由登入使用者建立的目錄/檔案(預設屬主為登入使用者)的屬主修改為用于執行遠端任務的使用者(become_user),這樣become_user就必定可以讀取臨時目錄(以及其他在特定情況下需要的目錄)下以及臨時檔案/其他必要的檔案中的内容。
然而這條chown指令卻執行失敗了。
一般來說常見的造成chown錯誤的原因無非是:
- 目錄或檔案不存在
- 沒有權限修改屬主
- 目标使用者不存在
臨時目錄是由登入使用者建立的,按道理說前兩點可以排除了,是以先檢查一下become_user是不是存在,是不是寫錯了。如果不存在become_user錯誤,那就隻好将生成的cmd(即上文chown的傳回值)列印出來,到目标伺服器上去看看報錯的具體原因了。