使用become
更改用戶的默認 shell 不會給出預期的結果。使用:
- name: Change default shell to zsh
shell: sudo chsh -s $(which zsh) $(whoami)
/etc/passwd
好像:
root:x:0:0:root:/root:/bin/bash
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/usr/bin/zsh
這是預期的結果,ubuntu
即將用戶的默認 shell 更改為zsh
. 現在,我寧愿使用become
:
- name: Change default shell to zsh
become: true
shell: chsh -s $(which zsh)
但/etc/passwd
看起來像:
root:x:0:0:root:/root:/usr/bin/zsh
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
請注意,僅zsh
設定為root
。
我在這里想念什么?become: true
加號不shell: chsh -s $(which zsh)
等于sudo chsh -s $(which zsh) $(whoami)
orsudo chsh -s $(which zsh)
嗎?
如果我添加$(whoami)
如下:
- name: Change default shell to zsh
become: true
shell: chsh -s $(which zsh) $(whoami)
我得到相同的結果,root
但zsh
不是我要更改的用戶。
uj5u.com熱心網友回復:
我會使用這樣的東西:
- name: Ensure the user 'ubuntu' has a zsh shell.
user:
name: ubuntu
shell: /bin/zsh
state: present
become: yes
uj5u.com熱心網友回復:
這有效:
- name: Register current user (workaround to change default shell)
shell: whoami
register: current_user
- name: Change default shell to zsh
become: true
shell: "chsh -s $(which zsh) {{ current_user.stdout }}"
有關詳細資訊,請參閱上面的評論。簡而言之,我必須先運行,whoami
以便在運行時使用正確的用戶chsh
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480610.html