运行 ansible 脚本后无法登录 root

运行 ansible 脚本后无法登录 root

我目前无法登录我的超级用户。以下是事件的顺序

~ su  
password:  
su: using restricted shell which zsh  
su: failed to execute which zsh: No such file or directory  

之前我曾尝试使用 ansible 实现自动化。

- hosts: localhost
  become: true
  tasks: 
  - name: Install zsh
    apt: name=zsh
  - name: change shell
    shell: chsh -s `which zsh`
  - name: Install ohmyzsh autosuggestions plugin
    ansible.builtin.git:
      repo: 'https://github.com/zsh-users/zsh-autosuggestions.git ~/.oh-my-zsh/plugins/zsh-autosuggestions'
      dest: "~/.oh-my-zsh/plugins/zsh-autosuggestions"
  - name: Update our zshrc
    shell: sed -i 's/(git/(git zsh-autosuggestions' ~/.zshrc

当我运行剧本时,有时会在任务部分失败

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [localhost]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"failed": true, "module_stderr": "sudo: a password is required\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}}, "msg": "The following modules failed to execute: ansible.legacy.setup\n"}

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

有时它会进入 chsh 部分(我认为是在我更改 shell 或为 python 创建别名或更改 ansible 脚本(摆脱某些步骤)时发生的。我不完全理解它,但它给了我 chsh 部分的错误

fatal: [localhost]: FAILED! => {"changed": true, "cmd": "chsh -s `which zsh`", "delta": "0:00:00.006789", "end": "2023-08-03 19:46:19.294159", "msg": "non-zero return code", "rc": 1, "start": "2023-08-03 19:46:19.287370", "stderr": "Password: chsh: PAM: Authentication failure", "stderr_lines": ["Password: chsh: PAM: Authentication failure"], "stdout": "", "stdout_lines": []}

总的来说,我想修复两个问题,一是超级用户帐户,二是 ansible 脚本,这样就不会再发生这种情况了

答案1

似乎which zsh在这个 ansible 任务中

- name: change shell
    shell: chsh -s `which zsh`

未进行评估,默认 shell 被设置为which zsh而不是 zsh 的路径。您将 root 的登录 shell 设置为不存在的内容。当您以 root 身份登录时,它会尝试查找名为 的登录 shell which zsh,但找不到它并放弃。

您可以更改任务以使用固定路径,如下所示:

- name: change shell
    shell: chsh -s /path/to/zsh

如果您在路径不同的不同系统上运行剧本zsh,则可以在运行“更改 shell”任务之前将其读入单独任务中的变量。

相关内容